Aptitude (12) C (2) C++ (10) CSS (1) Data Structure (2) English (7) General (14) HTML (3) Integration (1) Itinerary and Travel Cost (1) Java (29) JQ (5) JS (5) JSP (1) Lightning (14) logical (3) Math (1) Music Notation (17) MySql (4) Network (1) Regex (1) salesforce (131) Servlet (4) SF Admin (31) SF Dev (102) SQL (4) SQL_SERVER (1) StudyPoints (2) ytChannels (2) अन्ताक्षरी (4)

NumbersOnly Validation using JQ,JS

JS Function:
-----------------------------------------
Function that returns true if value is numeric:

function numbersonly(e)
      {    
var keyCode;  
if (window.event) // IE
{
keyCode = e.keyCode;
}
return ((!((keyCode < 48) || (keyCode > 57))) || (keyCode == 8));
    }
------------------------------------------
Function that returns true if value is numeric and 2 digit only:
function numbersonly(e,s)
      {  
           var keyCode;
           if (window.event) // IE
           {
            keyCode = e.keyCode;
           }
           if(s.value.length>1)
           {
           return false;
           }
           else
           return ((!((keyCode < 48) || (keyCode > 57))) || (keyCode == 8));
      }
------------------------------------------
KeyCode values:
048
149
250
351
452
553
654
755
856
957
backspace8

Call this Method:
onkeypress="javascript:return numbersonly(event);"
---------------------------------------------------------------
onkeypress="javascript:return numbersonly(event,this);"

No comments: