Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

jquery validation rules

  1. My phone validation depends on a checkbox (no, don't contact me via phone). If this is checked, then I will not need run the phone validation. I googled around and found 'depends' function.

I have

  $("#myForm").validate({
  ....
  rules: {
  phone1: {
    required: {
      depends: "!#pri_noPhone:checked"
    },
    number: true,
    minlength:3,
    }

It doesn't throw an error, but it still tries to validate the phone number.

  1. Under the rules: how do i make sure that email and confirmEmail are the same? I have rules, and messages separate.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try something like this:

"#phone1": {
   number: true,
   minlength:3,
   required: function(element){
      return ($('#pri_noPhone_wrapper input:checked').val() == 'True');
   }
}

The HTML (after looking at this I forgot to add the wrapper HTML)

<span id='pri_noPhone_wrapper'>
Phone:
<input type="checkbox" name="pri_noPhone" value="what ever" />
</span>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...