php - JavaScript regex form validation failing -


i made random number guessing game using php, , i'm trying implement client-side form validation using javascript. script should allow number 1-100 , reject else whitespace, alphanumeric characters, negative numbers, , floating point numbers, and/or mix of these. here's script i've got far:

<script type="text/javascript"> //<![cdata[  // checkform() function makes sure user's guess valid function checkform() {     var numericexpression = /[-+]?([0-9]*\.)?[0-9]+/; // code regular-expressions.info     if (document.getelementbyid("userguess").value.match(numericexpression)) {         return true;         } // ends if statement     else {         alert("enter valid number between 1-100 isn't negative or decimal value");         document.getelementbyid("userguess").focus();         return false;     } // ends else statement } //]]> </script> 

and here's form:

<div id="container">    <h1 id="mainheading">let's play game!</h1>     <h2 id="subheading">i'm thinking of number between 1-100. take guess on think 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 going wrong.</button>    </form> </div> <!-- end container div --> 

it doesn't work intended though, , rejects pure alphabetic input. else mixed , script doesn't work. have web page uploaded personal website: php random number game

thanks!

while agree others don't need regex, in case wanted know how regex should number (which string since you're using regex) between 1 , 100, here 1 way: /^[1-9]$|^[1-9][0-9]$|^100$/

let me know if helps or if have questions :)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -