Sunday, July 26, 2009

Simple Javascript code for validating only numbers in a text box

A small script which will allow only numbers to be typed inside a textbox.
No other character or symbol will be allowed.
This would be helpful if we have a field in which only numbers should be entered by user.


function validateForNumbers(valFromTextBox)
{
if(valFromTextBox.value.length>0)
{
valFromTextBox.value =
valFromTextBox.value.replace(/[^\d]+/g, '');
}
}

Call this function on "onkeyup" event.

Bye...

No comments: