473,568 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check if string contains any numbers

i need a check that can go through a string to see if it contains any
numbers.

Please help!

Jan 7 '06 #1
12 78706
Mokka wrote:
i need a check that can go through a string to see if it contains any
numbers.


function hasNumbers(t)
{
return /\d/.test(t);
}
Returns true if t contains numbers, otherwise false.
--
Rob
Jan 7 '06 #2
RobG wrote:
Mokka wrote:
i need a check that can go through a string to see if it contains any
numbers.

function hasNumbers(t)
{
return /\d/.test(t);
}
Returns true if t contains numbers, otherwise false.


That should probably be returns true if t contains at least one
number, otherwise false.

So the function should probably be called hasNumber() or hasDigit().

--
Rob
Jan 7 '06 #3


Say, myVar is your object to be checked, if (/\d/.test(myVar)) .....
Danny
Jan 8 '06 #4
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

Jan 9 '06 #5
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

Jan 9 '06 #6
// t is your string
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

:) try ~_~

Jan 9 '06 #7
epbon said the following on 1/8/2006 6:29 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.c om, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

And please do not post the same article three times. Give Google time to
show your article.
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~


And then try:

function hasNumber(t){re turn /\d/.test(t);}

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Jan 9 '06 #8
If you do this a lot stretch the String.prototyp e:

String.prototyp e.gotNum=functi on(){
return /\d/.test(this)
}
otherwise
function gotNum(str){
return /\d/.test(str)
}
Jan 9 '06 #9
JRS: In article <Y9Cwf.1763$%W3 .364@trndny07>, dated Mon, 9 Jan 2006
23:17:12 local, seen in news:comp.lang. javascript, mrHoo
<dr***@yankeewe b.com> posted :
If you do this a lot stretch the String.prototyp e:

String.prototy pe.gotNum=funct ion(){
return /\d/.test(this)
}
otherwise
function gotNum(str){
return /\d/.test(str)
}


If you do it, do it right. The test is for a digit, not for a number,
and should be named accordingly.

R2D2 & C3PO have digits in their names, but not numbers.

HM Queen Elizabeth II and William Gates III have a number in their IDs,
but no digits.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jan 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1171
by: Chris Michael | last post by:
I've got a string of numbers, say 123456 (the actually number is 12 digits long). I need to calculate the sum of each individual number in the string of numbers, so in the example of 123456 the sum would be 21 (1+2+3+4+5+6). Thanks
5
13845
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
4
36735
by: Merlin | last post by:
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
9
11214
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet. I wanted to use a regular expression but I could not find an ‘AND’ operator within the regular expression – so I need something like match true if...
1
3148
by: Lala | last post by:
Hi, I need to check if a string contains unicode characters, and generate an alert if it does. (I do not want to parse the string to unicode). Anyone who knows how to do this? Thanks! :D
13
14954
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
14
2906
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also contain zero
4
10259
by: Jeff | last post by:
Hey ..NET 3.5 I'm trying to search a string to determine if the string contains </table>, but string.Contains don't find it. I've used WebRequest/WebReponse to retrieve the html from a webpage and now I'm searching through this html... this webpage contains 4 html tables. so there are 4 </table>, but my code don't find any...
2
12869
by: gamernaveen | last post by:
I wanted to include a string invalid character check in my application. Like the field comment , if a user inserts any special characters (Except '' , '@' , '/' , '=' and numbers - alphabets) , the function should return true , if the string contains no special char in any position , the function should return false. Can anyone design a...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.