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

check if string contains any numbers

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

Please help!

Jan 7 '06 #1
Share this Question
Share on Google+
12 Replies


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

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

P: n/a


Say, myVar is your object to be checked, if (/\d/.test(myVar)) .....
Danny
Jan 8 '06 #4

P: n/a
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

Jan 9 '06 #5

P: n/a
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

Jan 9 '06 #6

P: n/a
// t is your string
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

:) try ~_~

Jan 9 '06 #7

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.