开发者

JQuery Validation is not working

I have one form in which one input type whose value is "First Name". But this can be changed on onfocus function I want validation for this input field if it is blank or "First name"

I have two jQuery files jquery-1.4.2.min.js & jquery.validate.pack.js.

I have another jQuery file for this form:

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);
});

In this file required:true is working if value is blank but by default value is "First Name" so it does not work I want both if it is blank or it is "First Name".

<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="Fna开发者_如何学编程me" value="First Name" onfocus="if(this.value=='First Name')this.value='';" onblur="if(this.value=='')this.value='First Name';" />
            </div>
        </li>
    </ul>
</form>

Please reply as early as possible.

Thank you.


You can use required rule with Callback and check for equality with First Name before returning


You should create custom validation rules for such cases as described here: jquery.validation - how to ignore default values when validating mandatory fields


this is my code it's working properly: try this

$().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"
       },
   }

 });


Did u try the adding custom validation ]

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;
    }, ""
);

Pls note i have added example

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