Jump to content

JQuery Validation For Zip Code


sridaran
 Share

Recommended Posts

if you just want to check the length, assuming your input field id is zipcode

function ValidateInput(){   var error = '';   if($('#zipcode).val().length < 6)   {	  error += 'zip code must have 6 digits';   }   /*Do here other validations*/   if(error.length >= 1)   {	  alert(error);	  return false;   }   else   {	  return true;   }}

if you also want to validate the format use regular expression, i'am not familiar with U.S. zip code if thats what you're looking for.also if you really want to use only jquery, try checking this jquery validation plugin and the method specified in that website.

Link to comment
Share on other sites

Thank you very much. Is there a way to allow only numbers along with this validation? i.e Only numbers are allowed with six digits only.

if you just want to check the length, assuming your input field id is zipcode
function ValidateInput(){   var error = '';   if($('#zipcode).val().length < 6)   {	  error += 'zip code must have 6 digits';   }   /*Do here other validations*/   if(error.length >= 1)   {	  alert(error);	  return false;   }   else   {	  return true;   }}

if you also want to validate the format use regular expression, i'am not familiar with U.S. zip code if thats what you're looking for.also if you really want to use only jquery, try checking this jquery validation plugin and the method specified in that website.

Link to comment
Share on other sites

yes, as he recommended, you would want to use regex (regular expressions).http://www.w3schools.com/js/js_obj_regexp.aspAlthough with a little Googling, you might find that jQuery might also have support for that. Don't know off the top of my head.

Link to comment
Share on other sites

yep, use regex to to validate that is numeric.but i would prefer to do do something to prevent the user from even trying to insert anything else, doing something like:

$(document).ready(   function()   {	  $("#zipcode").keydown(function(event) {	// Allow backspace and delete	if ( event.keyCode == 46 || event.keyCode == 8 ) {	   	}	else {	   // Ensure that it is a number	   if (event.keyCode < 48 || event.keyCode > 57 ) {		event.preventDefault();		   }		}   });});

Link to comment
Share on other sites

there is that school of thought, but it might confuse users if they submit input and then nothing shows up, as opposed to telling them as they submit that their input is invalid. For instance, when the input loses focus, you could validate it and show a warning message on the screen telling them that, for example, only numbers are allowed for this field.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...