New course! Every coder should learn Generative AI!
Try a free lesson- 1
JavaScript regex
Please help, What would I have to add to this code below to allow the option for zip codes with 6 digits? function isUSAZipCode(str) { return /^\d{5}(-\d{4})?$/.test(str); }
12 Answers
+ 2
return /^\d{5,6}(-\d{4})?$/.test(str); ?
+ 2
/(^\d{5}$)|(^\d{5}-\d{4}$)/
+ 2
/^\d{6}(-\d{4})?$/
Simple replace 5 with 6 now it will allow 6 digit
ex :
231301-2123 //pass
618464 //Pass
if u want both
u can use 5 || 6 or 5,6
/^\d{5||6}(-\d{4})?$/
/^\d{5,6}(-\d{4})?$/
+ 2
It's one of the concept taught in Sololearn itself. You can use {A,B} to specify the repeation range of a sub regex. I would like to memorize it as {N} is derived from the syntax.
https://www.sololearn.com/learn/9639/?ref=app
+ 1
https://regexr.com
This might help u
+ 1
Thanks once again Beauty and CarrieForle
You both are my icons
With people like you both on the SoloLearn community, I think you guys are making SoloLearn a much faster option for getting fast and reliable answers than stackoverflow
0
Beauty your Code doesn't work
0
Beauty your code still doesn't allow the option for 6 digits, only 5
0
Beauty, please checkout this website, you'll see that the code is still not working
https://www.javascript-coder.com/form-validation/javascript-zip-code-validation/
0
You got it CarrieForle
Can you please explain to me your code or send me an article or a link that you used to learn regex?
0
Beauty thanks