We can able to restrict entering special characters inside the text input field in a form
Call "forceNumber(event)" function the "onKeyPress" event on input field like this,
<input name="text" maxlength="17" onkeypress="return forceNumber(event);" />
Use the following function to check in javascript itself
<script language="javascript">
function forceNumber(event){
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 32 && keyCode != 37 && keyCode != 39 && keyCode != 40 && keyCode != 41 && keyCode != 43 && keyCode != 45 && keyCode != 46)
return false;
}
</script>
This function will allow numbers from 0-9 and characters "+ ()-"
and it will be more useful in telephone number validation inside a form
Note: the function "forceNumber()" should be inside <head>.
April 22, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment