www.webdeveloper.com
Results 1 to 2 of 2

Thread: Is the opposite of isNaN() possible? With letters, I mean?

  1. #1
    Join Date
    Apr 2014
    Posts
    44

    Is the opposite of isNaN() possible? With letters, I mean?

    In Javascript, I've had loops set up where it asks the user to input a number and then using isNaN(), it determines if they input the correct information and if not, loops back to the beginning.

    This is great for doing math and stuff, but what if I want names? So that if the user enters numbers, I get a false result and it continues in the loop. I've done some searching, but I can't find anything for it. Is this possible?

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    It's time for regular expressions (which never really felt all that regular to me)!

    So, basically there is a simple regular expression that can check for numeric values.
    Code:
    var $regEx = /\d+/g;
    Now we take this and use javascripts .match() function and you've got a winner:
    Code:
    var $regEx = /\d+/g;
    var $myString = "S0m3 T3xt! W1th Numb3rz";
    var $alsoMyString = "Just regular text";
    
    if($myString.match($regEx)) alert("NUMBERS FOUND!");
    if($alsoMyString.match($regEx)) alert("NUMBERS FOUND!");
    So in the example this uses an if() statement with our match() function (so we get a true/false conditional statement). If a number is found in the string it alerts. If not then you see nothing. You can apply this same method to your own code, but rather than alert you would continue your code. Or you can add an exclamation point in front of the match statement if you want to only continue if no numbers are found.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles