?
Solved

Need help using jQuery validation rules.

Posted on 2011-10-19
2
Medium Priority
?
361 Views
Last Modified: 2012-05-12
Need help using jQuery validation rules. I have a input field that needs specific rules. See below. I'm not sure if you can use range with multiple ranges.

Any help would be great thanks.

http://docs.jquery.com/Plugins/Validation




rules: {
      zip: {required: true, digits: true, maxlength: 9, minlength: 5},
}


1) Zip should accept either 5 or 9 digits.
2) Should not accept 6 digits or 7 digits or 8 digits.
0
Comment
Question by:yando18
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36997074
check this : http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage

test page : http://jsfiddle.net/cMyzS/

jQuery.validator.addMethod("zip5or9", function(value, element) {
  return this.optional(element) || value.length == 5 || value.length == 9;
}, "Please specify a correct zip");


rules: {
      zip: { required: true, zip5or9: true },
}

Open in new window


0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36997922
leakim971's code is perfect just to show the correct message to end user what they need to enter

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script>
$(document).ready(function(){
  
  $.validator.addMethod("zip5or9", function(value, element) { 
    return this.optional(element) || value.length == 5 || value.length == 9 ; 
}, "Please specify a correct zip");
  
  $("#testForm").validate({
    rules: {
      zip: {
        required:true,
        digits:true,
        maxlength: 9,
        minlength:5,
        zip5or9: true
      }
      
    },
    messages:{
      zip: {
        required: "Please enter zip code",
        digits: "Enter digit only",
        maxlength: "9 digit max",
        minlength: "5 digit minimum",
        zip5or9: "Zip should be either 5 Or 9 digit only"
        }
    }
  });
});
</script>

</head>

<body>
<form method="POST" id="testForm" >
  <input name="zip" type="text">
  <input type="submit">
</form>
</body>

</html>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question