HighDots Forums  

check if string contains any numbers

Javascript JavaScript language (comp.lang.javascript)


Discuss check if string contains any numbers in the Javascript forum.

Reply
 
Thread Tools Display Modes
  #1  
Old   
Mokka
 
Posts: n/a

Default check if string contains any numbers - 01-07-2006 , 09:29 AM






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

Please help!




Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: check if string contains any numbers - 01-07-2006 , 09:54 AM






Mokka wrote:
Quote:
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
  #3  
Old   
RobG
 
Posts: n/a

Default Re: check if string contains any numbers - 01-07-2006 , 09:58 AM



RobG wrote:
Quote:
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
  #4  
Old   
Danny
 
Posts: n/a

Default Re: check if string contains any numbers - 01-07-2006 , 11:25 PM





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


Danny

Reply With Quote
  #5  
Old   
epbon
 
Posts: n/a

Default Re: check if string contains any numbers - 01-08-2006 , 06:29 AM



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

just try ~_~


Reply With Quote
  #6  
Old   
epbon
 
Posts: n/a

Default Re: check if string contains any numbers - 01-08-2006 , 06:30 AM



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

just try ~_~


Reply With Quote
  #7  
Old   
epbon
 
Posts: n/a

Default Re: check if string contains any numbers - 01-08-2006 , 06:36 AM



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



try ~_~


Reply With Quote
  #8  
Old   
Randy Webb
 
Posts: n/a

Default Re: check if string contains any numbers - 01-09-2006 , 04:30 AM



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.

Quote:
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
  #9  
Old   
mrHoo
 
Posts: n/a

Default Re: check if string contains any numbers - 01-09-2006 , 06:17 PM



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
  #10  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: check if string contains any numbers - 01-10-2006 , 08: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
<drwot (AT) yankeeweb (DOT) com> posted :
Quote:
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
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.