jquery validata js_表單驗證

一、首先在加入函數 $("#login").validate()的時候要注意裏面默認的規則,有很多是不需要加上去的,詳見最後的例子;

 (1)required:true                必輸字段
(2)remote:"check.php"      使用ajax方法調用check.php驗證輸入值
(3)email:true                    必須輸入正確格式的電子郵件
(4)url:true                        必須輸入正確格式的網址
(5)date:true                      必須輸入正確格式的日期 日期校驗ie6出錯,慎用
(6)dateISO:true                必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗證格式,不驗證有效性
(7)number:true                 必須輸入合法的數字(負數,小數)
(8)digits:true                    必須輸入整數
(9)creditcard:                   必須輸入合法的信用卡號
(10)equalTo:"#field"          輸入值必須和#field相同
(11)accept:                       輸入擁有合法後綴名的字符串(上傳文件的後綴)
(12)maxlength:5               輸入長度最多是5的字符串(漢字算一個字符)
(13)minlength:10              輸入長度最小是10的字符串(漢字算一個字符)
(14)rangelength:[5,10]      輸入長度必須介於 5 和 10 之間的字符串")(漢字算一個字符)
(15)range:[5,10]               輸入值必須介於 5 和 10 之間
(16)max:5                        輸入值不能大於5
(17)min:10                       輸入值不能小於10

二、導入jquery跟validate.js就不說了吧~~~

三、你在不寫message自定義的時候,它會有默認提示,如下;

messages: {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    dateDE: "Bitte geben Sie ein g眉ltiges Datum ein.",
    number: "Please enter a valid number.",
    numberDE: "Bitte geben Sie eine Nummer ein.",
    digits: "Please enter only digits",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: $.validator.format("Please enter no more than {0} characters."),
    minlength: $.validator.format("Please enter at least {0} characters."),
    rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
    range: $.validator.format("Please enter a value between {0} and {1}."),
    max: $.validator.format("Please enter a value less than or equal to {0}."),
    min: $.validator.format("Please enter a value greater than or equal to {0}.")
},

四、如果要修改的話這樣就可以了;

jQuery.extend(jQuery.validator.messages, {
  required: "必選字段",
  remote: "請修正該字段",
  email: "請輸入正確格式的電子郵件",
  url: "請輸入合法的網址",
  date: "請輸入合法的日期",
  dateISO: "請輸入合法的日期 (ISO).",
  number: "請輸入合法的數字",
  digits: "只能輸入整數",
  creditcard: "請輸入合法的信用卡號",
  equalTo: "請再次輸入相同的值",
  accept: "請輸入擁有合法後綴名的字符串",
  maxlength: jQuery.validator.format("請輸入一個 長度最多是 {0} 的字符串"),
  minlength: jQuery.validator.format("請輸入一個 長度最少是 {0} 的字符串"),
  rangelength: jQuery.validator.format("請輸入 一個長度介於 {0} 和 {1} 之間的字符串"),
  range: jQuery.validator.format("請輸入一個介於 {0} 和 {1} 之間的值"),
  max: jQuery.validator.format("請輸入一個最大爲{0} 的值"),
  min: jQuery.validator.format("請輸入一個最小爲{0} 的值")
});

五、下面是自己寫的一個demo,裏面註釋寫的很詳細,應該沒什麼問題的

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=11">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title id="title">jquery validata js_表單驗證</title>
    <link type="text/css" rel="stylesheet" href="css/base.css">
    <style type="text/css">
        #login{width:500px;margin:100px auto;}
        #login li{padding: 5px 10px;}
        .sub{margin-left: 60px;cursor: pointer;}
        form .highlight{ background-color:#e2e2e2;}
        /*定義錯誤信息輸出的樣式*/
        .error{font-size: 10px;color: #cd0a0a;}
    </style>
</head>
<body>
<form method="post" action="" id="login">
    <ul>
        <li>
            <label>賬戶</label>
            <input type="text" name="email" class="required email" id="email"/>
            <br/>
        </li>
        <li>
            <label>密碼</label>
            <input type="password" name="password" id="pass" class="required" minlength="6" maxlength="10"/>
        </li>
        <li>
            <input type="submit" name="sub" class="sub" id="sub"/>
        </li>
    </ul>
    <div id="messageBox"></div>
</form>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script>
    $(function() {
        // highlight
        var elements = $("input[type!='submit'], textarea, select");
        elements.focus(function() {              //只是跟li添加樣式,跟validata無關
            $(this).parents('li').addClass('highlight');
        });
        elements.blur(function() {
            $(this).parents('li').removeClass('highlight');
        });
        $("#login").validate({
            debug:true,                            //debug:進行調試模式(表單不提交):
            ignore:".ignore",                       //對某些元素不進行驗證
            errorLabelContainer: "#messageBox",       //顯示錯誤信息的容器ID
            wrapper: "li",                            //包含每個錯誤信息的容器
            onsubmit:true,                         //是否提交時驗證
            onfocusout:true,                        //是否在獲取焦點時驗證
            onclick:true,                          //是否在鼠標點擊時驗證(一般驗證checkbox,radiobox)
            errorElement:"em",                          //errorElement String 默認:"label" 使用什麼標籤標記錯誤
//            errorClass:"invalid",                       //指定錯誤提示的css類名,可以自定義錯誤提示的樣式  默認:"error"
//            highlight:'error',                            //可以給未通過驗證的元素加效果,閃爍等
            success:"valid",
            submitHandler:function() { alert("Submitted!") },//要驗證的元素通過驗證後的動作,如果跟一個字符串,會當做一個css類,也可跟一個函數
            submitHandler:function(form) {
                $(form).ajaxSubmit();
            },
            rules: {                                        //自定義規則,key:value的形式,key是要驗證的元素,value可以是字符串或對象
                    email: {                                //rule裏面的東西可以寫在input標籤裏面,也可以寫在這裏;
                        required:true,
                        email: true,
                        minlength:6
                    },
                    password: {
                        required: true
                    }
                 },
                messages: {                                 //自定義的提示信息key:value的形式key是要驗證的元素,值是字符串或函數
                    email: {                             
                        required:"用戶名爲必填",
                        minlength:"用戶名至少6個字符",
                        email:"請填寫正確有效的郵箱"
                    },
                    password:{
                        required:"密碼不能爲空",
                        minlength:"密碼不能小於6位數"
                    }
                }
            })
        })
</script>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章