Thursday, July 11, 2013

How to make a text box take only numeric input or any specific input as per user's requirement using JavaScript

In order to insert only numeric value from textbox this JavaScript code could be applied.
Same code can be modified to restrict the users to enter different keyboard inputs as per requirement.
This can be done by changing the ASCII value mentioned in the JavaScript file.


JAVASCRIPT CODE:

write this function under the JavaScript code.

 function isNumber(event)
   {
            var KeyBoardCode = (event.which) ? event.which : event.keyCode;
            if (KeyBoardCode > 31 && (KeyBoardCode < 48 || KeyBoardCode > 57))
            {
                return false;
            }
            return true;
    }


HTML CODE:

use this code in the textbox field to perform the required operation.

onkeypress="return isNumber(event)"

One can make the text box readonly and nothing can be pasted into that by adding the following script in textbox:

onKeyPress = "javascript: return false;" onPaste = "javascript: return false;"

No comments:

Post a Comment

Thursday, July 11, 2013

How to make a text box take only numeric input or any specific input as per user's requirement using JavaScript

In order to insert only numeric value from textbox this JavaScript code could be applied.
Same code can be modified to restrict the users to enter different keyboard inputs as per requirement.
This can be done by changing the ASCII value mentioned in the JavaScript file.


JAVASCRIPT CODE:

write this function under the JavaScript code.

 function isNumber(event)
   {
            var KeyBoardCode = (event.which) ? event.which : event.keyCode;
            if (KeyBoardCode > 31 && (KeyBoardCode < 48 || KeyBoardCode > 57))
            {
                return false;
            }
            return true;
    }


HTML CODE:

use this code in the textbox field to perform the required operation.

onkeypress="return isNumber(event)"

One can make the text box readonly and nothing can be pasted into that by adding the following script in textbox:

onKeyPress = "javascript: return false;" onPaste = "javascript: return false;"

No comments:

Post a Comment