Jump to content
sridaran

JQuery Validation For Zip Code

Recommended Posts

How to write validation in jquery for zip code(pin code) i.e only 6 digit numbers. Also I need jquery validation for phone number. Can anyone help me please?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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();		   }		}   });});

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...