1. 程式人生 > >jqeury validate(2): 將校驗規則寫到js程式碼中

jqeury validate(2): 將校驗規則寫到js程式碼中

required:true 必須有值
required:"#aa:checked"
表示式的值為真,則需要驗證
required:function(){}
返回為真,表時需要驗證後邊兩種常用於,表單中需要同時填或不填的元素


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>將校驗規則寫到控制元件中</title>
    <script src="jqValidate/jquery.min.js" type="text/javascript"></script>
    <script src="jqValidate/jquery.metadata.js" type="text/javascript"></script>
    <script src="jqValidate/jquery.validate.js" type="text/javascript"></script>
    <script src="jqValidate/jquery.validate.messages_cn.js" type="text/javascript"></script>
    <script type="text/javascript">
        //1. messages處,如果某個控制元件沒有message,將呼叫預設的資訊
        $(function () {
            $("#signupForm").validate({
                rules: {
                    firstname: "required",
                    email: {
                        required: true,
                        email: true
                    },
                    password: {
                        required: true,
                        minlength: 5
                    },
                    confirm_password: {
                        required: true,
                        minlength: 5,
                        equalTo: "#password"
                    }
                },
                messages: {
                    firstname: "請輸入姓名",
                    email: {
                        required: "請輸入Email地址",
                        email: "請輸入正確的email地址"
                    },
                    password: {
                        required: "請輸入密碼",
                        minlength: jQuery.format("密碼不能小於{0}個字 符")
                    },
                    confirm_password: {
                        required: "請輸入確認密碼",
                        minlength: "確認密碼不能小於5個字元",
                        equalTo: "兩次輸入密碼不一致不一致"
                    }
                }
            });
        });
        
    </script>
</head>
<body>
    <form id="signupForm" method="get" action="">
    <p>
        <label for="firstname">
            Firstname</label>
        <input id="firstname" name="firstname" />
    </p>
    <p>
        <label for="email">
            E-Mail</label>
        <input id="email" name="email" />
    </p>
    <p>
        <label for="password">
            Password</label>
        <input id="password" name="password" type="password" />
    </p>
    <p>
        <label for="confirm_password">
            確認密碼</label>
        <input id="confirm_password" name="confirm_password" type="password" />
    </p>
    <p>
        <input class="submit" type="submit" value="Submit" />
    </p>
    </form>
</body>
</html>