Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-27-2012, 03:43 PM   PM User | #1
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 30
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
Function to check if a string contains numbers

Hey everyone,

Is there a Javascript function to check if a string contains any numbers?

Also, is there one to check if a string contains special characters or symbols?

Thanks!

Last edited by hunter1611; 12-27-2012 at 05:22 PM..
hunter1611 is offline   Reply With Quote
Old 12-27-2012, 03:59 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
RegEx (Regular Expression) test:

Code:
var regexNum = /\d/g;// Global check for numbers
var a = "This contains no numbers", b="This contains 1 number";
var c = regexNum.test(a), d = regexNum.test(b);
alert(c + " and " + d);// will alert false and true
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
hunter1611 (12-27-2012)
Old 12-27-2012, 04:37 PM   PM User | #3
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 30
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
Ok so, slightly confused. How do I plug a variable into that? Is regexNum the variable you're checking for numbers?

(Sorry, I'm really not a Javascript person..I fumble through it as needed).
hunter1611 is offline   Reply With Quote
Old 12-27-2012, 04:41 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by hunter1611 View Post
Ok so, slightly confused. How do I plug a variable into that? Is regexNum the variable you're checking for numbers?

(Sorry, I'm really not a Javascript person..I fumble through it as needed).

The variables a and b in Wolfshade's example contain the strings to be analysed.

To answer your second question

var str = "This string contains 2 special characters ! and ?"
alert (/[^a-z0-9\s\_]/gi.test(str)); // i.e. anything other than letters, numbers, space, underscore
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
hunter1611 (12-27-2012)
Old 12-27-2012, 05:02 PM   PM User | #5
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 30
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
Heh, whoops, ignore this post...something broke in my script...

Last edited by hunter1611; 12-27-2012 at 05:09 PM..
hunter1611 is offline   Reply With Quote
Old 12-27-2012, 05:21 PM   PM User | #6
hunter1611
New Coder

 
Join Date: Jan 2011
Posts: 30
Thanks: 11
Thanked 0 Times in 0 Posts
hunter1611 is an unknown quantity at this point
OK! Got it working! Thank you!
hunter1611 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 AM.


Advertisement
Log in to turn off these ads.