check if string contains any numbers

M

Mokka

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

Please help!
 
R

RobG

Mokka said:
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.
 
R

RobG

RobG said:
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().
 
E

epbon

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



:) try ~_~
 
R

Randy Webb

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);}
 
M

mrHoo

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)
}
 
D

Dr John Stockton

JRS: In article <Y9Cwf.1763$%W3.364@trndny07>, dated Mon, 9 Jan 2006
23:17:12 local, seen in mrHoo
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.
 
R

Randy Webb

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 mrHoo



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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 10 Jan
2006 14:35:40 local, seen in Randy Webb
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?
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.
 
R

Randy Webb

Dr John Stockton said the following on 1/11/2006 9:05 AM:
JRS: In article <[email protected]>, dated Tue, 10 Jan
2006 14:35:40 local, seen in Randy Webb



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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top