edvinson
asked on
How to accept only numbers in textfield?
I have a text field and only want to accept numbers (for a phone number). Can I do this using some simple javascript?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hey it's easy by using java script.
<SCRIPT LANGUAGE="JavaScript">
function checkIt(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
status = "This field accepts numbers only."
return false
}
status = ""
return true
}
</script>
//just write this script just above ur form or in body tag.
//and in textfield code just write onKeyPress="return checkIt(event)"
//as shown below example
<input type="text" name="first_name" maxlength="50" size="30" onKeyPress="return checkIt(event)">
Open in new window