Monday, January 19, 2009

JS: javascript accepts only number

This javascript, will helps to accept only number in a control. in this javascript is build with using regularexpression pattern

function AcceptNumber(objtextbox)
{
var exp = /[^\d]/g;
objtextbox.value = objtextbox.value.replace(exp,'');
}

The variable exp is the regular expression for digits only."[^\d]" means that all characters of keyboard except digits."/g" is used to validate all occurrences in a textbox.

call this method in "onkeyup" event, whenever we type letter, The value of whole textbox is validated and Characters other than digits are replace with empty string ''

No comments:

Post a Comment