0

I made this random number guessing game using PHP, and I'm trying to implement client-side form validation using JavaScript. The script should allow only a number from 1-100 and reject everything else like whitespace, alphanumeric characters, negative numbers, and floating point numbers, and/or a mix of these. Here's the script I've got so far:

<script type="text/javascript">
//<![CDATA[

// The checkForm() function makes sure the user's guess is valid
function checkForm() {
    var numericExpression = /[-+]?([0-9]*\.)?[0-9]+/; // Code is from regular-expressions.info
    if (document.getElementById("userGuess").value.match(numericExpression)) {
        return true;
        } // ends the if statement
    else {
        alert("Enter a valid number between 1-100 that isn't negative or a decimal value");
        document.getElementById("userGuess").focus();
        return false;
    } // ends the else statement
}
//]]>
</script>

And here's the form:

<div id="container">
   <h1 id="mainHeading">Let's play a game!</h1>

   <h2 id="subHeading">I'm thinking of a number between 1-100. Take a guess on what you think it is.</h2>

   <!-- User input -->
   <form action="" method="post" onsubmit="return checkForm()" name="formGuess" id="formGuess">
      <input type="text" name="userGuess" id="guessInput" /> 
      <button type="submit" id="submit">You're probably going to get it wrong.</button>
   </form>
</div> <!-- End container div -->

It doesn't work as intended though, and it only rejects pure alphabetic input. Anything else mixed and the script doesn't work. I have the web page uploaded to my personal website: PHP random number game

Thanks!

1
  • 3
    To check for a number from 1-100 you don't need regex. Commented May 9, 2013 at 22:21

2 Answers 2

2

You don't need regex. To check if a variable is a number use:

var isNumber = (! isNaN(variable));
Sign up to request clarification or add additional context in comments.

4 Comments

Why the outer parentheses?
I'm trying to familiarize myself with regular expressions, which is why I was doing it this way. Thank you for the help though!
I see. I recommend you this video tutorial series about regex: net.tutsplus.com/tutorials/php/…. This is the place where I actually understood regex for the first time.
Okie doke, I'll check it out. Thanks c:
0

While I agree with the others that you don't really need a regex, in case you wanted to know how the regex should look like for a number (which is really a string since you're using regex) between 1 and 100, here is one way: /^[1-9]$|^[1-9][0-9]$|^100$/

Let me know if that helps or if you have any questions :)

3 Comments

why not allow 007? or " 007 "?
Oh awesome, thank you so much! I'm trying to make sense of this regex you made right now. So if I understand this correctly, this regex allows for either one digit from 1-9 and nothing else; or two digits, with the first digit being 1-9 and the second 0-9 (effectively allowing 1-99); or simply 100. The ^ and the $ at the beginning and end makes it so that nothing else can come before or after. Is that right? o:
@iBringDaLULZ Yep, you nailed it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.