473,406 Members | 2,847 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,406 software developers and data experts.

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 78689
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.com, 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){return /\d/.test(t);}

--
Randy
comp.lang.javascript 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.prototype:

String.prototype.gotNum=function(){
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***@yankeeweb.com> posted :
If you do this a lot stretch the String.prototype:

String.prototype.gotNum=function(){
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.demon.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
Dr John Stockton said the following on 1/10/2006 8:26 AM:
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***@yankeeweb.com> posted :
If you do this a lot stretch the String.prototype:

String.prototype.gotNum=function(){
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.


People can name functions anything they want. The fact that you disagree
with that naming convention is irrelevant to the code actually working.
R2D2 & C3PO have digits in their names, but not numbers.
Actually, they do. The name is R-Two-D-Two which contains two numbers.
C3PO has the Number Three in its name.
HM Queen Elizabeth II and William Gates III have a number in their IDs,
but no digits.


Well Duh.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 10 '06 #11
JRS: In article <ft********************@comcast.com>, dated Tue, 10 Jan
2006 14:35:40 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 1/10/2006 8:26 AM:


First of October? Surely, even if your software gets the date wring in
the first place, you can correct it manually?
If you do it, do it right. The test is for a digit, not for a number,
and should be named accordingly.


People can name functions anything they want. The fact that you disagree
with that naming convention is irrelevant to the code actually working.


Code should be written so that it can be correctly understood by the
literate, as well as the illiterate. You'll just have to accept that
handicap.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jan 11 '06 #12
Dr John Stockton said the following on 1/11/2006 9:05 AM:
JRS: In article <ft********************@comcast.com>, dated Tue, 10 Jan
2006 14:35:40 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 1/10/2006 8:26 AM:

First of October?


Where is there anything above that says October? Even you, in all your
infamously glorious ignorance surely wouldn't read a post on the 10th
day of January and read the date as October the First? The only two word
phrase that can appropriately describe that behavior is: pedantic idiot.
With the emphasis on idiot.
Surely, even if your software gets the date wring in the first place,
you can correct it manually?
How do you wring a date?

wring: to squeeze or twist especially so as to make dry or to extract
moisture or liquid

I am unaware that a date contains any moisture or liquid to extract.

Check your spell checker, it seems to be broken. And from someone who
self proclaims to compose off line, connect and then post you should
endeavor to check your spelling. Especially if you wish to be as
pedantic as you are at times.

Anyway, if my software had gotten the date wrong, then surely I could
have corrected it. But since it got it right there was no reason to
correct it. Manually or programatically.
If you do it, do it right. The test is for a digit, not for a number,
and should be named accordingly.
People can name functions anything they want. The fact that you disagree
with that naming convention is irrelevant to the code actually working.

Code should be written so that it can be correctly understood by the
literate, as well as the illiterate.


There was nothing to be mis-understood about that code. Well, other than
your pedantic ignorance about the meaningless name of a function. It
could well have been named myHumptyDumptyFunction and the code would
have remained the same. Other than it didn't satisfy your ignorant
pedantic desires of a name for that function.

You'll just have to accept that handicap.


I don't have a problem with any handicap you have, I am pleased to see
you finally admit to having one though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 11 '06 #13

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

Similar topics

6
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...
5
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...
4
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...
9
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....
1
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
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
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...
4
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...
2
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) ,...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.