Home  >  Article  >  Web Front-end  >  Introduction to Jquery Validate validation and code examples of jQuery form validation

Introduction to Jquery Validate validation and code examples of jQuery form validation

不言
不言Original
2018-08-14 17:01:451389browse

This article brings you an introduction to Jquery Validate verification and code examples of jQuery form verification. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

JQuery framework validation: validate framework

Jquery Validate validation rules

(1)required:true Required field
(2)remote:”check.PHP” Use the ajax method to call check.php to verify the input value
(3)email:true You must enter an email in the correct format
(4)url:true You must enter the URL in the correct format
(5)date:true You must enter the date in the correct format
(6)dateISO:true You must enter the date (ISO) in the correct format, for example: 2009-06-23, 1998/01/22 Only verify the format, not the validity
(7)number:true Must enter legal numbers (negative numbers, decimals)
(8)digits:true Must enter an integer
(9)creditcard: You must enter a legal credit card number
(10)equalTo:"#field" The input value must be the same as #field
(11)accept: Enter a string with a legal suffix ( Suffix of the uploaded file)
(12)maxlength:5 Enter a string with a maximum length of 5 (Chinese characters count as one character)
(13)minlength:10 Enter a string with a minimum length of 10 (Chinese characters count as one character) )
(14)rangelength:[5,10] The input length must be between 5 and 10") (Chinese characters count as one character)
(15)range:[5,10] Input value Must be between 5 and 10
(16)max:5 The input value cannot be greater than 5
(17)min:10 The input value cannot be less than 10

Jquery Validate Custom validation Rules

addMethod(name, method, message) method:
The parameter name is the name of the added method
The parameter method is a function that receives three parameters (value, element, param ) value is the value of the element, element is the element itself param
is the parameter, we can use addMethod to add validation methods other than built-in Validation methods. For example, if there is a field, only
can enter a letter, range It is a-f, written as follows:

$.validator.addMethod(“af”,function(value,element,params){
if(value.length>1){
return false;
}
if(value>=params[0] && value<=params[1]){
return true;
}else{
return false;
}
},”必须是一个字母,且a-f”);
用的时候,比如有个表单字段的id=”username”,则在rules 中写
username:{
af:["a","f"]
}

method
The first parameter of addMethod is the name of the added verification method. At this time, it is the third parameter of af
addMethod, which is custom The error message here is: "Must be a letter, and a-f"
The second parameter of addMethod is a function, which is more important and determines the writing method when using this verification method
If only For a parameter, write it directly. If af: "a", then a is the only parameter. If multiple parameters are used in [], separate them with commas

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .error{
            color: red;
        }
    </style>


<script src="js/jquery.min.js"></script>
<!-- 导入的框架 -->
<script src="js/jquery.validate.min.js"></script>
<script>
    $(function(){
        $(&#39;#a&#39;).validate({
            rules:{
                username:{
                    required:true
                },
                password_1:{
                    required:true,
                    rangelength:[5,10],

                },
                password_2:{
                    required:true,
                    equalTo:&#39;#pa&#39;
                },
                sex:{
                    required:true
                },
                you:{
                    required:true,
                    email:true
                },

            },
            messages:{
            username:{
                required:&#39;字段不能为空&#39;
               },
               password_1:{
                required:&#39;字段不能为空&#39;,
                rangelength:&#39;密码长度要在5到10位&#39;,
               },
               password_2:{
                required:&#39;字段不能为空&#39;,
                equalTo:&#39;两次密码不一样&#39;
               },
               sex:{
                required:&#39;性别不能为空&#39;,
               },
               you:{
                required:&#39;邮箱不能为空&#39;,
                email:&#39;邮箱格式不对&#39;
               }
           }
        })
    })
</script>
</head>
<body>
    <form action="a.jsp" method="post" id="a">
        请输入姓名:<input type="text" name="username" ><br>
        请输入密码: <input type="password" name="password_1" id="pa"><br>
        确认密码: <input type="password" name="password_2" ><br>
      
        性别: <input type="radio" name="sex" value="男">男
             <input type="radio" name="sex" value="女">女
             <label for="sex" generated="true"></label><br>
             邮箱: <input type="text" name="you" ><br>
             <input type="submit" value="submit">
    </form>
</body>
</html>

