Jump to content

function onkeypress


gaya

Recommended Posts

Hi, My form is having fields like year, pageno,chapterno,standard and correct answer. I have written the script

function isNumberKey(evt , val)	   {    var no=val;		  var charCode = (evt.which) ? evt.which : event.keyCode;		   if (charCode != 46 && charCode!=8 && charCode > 31  && (charCode < 48 || charCode > 57 )  )    {			 return false;    }   else    {	 if (no.length < 4)	 {	 return true;	 }    else if(no.length ==4)	 {	 alert("No should be within 4 characters");	 return false;	 }    else	 {	 return false;	 }    }		  return true;	   }

But for correct answer i need to restrict input to one of the digits 1, 2 ,3 or 4. please tell me how to do this with another function.

Link to comment
Share on other sites

I suggest you this code or thing linke this:

 <html><head>    <title></title>    <script type="text/javascript">	  <!--	    function isDigit(e) {		    var charCode = (e.which) ? e.which : e.keyCode		    if (charCode > 31 && (charCode < 49 || charCode > 52))			    return false;		    return true;	    }	  //-->    </script></head><body>    <form action="">    <div>	    <input id="correctAnswerTxt" maxlength="1" size="1" onkeypress="return isDigit(event)" type="text" name="txtChar" />    </div>    </form></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...