finding out if a string contains characters

M

Merlin

Hi there,

I would like to check if a string is a valid zip code via Javascript.
Length and existents are already checked.

How can I find out if the string contains characters other than numbers?

Example: 834F7
schould be false since it countains an F character

Thank you for any help,

merlin
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 28 Dec
2004 22:56:04, seen in Merlin
I would like to check if a string is a valid zip code via Javascript.

Better to say "postal code" for the general case, since Murricans will
tend to interpret "zip code" as "USPS Zip Code".
Length and existents are already checked.

How can I find out if the string contains characters other than numbers?

Bad = /\D/.test(code) // fails on first non-digit
OK = /^\d*$/.test(code) // tests all characters digit
OK = /^\d+$/.test(code) // tests all characters digit, not empty
OK = /^\d{7}$/.test(code) // tests for 7 digits exactly

There is probably no need to test length independently, nor to test for
existence (an empty string exists).

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
E

Evertjan.

ZeroCoded wrote on 30 okt 2005 in comp.lang.javascript:
Hope this example would help

public class Exemple {

public static void main ( String[] args ) {

int x;
double number;
String s ;

Is not Javascript. OT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top