The second one is js Form validation written for blur event:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    /* form{
      margin-left:400px;
  } */
</style>
<script src="js/jquery.min.js"></script>

<script>
    $(function () {
        var a = b = c = d = e = f = g = false;
        $("#tables").css({ "border": &#39;1px solid blue&#39;, &#39;text-align&#39;: &#39;center&#39; }).css("width", "800").css("height", "400px")
        $(&#39;td&#39;).css({ "border": "1px solid blue" })
        $("#td1").css({ &#39;width&#39;: &#39;100&#39; })
        $("#td2").css({ "width": "400" })
        $("#td3").css({ "width": "300" })
        // 设置id a的颜色 
        $(&#39;span&#39;).css(&#39;color&#39;, &#39;red&#39;)
        //登录名的验证
        $("input[name=&#39;username&#39;]").blur(function () {
            var va = $(this).val();
            var char = va.charCodeAt(0);
            //alert(va);
            if (va == "") {
                a = false;
                // $(this).click(function(){
                //     $(&#39;#a&#39;).css(&#39;background&#39;,&#39;blue&#39;).css(&#39;width&#39;,&#39;100px&#39;)
                // })        
                $(&#39;#a&#39;).html(function () {
                    return "值不能为空";
                })
            }
            else if (va.length < 6) {
                a = false;
                $(&#39;#a&#39;).html(&#39;登录名不能少于6个字&#39;)
            }
            else if (!((char >= 65 && char <= 90) || (char >= 97 && char <= 122))) {
                a = false;
                $(&#39;#a&#39;).html(&#39;登录名的首字母只能为字母&#39;)
            }
            else {
                a = true;
                $(&#39;#a&#39;).html(function () {
                    return &#39;&#39;;
                })
            }
        })
        //验证姓氏
        $("input[name=&#39;xing&#39;]").blur(function () {
            var xing = $(this).val();
            if (xing == &#39;&#39;) {
                b = false;
                $(&#39;#b&#39;).html(&#39;值不能为空&#39;)
            }
            else if(xing.length>6){
                b=false;
                $(&#39;#b&#39;).html(&#39;你的姓氏不符合要求,字符长度超过6&#39;)
            }
            else{
                b=true;
                $(&#39;#b&#39;).html(function(){
                    return &#39;&#39;;
                })
            }
        })
        //验证密码
        $(&#39;#password_1&#39;).blur(function(){
            var va=$(&#39;#password_1&#39;).val();
            if(va==&#39;&#39;){
                c=false;
                $(&#39;#c&#39;).html(&#39;密码不能为空&#39;)
            }
           else if(va.length<8){
                c=false;
                $(&#39;#c&#39;).html(&#39;你的密码不足8位数不符合要求&#39;)
            }
            else{
                c=true;
                $(&#39;#c&#39;).html(&#39;&#39;);
            }
        })
        //密码重复验证
        $(&#39;#password_2&#39;).blur(function(){
            //拿到本身的密码
            var va1=$(this).val();
            //拿到之前的密码
            var va2=$(&#39;#password_1&#39;).val();
            if(va1==&#39;&#39;){
                d=false;
                $(&#39;#d&#39;).html(&#39;密码不能为空&#39;)
            }
            else if(va1!=va2){
                d=false;
                $(&#39;#d&#39;).html(&#39;两次密码不正确&#39;)
            }
            else{
                d=true;
                $(&#39;#d&#39;).html(&#39;&#39;)
            }
        })
        //性别选择直接选中下下标为1的
        $(&#39;input[name=sex]:eq(1)&#39;).prop(&#39;checked&#39;,&#39;checked&#39;)
        $(&#39;input[name=sex]&#39;).blur(function(){

        })
        //年验证
        $(&#39;#year&#39;).blur(function(){
            //拿到年的值
            var va=$(this).val();
            // var v=Number(va);
           //alert(va)
           var s=/^[0-9]+$/;
            if(va==&#39;&#39;){
                f=false;
                $(&#39;#f&#39;).hmtl(&#39;年不能为空&#39;)
            }
            // else if(!s.test(va)){
            //     f=false;
            //     $(&#39;#f&#39;).hmtl(&#39;年只能是数字&#39;)
            // }
            else if(isNaN(va)){
                f=false;
                $(&#39;#f&#39;).html(&#39;年只能是数字&#39;)
            }
            else if(va.length!=4){
                f=false;
                $(&#39;#f&#39;).html(&#39;值必须为4位数&#39;)
            }
            else{
                f=true;
                $(&#39;#f&#39;).html(&#39;&#39;) 
            }

        })
    //天数验证
    $("input[name=&#39;day&#39;]").blur(function(){
        var va=$(this).val();
        var t = /^(([1-9])|((1|2)[0-9])|30|31)$/; 
        if(va===&#39;&#39;){
            g=false;
            $(&#39;#f&#39;).html(&#39;天数不能为空&#39;)
        }
        else if(! t.test(va)){
            g=false;
            $(&#39;#f&#39;).html(&#39;对不起,天数必须在 1-31 之间!&#39;)
        }else{
            g=true;
            $(&#39;#f&#39;).html(&#39;&#39;) 
        }
    })
    $(&#39;#su&#39;).click(function(){
      return  a&&b&&c&&d&&f&&g
    })

    })
</script>

<body>
    <form action="s">
        <table id="tables">
            <tr>
                <td colspan="3">
                    <img src="images/d.png" alt="">
                </td>

            </tr>
            <tr>
                <td id="td1">登录名</td>
                <td id="td2">
                    <input id="input1" type="text" name="username">
                </td>
                <td id="td3">
                    <span id="a"></span>
                </td>
            </tr>
            <tr>
                <td>姓氏</td>
                <td>
                    <input type="text" name="xing">
                </td>
                <td>
                    <span id="b"></span>
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td>
                    <input type="password" name="password" id="password_1">
                </td>
                <td>
                    <span id="c"></span>
                </td>
            </tr>
            <tr>
                <td>再次输入密码</td>
                <td>
                    <input type="password" name="password" id="password_2">
                </td>
                <td>
                    <span id="d"></span>
                </td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                    <input type="radio" name="sex" value="男" >男
                    <input type="radio" name="sex" value="女">女
                </td>
                <td>
                    <span id="e"></span>
                </td>
            </tr>
            <tr>
                <td>生日</td>
                <td>
                    <input type="text" name="year" id="year">
                    <select name="month" id="select_1">
                        <option value="一月" selected>一月</option>
                        <option value="二月">二月</option>
                        <option value="三月">三月</option>
                    </select>
                    <input type="text" name="day">
                </td>
                <td>
                    <span id="f"></span>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <input type="reset" value="reset">
                </td>

            </tr>
            <tr>
                <td colspan="3">
                    <input type="submit" value="提交" id="su">
                </td>
            </tr>
        </table>
    </form>
</body>

</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.error{
color: red;
}
</style>
<script src="js/jquery.min.js"></script>
<!-- 导入的框架 -->
<script src="js/jquery.validate.min.js"></script>
<script>
$(function(){
$(&#39;#a&#39;).validate({
rules:{
username:{
required:true
},
password_1:{
required:true,
rangelength:[5,10],
},
password_2:{
required:true,
equalTo:&#39;#pa&#39;
},
sex:{
required:true
},
you:{
required:true,
email:true
},
},
messages:{
username:{
required:&#39;字段不能为空&#39;
},
password_1:{
required:&#39;字段不能为空&#39;,
rangelength:&#39;密码长度要在5到10位&#39;,
},
password_2:{
required:&#39;字段不能为空&#39;,
equalTo:&#39;两次密码不一样&#39;
},
sex:{
required:&#39;性别不能为空&#39;,
},
you:{
required:&#39;邮箱不能为空&#39;,
email:&#39;邮箱格式不对&#39;
}
}
})
})
</script>
</head>
<body>
<form action="a.jsp" method="post" id="a">
请输入姓名:<input type="text" name="username" ><br>
请输入密码: <input type="password" name="password_1" id="pa"><br>
确认密码: <input type="password" name="password_2" ><br>
 
性别: <input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
<label for="sex" generated="true" class="error"></label><br>
邮箱: <input type="text" name="you" ><br>
<input type="submit" value="submit">
</form>
</body>
</html>

Related recommendations:

jquery validata form validation DEMO

jquery form validation submission

The above is the detailed content of Introduction to Jquery Validate validation and code examples of jQuery form validation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn