Closed Thread Icon

Preserved Topic: another JS RegExp problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18541" title="Pages that link to Preserved Topic: another JS RegExp problem (Page 1 of 1)" rel="nofollow" >Preserved Topic: another JS RegExp problem <span class="small">(Page 1 of 1)</span>\

 
cira
Obsessive-Compulsive (I) Inmate

From: Phoenix, Az, USA
Insane since: Apr 2001

posted posted 06-27-2002 22:32

Hi everyone. I'm trying to test to see if a user has entered a correct zip code in a form. I'm getting wierd errors and I don't know why.

The code:

var pattern = /\d{5}/;
if (pattern.test(element.value)) {
alert(element.value);
return true;
}
else { return false; }

What is happening is that it matches 5 or more digits, not exactly 5. I have tried using the pattern /\d\d\d\d\d/ but the same thing happens.
I have tried changing my language declaration in the script tag to include the version number, but that didn't seem to help either.
The alert was put in to double check the element's value, and it returns whatever I entered in the field, no mistakes there.
I know I can test for string length to fix this probelm, but it seems like I shouldn't have to do so.

Am I missing something obvious? Or is this some kind of bug. BTW, I'm testing this using IE 5.5.

Thanks in advance,
Cira

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-27-2002 22:55

The problem is that the following string:

"123456789"

*does* contain five digits in a row. For example, it contains "12345" which matches the pattern, and it contains "34567" which matches the pattern.

You need to set your pattern so that it requires the five digits to start at the beginning of the string and end at the end of the string. The pattern

/^\d{5}$/

Should work, if I remember correctly. ^ matches the beginning of the string, and $ matches the end.

cira
Obsessive-Compulsive (I) Inmate

From: Phoenix, Az, USA
Insane since: Apr 2001

posted posted 06-27-2002 23:21

wonderful, thank you for your help. I assumed I was missing something as I am new to regular expression.

-Cira

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-28-2002 05:15

um... I'm not gonna knock you for using regexp's (and learning them it's a great skill, kinda like SQL that you use with virtually all languages).

but in a simple case like this just testing the length

if (element.length.value == 5)

is fine.
if it were an email address you were testing for however



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-28-2002 05:25

Well, a reg exp is also able to make sure that they're all digits simultaneously.

hlaford
Bipolar (III) Inmate

From: USA! USA! USA!
Insane since: Oct 2001

posted posted 06-28-2002 15:41

regexes are the future of all computing! okay, maybe not, but for field validation (i.e. string parsing), there's nothing better. how long would the code be to test for an extended ZIP code? well, my regex is simple:

/^\d{5}(-?\d{4})?$/

once you learn regexes, you'll be hard-pressed to try thinking about these kinds of problems in another light. IMHO, regexes are what make Perl so strong.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 06-28-2002 16:12

Of course, even those RegExp's won't keep me from entering "00000" as my zip code, which is what I always do =)

Keep in mind that many contries don't have zip codes, so don't require them if you want your page to work everywhere.

hlaford
Bipolar (III) Inmate

From: USA! USA! USA!
Insane since: Oct 2001

posted posted 07-01-2002 15:42

What you are proving by entering 00000 is that there is no substitute for server-side validation. Too often I have come across applications in a production environment that rely solely on client-side validation.

« BackwardsOnwards »

Show Forum Drop Down Menu