JQuery验证无效

时间:2011-05-20 10:14:17

标签: php javascript

我有一个表单,其中一个输入类型的值为“First Name”。但是这可以在onfocus函数上更改我想要验证此输入字段,如果它是空白或“名字”

我有两个jQuery文件jquery-1.4.2.min.js& jquery.validate.pack.js

我有这个表单的另一个jQuery文件:

jQuery(document).ready(function() {
    jQuery("#frmRegister").validate({
        errorElement:'div',
        rules: {
            Fname:{
                required:true,
                minlength: 2,
                maxlength:30            
            }
        },
        messages: {
            Fname:{
                required: "Please enter first name",
                minlength: "Required minimum 2 characters allowed",
                maxlength: "Required maximum 30 characters allowed"
            }
    });
    jQuery("#msg").fadeOut(5000);
});

在此文件required:true正在工作,如果值为空但默认值为“名字”,因此它不起作用我想要两者如果它是空白或它是“名字”。

<form name="frmRegister" id="frmRegister" method="post">
    <ul class="reset ovfl-hidden join">
        <li class="fall">
            <label for="Fname">Full Name:</label>
            <div class="fl">
                <input type="text" class="form" id="Fname" name="Fname" value="First Name" onfocus="if(this.value=='First Name')this.value='';" onblur="if(this.value=='')this.value='First Name';" />
            </div>
        </li>
    </ul>
</form>

请尽早回复。

谢谢。

4 个答案:

答案 0 :(得分:0)

您可以使用required rule with Callback并在返回

之前检查与名字的相等性

答案 1 :(得分:0)

您应该为此处描述的此类案例创建自定义验证规则:jquery.validation - how to ignore default values when validating mandatory fields

答案 2 :(得分:0)

这是我的代码,它正常工作:试试这个

$().ready(function() {
$("#form2").validate();
// validate signup form on keyup and submit
 $("#form1").validate({
  rules: {
 userid: "required",
 username: {
  required: true,
  minlength: 2
},
 password: {
    required: true,
    minlength: 5
},
authority: {
      required: true
   },
 emailid: {
  required: true,
  email: true
   }
},
    messages: {
     userid: "Please enter your user Id",
     username: {
      required: "Please enter a username",
      minlength: "Your username must consist of at least 2 characters"
       },
     password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
        },
      emailid: "Please enter a valid email address",
      authority:"Please select Authority"
       },
   }

 });

答案 3 :(得分:0)

您是否尝试添加自定义验证]

jQuery.validator.addMethod("workPublicserviceDate",
    function(value, element) {

    var hint = '<?php echo $dateHint; ?>';
    var format = '<?php echo $format; ?>';
    var pubService = strToDate($('#txtAPS').val(), format)
    var dateAssume = strToDate($('#txtAssumeDate').val(), format);

    if (pubService && dateAssume && (pubService > dateAssume)) {
        return false;
    }
        return true;
    }, ""
);

请注意我添加了示例

$("#frmEmpJobDetails").validate({
                   rules: {
txtAPS: { workPublicserviceDate: function(){ return ['<?php echo $dateHint; ?>','<?php echo $format; ?>']}, required:true  },
}
 messages: {
txtAPS : { workPublicserviceDate: '<?php echo __("Invalid date."); ?>',
}