check if string contains any numbers

Discussion in 'Javascript' started by Mokka, Jan 7, 2006.

  1. Mokka

    Mokka Guest

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

    Please help!
    Mokka, Jan 7, 2006
    #1
    1. Advertising

  2. Mokka

    RobG Guest

    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
    RobG, Jan 7, 2006
    #2
    1. Advertising

  3. Mokka

    RobG Guest

    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
    RobG, Jan 7, 2006
    #3
  4. Mokka

    Danny Guest

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


    Danny
    Danny, Jan 8, 2006
    #4
  5. Mokka

    epbon Guest

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

    just try ~_~
    epbon, Jan 8, 2006
    #5
  6. Mokka

    epbon Guest

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

    just try ~_~
    epbon, Jan 8, 2006
    #6
  7. Mokka

    epbon Guest

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



    :) try ~_~
    epbon, Jan 8, 2006
    #7
  8. Mokka

    Randy Webb Guest

    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/
    Randy Webb, Jan 9, 2006
    #8
  9. Mokka

    mrHoo Guest

    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)
    }
    mrHoo, Jan 9, 2006
    #9
  10. JRS: In article <Y9Cwf.1763$%W3.364@trndny07>, dated Mon, 9 Jan 2006
    23:17:12 local, seen in news:comp.lang.javascript, mrHoo
    <> 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.
    Dr John Stockton, Jan 10, 2006
    #10
  11. Mokka

    Randy Webb Guest

    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
    > <> 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/
    Randy Webb, Jan 10, 2006
    #11
  12. JRS: In article <>, dated Tue, 10 Jan
    2006 14:35:40 local, seen in news:comp.lang.javascript, Randy Webb
    <> 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.
    Dr John Stockton, Jan 11, 2006
    #12
  13. Mokka

    Randy Webb Guest

    Dr John Stockton said the following on 1/11/2006 9:05 AM:
    > JRS: In article <>, dated Tue, 10 Jan
    > 2006 14:35:40 local, seen in news:comp.lang.javascript, Randy Webb
    > <> 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/
    Randy Webb, Jan 11, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    912
    X-Centric
    Jun 30, 2005
  2. Replies:
    14
    Views:
    700
    Jacek Dziedzic
    Apr 6, 2007
  3. tomaszewski.p
    Replies:
    4
    Views:
    764
    Tom Anderson
    Sep 12, 2008
  4. Knute Johnson
    Replies:
    0
    Views:
    400
    Knute Johnson
    Sep 11, 2008
  5. Roedy Green
    Replies:
    0
    Views:
    408
    Roedy Green
    Sep 12, 2008
Loading...

Share This Page