JQuery Validation is not working

2019-07-13 18:57发布

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="Fname" 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.

4条回答
混吃等死
2楼-- · 2019-07-13 19:19

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

查看更多
等我变得足够好
3楼-- · 2019-07-13 19:23

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."); ?>',
}
查看更多
别忘想泡老子
4楼-- · 2019-07-13 19:37

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

查看更多
太酷不给撩
5楼-- · 2019-07-13 19:42

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

 });
查看更多
登录 后发表回答