具有默认值的字段的jquery验证

时间:2014-11-19 12:29:16

标签: jquery html jquery-validate

http://jqueryvalidation.org/files/demo/

我使用上面的插件进行注册表单的jquery验证

它工作正常,但在我的表单中有一个字段名称"域"其默认值已设置为" www。"所以用户必须只写其他部分域。

所以实际上它在任何时候都不是空白的,所以它不会验证用户是否将该文件保留为默认值,这里是我的jquery和html代码

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

        $("#frm").validate({
            rules: {
                domain: "required",
                uname: "required",
                password:'required',
                cpassword:'required',
                fname:'required',
                lname:'required',
                bussname:'required',
                city:'required',
                state:'required',
                pincode:'required',
                country:'required',
                phone1:'required',
                email: {
                    required: true,
                    email: true
                },
                code:'required',


            },
            messages: {
                domain: "Please enter domain name",
                uname: "Please enter user name",
                password: "Please enter password",
                cpassword:'Please confirm password',
                fname:'Please enter first name',
                lname:'Please enter last name',
                bussname:'Please enter practice',
                city:'Please enter city',
                state:'Please enter state',
                pincode:'Please enter zip code',
                country:'Please select country',
                phone1:'Please enter phone',
                email: 'Please enter email address',
                code: 'Please enter security code',


            }
        });


    });
</script>

我的HTML表单

    <form name="frm" id="frm" action="ezregistration2.php" method="post">


<input name="domain" type="text" id="domain"  value="www."  size="40" class="tb7"   />



<input name="uname" type="text" id="uname" value="" size="40" class="tb7" onkeyup="countchars(this);" />


<input name="password" type="password" id="password"  size="40" class="tb7">

<input name="cpassword" type="password" id="cpassword"  size="40" class="tb7">


<input name="fname" type="text" id="fname" value="" size="40" class="tb7" >


<input name="lname" type="text" id="lname" value="" size="40" class="tb7" >


<input name="bussname" type="text" id="bussname" value="" size="40" class="tb7">


<input name="address1" type="text" id="address1" value="" size="40" class="tb7">




<input name="address2" type="text" id="address2" value="" size="40" class="tb7">


<input name="city" type="text" id="city" value="" size="40" class="tb7" >
                                                    <input name="state" type="text" id="state" value="" size="40" class="tb7" >


<input name="pincode" type="text" id="pincode" value="" size="40" class="tb7" >

<select name="country" id="country" class="tb7" style="width:225px;"> 
<option value="Afghanistan">Afghanistan</option></select>

<input type="text" name="phone1" size="40" id="phone1" value="" onkeypress="return noNumbers(event)" class="tb7">

<input name="email" type="text" id="email" value="" size="40" class="tb7">

<input type="text" name="code" id="code" size="8" class="tb7" style="width:100px" />

<input name="Submit" type="image" src="homeimages/submit.jpg" class="submitbtn" style="outline:none;" />


<input type="hidden" name="sub" value="1">
</form>
无论如何,

在验证时是否也不允许默认值?

1 个答案:

答案 0 :(得分:0)

$("#frm").validate({之前添加

jQuery.validator.addMethod("domain", function(value, element){ return this.optional(element) || value != "www."; }, "Please enter domain name");

在您的规则中,将domain: "required",更改为

domain: {
    required: true,
    domain: true
},