Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > check if string contains any numbers

Reply
Thread Tools

check if string contains any numbers

 
 
Mokka
Guest
Posts: n/a
 
      01-07-2006
i need a check that can go through a string to see if it contains any
numbers.

Please help!

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      01-08-2006


Say, myVar is your object to be checked, if (/\d/.test(myVar)) .....


Danny
 
Reply With Quote
 
epbon
Guest
Posts: n/a
 
      01-08-2006
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

 
Reply With Quote
 
epbon
Guest
Posts: n/a
 
      01-08-2006
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}

just try ~_~

 
Reply With Quote
 
epbon
Guest
Posts: n/a
 
      01-08-2006
// t is your string
function hasNumbers(t)
{
var regex = /\d/g;
return regex.test(t);
}



try ~_~

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      01-09-2006
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/

 
Reply With Quote
 
mrHoo
Guest
Posts: n/a
 
      01-09-2006
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)
}


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-10-2006
JRS: In article <Y9Cwf.1763$%W3.364@trndny07>, dated Mon, 9 Jan 2006
23:17:12 local, seen in news:comp.lang.javascript, mrHoo
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to check a String contains another String,case-insensitively? tomaszewski.p Java 4 09-12-2008 05:40 PM
Re: How to check a String contains another String, case-insensitively? Roedy Green Java 0 09-12-2008 04:26 PM
Re: How to check a String contains another String, case-insensitively? Knute Johnson Java 0 09-11-2008 07:59 PM
any STL/string function available for determing string contains numeric nishit.gupta@st.com C++ 14 04-06-2007 01:00 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments