#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    24
    Rep Power
    0

    Function onkeypress


    I'm having a function isNumberKey(event,val)
    In that function i'd checked the condition as
    Code:
    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;
           }
    In my form i'm using some fields like chapterno,year,pageno and correct answer.
    For the above fields i used the onkeypress, like
    Code:
    <td>Page No<font color="red">*</font></td><td>:</td>
    			<td><input type="text" name="pno" id="pno"  size="16" onkeypress="return isNumberKey(event, this.value)"/></td>
    But for the correct answer i dont want to check for 4values. I simply want to know whether the correct answer is 1 or 2 or 3 or4. How to do this, please anyone tell me.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    Hi,

    the function really isn't designed well. When a function is called isNumberKey, then that's what it's supposed to do: check if the user has pressed a number key. It should not check lengths or anything else. Do that in the event handler or write another function named checkLength or something.

    For checking if the input is 1..4, you could define a function to check if the input is included in an array of possible values:

    Code:
    function checkValueInRange(testValue, validValues) {
    	for (var i = 0; i < validValues.length; i++)
    		if (testValue == validValues)
    			return true;
    	return false;
    }
    However, this whole validation approach looks like overkill and is rather problematic with respect to usability. When I make I typo, I don't wanna be flooded with stupid alert messages blocking my whole browsers. If you want to use live validation, show a small checkmark/cross next to the field or something. But do not force the user to close error messages.
    Last edited by Jacques1; February 19th, 2013 at 06:56 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    24
    Rep Power
    0
    Thanks Jacques1. Please tell me is it possible to write more than a function inside a script. Because i need the above function also,and i need to have another function to restrict the input to one of the digits 1, 2 ,3 or 4.Also i'm having another function for form validation. Instead i want to write a function to restrict the input. Is it possible to write the seperate function to restrict the input.Please tell me.

IMN logo majestic logo threadwatch logo seochat tools logo