正则表达式不会产生错误回声,有人可以帮助我吗?

时间:2012-08-31 15:56:16

标签: php regex forms preg-match

当字段不正确时,正则表达式不会产生错误回显,除了我得到警告:preg_match()[function.preg-match]:编译失败:丢失)在第58行的偏移12处。有人可以帮助我吗?我非常绝望。

$array = array(
               "error1" == "1",
               "error2" == "2",
               "error3" == "3",
               "error4" == "4",
               "error5" == "5",
               "error6" == "6",
               "error7" == "7",
               "error8" == "8",
               "error9" == "9",
               "error10" == "10",
               "error11" == "11",
               "error12" == "12",
              );


    //the regular expressions of the variables in the form

    function ValidateForm($string, $type){

        switch($type)
            {
            case 'name':
                $pattern="/^[A-Za-z. -]+$/";
                break;
            case 'sex':
                $pattern="/[MWmw]/";
                break;
            case 'insertion':
                $pattern="/^([A-Za-z]*$/";
                break;
            case 'initials':
                $pattern="/^([A-Z]\.)+$/";
            break;
            case 'adress':
                $pattern="/^([A-Za-z -])+[0-9]+([a-z -])*$/";
                break;
            case 'postcode':
                $pattern="/^[1-9][0-9]{3}[]?[A-Za-z]{2}$/";
                break;
            case 'phone':
                $pattern="/^[0-9]{10}$/";
                break;
            case 'email':
                $pattern="/(^[0-9a-zA-Z_\.-]{1,}@([0-9a-zA-Z_\-]{1,}\.)+[0-9a-zA-Z_\-]{2,}$)/";
                break;
            case 'postbus':
                $pattern="/^[A-Z][a-zA-Z -]+$/";
                break;
            }



            if (preg_match($pattern, $string)){
                    return true;
                } else {
                    return false;
                }   
            }   



    if (isset($_POST['submitted'])){        


            if (ValidateForm($_POST["surname"],'name')){
                $error1 = true; 
            }else{
                $error1 = false; 
            } 

            if (ValidateForm($_POST["sex"],'sex')){
                $error2 = true; 
            }else{
                $error2 = false; 
            }

            if (ValidateForm($_POST["insertion"],'insertion')){
                $error3 = true;
            }else{
                $error3 = false; 
            }

            if (ValidateForm($_POST["initials"],'initials')){
                $error4 = true;
            }else{
                $error4 = false; 
            }

            if (ValidateForm($_POST["adress"],'adress')){
                $error5 = true;
            }else{
                $error5 = false; 
            }

            if (ValidateForm($_POST["postcode"],'postcode')){
                $error6 = true;
            }else{
                $error6 = false; 
            }           

            if (ValidateForm($_POST["postbus"],'postbus')){
                $error7 = true;
            }else{
                $error7 = false; 
            }

            if (ValidateForm($_POST["private_phone"],'phone')){
                $error8 = true;
            }else{
                $error8 = false; 
            }

            if (ValidateForm($_POST["mobile_phone"],'phone')){
                $error9 = true;
            }else{
                $error9 = false; 
            }

            if (ValidateForm($_POST["work_phone"],'phone')){

                $error10 = true;

            }else{$error10 = false;
            }

            if (ValidateForm($_POST["work_email"],'email')){
                $error11 = true;
            }else{
                $error11 = false;
            }

            if (ValidateForm($_POST["private_email"],'email')){
                $error12 = true;
            }else{
                $error12 = false; 
            }



    //makes an connection to the db
            mysql_connect("localhost", "root", '') or die(mysql_error());
            mysql_select_db('databaseimage') or die(mysql_error());


    //Here the form vars will be inserted into the database, as you can see above the mysql_query knows where to put it in


            mysql_query("INSERT INTO form (Surname, Insertion, Initials, Name, Sex, Adress, Postcode, Postbus, Location, Private_phone, Mobile_phone, Work_phone, Private_email, Work_email)
            VALUES
            ('".$_POST['surname']."','".$_POST['insertion']."','".$_POST['initials']."','".$_POST['name']."','".$_POST['sex']."','".$_POST['adress']."','".$_POST['postcode']."','".$_POST['postbus']."','".$_POST['location']."','".$_POST['private_phone']."','".$_POST['mobile_phone']."','".$_POST['work_phone']."','".$_POST['private_email']."','".$_POST['work_email']."')")
            or die(mysql_error());  

            }
    ?>

<html>
            <head>
                <meta charset="utf-8"/>
                <meta http-equiv="X-UA-Compatible" content="chrome=1" />
                <link rel="stylesheet" href="css/form.css" type="text/css"  />
                <link rel="stylesheet" href="css/style.css" type="text/css"  />



        <script language = "javascript">

            function DisableButton1() {
                        var surname = document.getElementById("surname").value;
                        var disableButton1 = false;
                        if (surname == "") disableButton1 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton1;
                    }

            function DisableButton2() {
                        var insertion = document.getElementById("insertion").value;
                        var disableButton2 = false;
                        if (insertion == "") disableButton2 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton2;
                    }

            function DisableButton3() {
                        var initials = document.getElementById("initials").value;
                        var disableButton3 = false;
                        if (initials == "") disableButton3 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton3;
                    }

            function DisableButton4() {
                        var name = document.getElementById("name").value;
                        var disableButton4 = false;
                        if (name == "") disableButton4 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton4;
                    }

            function DisableButton5() {
                        var sex = document.getElementById("sex").value;
                        var disableButton5 = false;
                        if (sex == "") disableButton5 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton5;
                    }

            function DisableButton6() {
                        var adress = document.getElementById("adress").value;
                        var disableButton6 = false;
                        if (adress == "") disableButton6 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton6;
                    }

            function DisableButton7() {
                        var postcode = document.getElementById("postcode").value;
                        var disableButton7 = false;
                        if (surname == "") disableButton7 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton7;
                    }

            function DisableButton8() {
                        var postbus = document.getElementById("postbus").value;
                        var disableButton8 = false;
                        if (postbus == "") disableButton8 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton8;
                    }
            function DisableButton9() {
                        var location = document.getElementById("location").value;
                        var disableButton9 = false;
                        if (location == "") disableButton9 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton9;
                    }

            function DisableButton10() {
                        var private_phone = document.getElementById("private_phone").value;
                        var disableButton10 = false;
                        if (private_phone == "") disableButton10 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton10;
                    }

            function DisableButton11() {
                        var mobile_phone = document.getElementById("mobile_phone").value;
                        var disableButton11 = false;
                        if (mobile_phone == "") disableButton11 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton11;
                    }

            function DisableButton12() {
                        var work_phone = document.getElementById("work_phone").value;
                        var disableButton12 = false;
                        if (work_phone == "") disableButton12 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton12;
                    }
            function DisableButton13() {
                        var private_email = document.getElementById("private_email").value;
                        var disableButton13 = false;
                        if (private_email == "") disableButton13 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton13;
                    }

            function DisableButton14() {
                        var work_email = document.getElementById("work_email").value;
                        var disableButton14 = false;
                        if (work_email == "") disableButton14 = true;
                        document.forms['form'].elements['submit'].disabled = disableButton14;
        }



            for (i = 0; i < frm_elements.length; i++)
                {
            field_type = frm_elements[i].type.toLowerCase();
            switch (field_type)
            {
            case "text":
            case "password":
            case "textarea":
            case "hidden":
                frm_elements[i].value = "";
                break;
            case "radio":
            case "checkbox":
                if (frm_elements[i].checked)
                {
                    frm_elements[i].checked = false;
                }
                break;
            case "select-one":
            case "select-multi":
                frm_elements[i].selectedIndex = -1;
                break;
            default:
                break;
            }
        }
        </script>   

            </head>

            <?php





            if (isset($_POST['submit'])){   

                    if ($error1 == true){
                         echo $_POST["surname"]." surname is invalid";
                    }

                    if ($error2 == true){
                         echo $_POST["sex"]." sex is invalid";
                    }

                    if ($error3 == true){
                         echo $_POST["insertion"]." insertion is invalid";
                    }

                    if ($error4 == true){
                         echo $_POST["initials"]." initials is invalid";

        }
                    if ($error5 == true){
                         echo $_POST["adress"]." adress is invalid";
                    }

                    if ($error6 == true){
                         echo $_POST["postcode"]." postcode is invalid";
                    }

                    if ($error7 == true){
                         echo $_POST["postbus"]." postbus is invalid";
                    }

                    if ($error8 == true){
                         echo $_POST["private_phone"]." private phone is invalid";
                    }

                    if ($error9 == true){
                         echo $_POST["mobile_phone"]." mobile_phone is invalid";
                    }

                    if ($error10 == true){
                         echo $_POST["work_phone"]." work phone is invalid";
                    }

                    if ($error11 == true){
                         echo $_POST["work_email"]." work email is invalid";
                    }

                    if ($error12 == true){
                         echo $_POST["private_email"]." private_email is invalid";
                    }
                }

            ?>

            <body>

            <div id="top_bit">

            <h1>Registration form</h1>



            <div class="boxed"> 

            <form ction="<?php echo $_SERVER['PHP_SELF'];?> name="form" method="post">   
            <div id="first_part">       
                <label for="name">Surname</label> 
                    <input type="text" name="surname" id="surname" placeholder="last name" value="Worst" onKeyUp="DisableButton1()"/>

                <label for="website">Insertion</label> 
                    <input type="text" name="insertion" id="insertion" placeholder="If you have one" value="Jaap" onKeyUp="DisableButton2()"/>

                <label for="number">Initials</label> 
                    <input type="text" name="initials" id="initials" maxlength="8" size="8" value="J.P" onKeyUp="DisableButton3()"/>

                <label for="name">Name</label> 
                    <input type="text" name="name" id="name" value="Hans" onKeyUp="DisableButton4()"/>

                <label for="sex">Sex</label> 
                    <input type="text" name="sex" id="sex" maxlength="3" size="3" placeholder="M/W" value="M" value="sex" onKeyUp="DisableButton5()"  />

                <label for="name">Adress</label> 
                    <input type="text" name="adress" id="adress" value="Donkeresteegstraat 78" onKeyUp="DisableButton6()" />
            </div>
            <div id="float_form">

                <label for="name">Postcode</label> 
                    <input type="text" name="postcode" id="postcode" value="1070 HR"  onKeyUp="DisableButton7()"/>

                <label for="name">Postbus</label> 
                    <input type="text" name="postbus" id="postbus"  value="jaap"  onKeyUp="DisableButton8()" />

                <label for="name">Location</label> 
                    <input type="text" name="location" id="location" value="Amsterdam" onKeyUp="DisableButton9()" />

                <label for="name">Private Phone</label> 
                    <input type="number" name="private_phone" id="private_phone" value="0206234567" maxlength="10" size="9" onKeyUp="DisableButton10()" />

                <label for="name">Mobile Phone</label>
                    <input type="number" name="mobile_phone" id="mobile_phoene" value="0654322345" maxlength="10" size="9" onKeyUp="DisableButton11()" />

                <label for="name">Work Phone</label>
                    <input type="number" name="work_phone" id="work_phone" value="0206234567" maxlength="10" size="9" onKeyUp="DisableButton12()" />
            </div>
            <div id="float_form_2"
                <label for="name">Private Email</label> 
                    <input type="email" name="private_email" id="private_email" value="hangslotman@gmail.com" onKeyUp="DisableButton13()" />

                <label for="name">Work Email</label> 
                    <input type="email" name="work_email" id="work_email" value="hangslotman@gmail.com" onKeyUp="DisableButton14()" />
            </div>

                    <input type="hidden" name="submitted" value="1" /> 

                <div id="reset_form"><input type="button" name="reset_form" value="Reset Form" onclick="this.form.reset();"></div>      


                <input type="submit" value="Send information" id="submit"/>

            </form>



            </div>

        </div>



            </body>

2 个答案:

答案 0 :(得分:2)

你的考试:

if (isset($_POST['submit'])){

永远不会被验证,因为提交按钮没有名称属性

将其改为:

<input type="submit" value="Send information" id="submit"/>

<input type="submit" name="submit" value="Send information" id="submit"/>

答案 1 :(得分:1)

你打开了

的支架
case 'insertion':
            $pattern="/^([A-Za-z]*$/";
            break;

但你在哪里关闭它?如果将其更改为

,错误可能会消失
case 'insertion':
                $pattern="/^([A-Za-z])*$/";
                break;