help me to correct the us zip code validation using javascript

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
LVL 6
Rajesh_mjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rajesh_mjAuthor Commented:
Hi,

i have a mistake in the above search patern  


The correct searchPattern that i cuurently gave is

searchPattern = /^(?(^00000)|(\d{5}))$ /;
0
DreamMasterCommented:
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
Rajesh_mjAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DreamMasterCommented:
How about this then:

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

Regards,
Max.
0
Rajesh_mjAuthor Commented:

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
sankar_raviCommented:
Hi,

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

Thanks.
0
DreamMasterCommented:
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
Rajesh_mjAuthor Commented:
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
DreamMasterCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rajesh_mjAuthor Commented:
Hi Max,

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

Many Thanks
Rajesh
0
DreamMasterCommented:
Glad to have been helpful :)

Regards,
Max.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.