Solved

help me to correct the us zip code validation using javascript

Posted on 2007-04-03
11
1,010 Views
Last Modified: 2010-05-19
Hi experts,

i want to format the given value to us zip code.For this i write this function

function formatZipOrPostalCode(source,textValue)
            {
            var textValueId = source;                  
            var codeValue = textValue.Value.replace("-","");                  
            var formatedCodeValue = codeValue.replace(" ","");                  
            if(formatedCodeValue.length == 5)
                  {      
                  var searchPattern = /^(?(^00000(|-0000))|(\d{5}(|-\d{4})))$ /;
                  if(formatedCodeValue.search(searchPattern) ==-1)
                  {      
                  textValue.IsValid = false;      
                  }
                  else
                  {                              
                  document.getElementById(textValueId).value = formatedCodeValue;
                  textValue.IsValid = true;
                  }
                  return;
            }
}

Whe i run this script o got the error invalid Quantifier ?.How can i solve this

My aim is to not allow the entry 00000 and 00000-0000    only allow 11111    12345-1254 like values.Not fully zeroes

Thanks
0
Comment
Question by:Rajesh_mj
  • 5
  • 5
11 Comments
 
LVL 6

Author Comment

by:Rajesh_mj
Comment Utility
Hi,

i have a mistake in the above search patern  


The correct searchPattern that i cuurently gave is

searchPattern = /^(?(^00000)|(\d{5}))$ /;
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
function isValidZipCode(value) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}

^\d{5} tells you that the regular expression must start ("^") with exactly five ("{5}") digits ("\d").
([\-]\d{4})?$ tells you that the grouping can appear either zero or one time ("?") and must be the end of the string ("$"). Inside this grouping is where the literal dash character ("-") must be, but since that character normally means other things in regular expressions, it needs to be "escaped" with the slash in front). After the dash is exactly four ("{4}") digits ("\d").

So, a value of "12345" will succeed. Any letter will cause the expression to fail. A value of "12345-6789" will succeed. If the 9 digit format is used, then the previous format (5 digits, dash, 4 digits) must be used. Other formats will fail.

Hope this helps,

Regards,
Max.
0
 
LVL 6

Author Comment

by:Rajesh_mj
Comment Utility
Hi,

Thanks for the reply .But if we use the above one it will accept 00000 and 00000-0000

It will not occur thats my requirement......

Thanks
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
How about this then:

function isValidZipCode(value) {
   var re = /^[1-9]{5}([\-][1-9]{4})?$/;
   return (re.test(value));
}

Regards,
Max.
0
 
LVL 6

Author Comment

by:Rajesh_mj
Comment Utility

Hi Max.,

if we use the above i was not able to enter this type os zip code.
eg: 10111-0021.I want to enter these type of zip codes.

and not allowed only these 2 cases
1) 00000
2)00000-00000 all others are allowed


Many Thanks
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:sankar_ravi
Comment Utility
Hi,

This is for sure works. Please see the solution..
var re = /^[1-9]+\d{4}/;
return (re.test(formatedCodeValue));

Thanks.
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
sankar,

The specification has changed. What I gave works for any zip code like 11111-1111 to 99999-9999 however it won't work for 99999-9999.X

Rajesh, I am not a native american so I can't guess what possible combinations need to work, so if you don't tell me that something like 10111-0021.I needs to work as well, it's going to be very hard for me to help you properly ;)

Regards,
Max.
0
 
LVL 6

Author Comment

by:Rajesh_mj
Comment Utility
Hi Max,

The Exact requirement of mine is that
The zip code can allow to enter the values
1)only digits
2)it can be the combination of 5 digits or 5digits-4 digits
for eg: 11111-1111 or 11111
3)not allow only this type of values 00000, 00000-0000
4)all other combination of 0 with other digits are allowed

Many Thanks
Rajesh
0
 
LVL 19

Accepted Solution

by:
DreamMaster earned 400 total points
Comment Utility
function isValidZipCode(value) {
   if (value != '00000' && value != '00000-0000')
   {
      var re = /^[1-9]{5}([\-][1-9]{4})?$/;
      return (re.test(value));
   }
   else
      return false;
}

Something like that perhaps? It's undoable to check this using a regular expression and why would you go through the trouble of creating an immensly complex regex, when you can test for these two combinations easily and only allow the check when these values have not been met.

Regards,
Max.
0
 
LVL 6

Author Comment

by:Rajesh_mj
Comment Utility
Hi Max,

Thanks for your suggestion to rethink.It will solve all the problems and work fine

Many Thanks
Rajesh
0
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
Glad to have been helpful :)

Regards,
Max.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now