简体   繁体   English

用于表单验证的正则表达式

[英]Regex for form validation

hello I have a simple form to validate I already asked about a client side validation but the problem now is that server side validation isn't working the idea is to validate the input field and check if its length between [5-20] chars and display an error msg if not as well as preventing the form from submitting this should happen at the client side then if every thing is right the data should be sent to the server and the server will check if the input data contains just letters if yes then it will echo "thanks for submitting the form " and if not display the error message " the name should contain only letters from a to z" the problem is after the form being submitted and I get the ok from the client side validation I never get one of the server messages please help here is the code 您好,我有一个简单的表单来验证,我已经问过客户端验证了,但是现在的问题是服务器端验证无法正常工作,这个想法是验证输入字段并检查其长度是否在[5-20]个字符之间,如果不是,则显示错误消息,并阻止表单提交,这将在客户端发生,然后,如果一切正常,则应将数据发送到服务器,服务器将检查输入数据是否仅包含字母(如果是),然后它将回显“感谢提交表单”,如果未显示错误消息“名称应仅包含从a到z的字母”,问题出在提交表单后,并且我从客户端验证中获得确定,但从未收到服务器消息之一,请帮助,这里是代码

 <?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $name = $_POST['user'];

    if(!preg_match("/^[a-zA-Z ]*$/",$name))
         echo "name must contain only characters";   
       else
         echo "thanks for submitting the form";


}
?>
<!doctype html>
<html>
<head>
    <title>form validation</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

    <script>
       $( document ).ready(function() 
        {

                $("form").on('submit', myfunction); //<-- listen for form submit, not button click

                function myfunction(evt)
                { //<-- the event object is automatically passed along;
                  //    this is key for suppressing submission
                    name = $("#user").val();
                    nameError = $(".error").first();
                    myNameError = "";

                    if(name.length < 5 || name.length > 20)
                        myNameError += "length should be between 5 and 20 ";
                    else
                        myNameError += "ok"

                    if (myNameError) 
                    {
                        evt.preventDefault(); //<-- suppress submission if error found
                        nameError.html(myNameError);
                    }



                 }


        });
    </script>

    <style>
        .error
        {
            color : red;
            text-transform: capitalize;
            margin-left: 20px;
        }
    </style>
</head>
<body>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>">
        NAME : <input type="text" name="user" id="user"><span class="error"></span><br />
        <input type="submit" value="Submit" id="submit">
    </form>

</body>
</html>

This is definitely a problem: 这绝对是一个问题:

if (name.length < 5 || name.length > 20)
    myNameError += "length should be between 5 and 20 ";
else
    myNameError += "ok"

if (myNameError) 
{
     ...

You are setting the myNameError variable to a string, either the error message or the string ok . 您正在将myNameError变量设置为字符串,错误消息或字符串ok So if (myNameError) is always true so you will never make any POST request to the server. 因此, if (myNameError)始终为true那么您将永远不会向服务器发出任何POST请求。

To solve this problem, you should not add any string to myNameError when there is no error. 要解决此问题,请不要在没有错误的情况下向myNameError添加任何字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM