473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

finding out if a string contains characters

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
4 36715
"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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
3
by: peterbe | last post by:
In a text that contains references to numbers like this: #583 I want to find them with a regular expression but I'm having problems with the hash. Hopefully this code explains where I'm stuck: ...
2
by: Paweł | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
5
by: Terry Olsen | last post by:
Is there a good way to find a pattern of bytes/chars in a stream? I've got a serial port connected to a tcp port. I need to be able to catch a unique character string in the stream so that I can...
26
by: alberto | last post by:
Hi. Im newbie in C language. I have a binary file with many character arrays of 50 character defined as char array But in some cases, many of these 50 characters are not being used. I would...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.