Rajesh_mj
asked on
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(sour ce,textVal ue)
{
var textValueId = source;
var codeValue = textValue.Value.replace("- ","");
var formatedCodeValue = codeValue.replace(" ","");
if(formatedCodeValue.lengt h == 5)
{
var searchPattern = /^(?(^00000(|-0000))|(\d{5 }(|-\d{4}) ))$ /;
if(formatedCodeValue.searc h(searchPa ttern) ==-1)
{
textValue.IsValid = false;
}
else
{
document.getElementById(te xtValueId) .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
i want to format the given value to us zip code.For this i write this function
function formatZipOrPostalCode(sour
{
var textValueId = source;
var codeValue = textValue.Value.replace("-
var formatedCodeValue = codeValue.replace(" ","");
if(formatedCodeValue.lengt
{
var searchPattern = /^(?(^00000(|-0000))|(\d{5
if(formatedCodeValue.searc
{
textValue.IsValid = false;
}
else
{
document.getElementById(te
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
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.
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.
ASKER
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
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
How about this then:
function isValidZipCode(value) {
var re = /^[1-9]{5}([\-][1-9]{4})?$ /;
return (re.test(value));
}
Regards,
Max.
function isValidZipCode(value) {
var re = /^[1-9]{5}([\-][1-9]{4})?$
return (re.test(value));
}
Regards,
Max.
ASKER
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
Hi,
This is for sure works. Please see the solution..
var re = /^[1-9]+\d{4}/;
return (re.test(formatedCodeValue ));
Thanks.
This is for sure works. Please see the solution..
var re = /^[1-9]+\d{4}/;
return (re.test(formatedCodeValue
Thanks.
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.
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Max,
Thanks for your suggestion to rethink.It will solve all the problems and work fine
Many Thanks
Rajesh
Thanks for your suggestion to rethink.It will solve all the problems and work fine
Many Thanks
Rajesh
Glad to have been helpful :)
Regards,
Max.
Regards,
Max.
ASKER
i have a mistake in the above search patern
The correct searchPattern that i cuurently gave is
searchPattern = /^(?(^00000)|(\d{5}))$ /;