客户端和服务器端(JavaScript和PHP验证)

时间:2014-01-19 08:14:50

标签: javascript php

如何使用JavaScript检查空字段以及在服务器端使用PHP验证电子邮件,用户名,以便进行客户端验证。我是初学者。

到目前为止,我已在客户端完成

 <script type="text/javascript">

       function checkme()
       {
           if(document.myform.uname.value == "")
           {
               alert("Please Enter your User Name")
               document.myform.uname.focus();
               return false;
           }

           if(document.myform.mail.value == "")
           {
               alert("Please Enter Your E-mail Address");
               document.myform.mail.focus();
               return false;
           }
return true;

}

    </script>
    </head>
<body>

    <form name="myform"  method="POST" action="index.php" onsubmit="return checkme();">


        User Name : <input type="text" name="uname" /><label> * </label>
        <br />

        E-Mail : <input type="text" name="mail" /><label> * </label>
        <br />
        <input type="submit" name="register" value="Register" />


    </form>

</body>

到目前为止,我已在服务器端完成

    <?php

    $uname = $_POST["uname"];  $unameerr = $mailerr = "" ;
    $mail = $_POST["mail"] ;

    if (! isset($_POST['register']))
    {
        echo "You need to submit the form " ;
    }

    if (empty($_POST["uname"]) || empty($_POST["mail"]))
    {
        echo "All fields are required" ;
        exit ;
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
     if(strlen($_POST["uname"])< 2 || (strlen($_POST["uname"])>12))

            {
                $unameerr = "Please Enter your USER NAME with in 2-12 characters" ;
            }
            else
            {
                $uname = $_POST["uname"] ;
            }

        if(!preg_match("/^[A-Za-z0-9_.]+\@+[A-Za-z]+\.+[a-z{2,4}]$/", $mail))

            {
                $mailerr = "Please Enter your email address in correct format[abc@abc.com]" ;
            }
            else
            {
                $mail = $_POST["mail"] ;
            }
    }    
    echo "Your inputs are :" ;

    echo "<br />" ;

    echo "Your User name is : " . $uname ;

    echo "<br />" ;

    echo "Your Registered E-mail is : " . $mail ;






    ?>

    <form>      <input type="button" value="Print A Copy" onsubmit="window.print()" /> </form>

任何建议???

1 个答案:

答案 0 :(得分:0)

既然你没有向我们解释你面临的困难。我建议你有一个很好的教程。

Click Here for the PHP validation Tute

Click Here for the Javascript validation Tute