Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Function to check if a string contains numbers

    Hey everyone,

    Is there a Javascript function to check if a string contains any numbers?

    Also, is there one to check if a string contains special characters or symbols?

    Thanks!
    Last edited by hunter1611; 12-27-2012 at 05:22 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    RegEx (Regular Expression) test:

    Code:
    var regexNum = /\d/g;// Global check for numbers
    var a = "This contains no numbers", b="This contains 1 number";
    var c = regexNum.test(a), d = regexNum.test(b);
    alert(c + " and " + d);// will alert false and true
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    hunter1611 (12-27-2012)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Ok so, slightly confused. How do I plug a variable into that? Is regexNum the variable you're checking for numbers?

    (Sorry, I'm really not a Javascript person..I fumble through it as needed).

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Quote Originally Posted by hunter1611 View Post
    Ok so, slightly confused. How do I plug a variable into that? Is regexNum the variable you're checking for numbers?

    (Sorry, I'm really not a Javascript person..I fumble through it as needed).

    The variables a and b in Wolfshade's example contain the strings to be analysed.

    To answer your second question

    var str = "This string contains 2 special characters ! and ?"
    alert (/[^a-z0-9\s\_]/gi.test(str)); // i.e. anything other than letters, numbers, space, underscore

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    hunter1611 (12-27-2012)

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Heh, whoops, ignore this post...something broke in my script...
    Last edited by hunter1611; 12-27-2012 at 05:09 PM.

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    51
    Thanks
    18
    Thanked 0 Times in 0 Posts
    OK! Got it working! Thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •