By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,162 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,162 IT Pros & Developers. It's quick & easy.

finding out if a string contains characters

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Merlin" <ne*********@gmx.de> wrote in message
news:33*************@individual.net...
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


http://www.rgagnon.com/jsdetails/js-0063.html

function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
States zip code in 5 digit format or zip+4
format. 99999 or 99999-9999

PARAMETERS:
strValue - String to be tested for validity

RETURNS:
True if valid, otherwise false.

*************************************************/
var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

//check for valid US Zipcode
return objRegExp.test(strValue);
}
Jul 23 '05 #2

P: n/a
JRS: In article <33*************@individual.net>, dated Tue, 28 Dec
2004 22:56:04, seen in news:comp.lang.javascript, Merlin
<ne*********@gmx.de> posted :

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>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

P: n/a

Hope this example would help

public class Exemple {

public static void main ( String[] args ) {

int x;
double number;
String s ;

// Bloc 1

System.out.println ( "Start bloc 1" );
System.out.print ( " Pls enter a number from 1 - 10: " );
// Clavier.lireInt() is a local class not found in java lib, u
can replace by ur own
x = Clavier.lireInt();
System.out.println ( "End bloc 1" );

try {

// Bloc 2

System.out.println ( "Start bloc 2" );
System.out.print ( " Entrez UPC SVP: " );
s = Clavier.lireString(); // local class pls replace by ur
own or use BufferReader
number = Double.parseDouble(s);
System.out.println ( "End bloc 2" );

} catch ( NumberFormatException e ) {

// Bloc 3
// ArithmeticException NumberFormatException
System.out.println ( "Start bloc 3" );
System.out.println ( " string, " + e.getMessage() );
System.out.println ( "End bloc 3" );

} finally {

// Bloc 4

System.out.println ( "Start bloc 4" );
System.out.println ( " anything goes" );
System.out.println ( "End bloc 4" );

}

// Bloc 5

System.out.println ( "Start bloc 5" );
System.out.println ( " Normal Program end" );
System.out.println ( "End bloc 5" );

} // main
} // Exemple
ExceptionS:
IOException
FileNotFoundException (ex : could be from ileReader)
RuntimeException
ArithmeticException (ex : division by 0)
IllegalArgumentException
NumberFormatException
(ex : could be
generated from integer.parseInt(...))
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
NegativeArraySizeException
NullPointerException
tks :)
--
ZeroCoded
------------------------------------------------------------------------
ZeroCoded's Profile: http://www.highdots.com/forums/m1266
View this thread: http://www.highdots.com/forums/t574789

Oct 30 '05 #4

P: n/a
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.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 30 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.