Solved

help me to correct the us zip code validation using javascript

Posted on 2007-04-03
11
1,019 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
ID: 18842220
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
ID: 18842261
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
ID: 18842360
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 19

Expert Comment

by:DreamMaster
ID: 18842511
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
ID: 18842592

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
 
LVL 6

Expert Comment

by:sankar_ravi
ID: 18842704
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
ID: 18843008
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
ID: 18848493
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
ID: 18849220
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
ID: 18849464
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
ID: 18849478
Glad to have been helpful :)

Regards,
Max.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

815 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

13 Experts available now in Live!

Get 1:1 Help Now