Link to home
Start Free TrialLog in
Avatar of yazan3000
yazan3000

asked on

entering numbers only

hi experts..
i have form with text box and i want to make user enter numbers only, when user enters non numbers an error  message should appear.
any idea ?
Avatar of knightEknight
knightEknight
Flag of United States of America image

<INPUT type="text" name="numeric" maxlength="8"  onkeyup='this.value=this.value.replace(/[^\d]*/gi,"");' />
The above will prevent the user from entering non-numeric characters, so there is no need for an error message box.
ASKER CERTIFIED SOLUTION
Avatar of James Rodgers
James Rodgers
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or in a function
<script>
function numericOnly(objText, iFlag){
regEx=/[^\d]/gi
if(regEx.test(objText.value)){
      if(iFlag){
            alert("Invalid Character");
      }
      objText.value=objText.value.replace(/[^\d]*/gi,"");
}

}

</script>
can be call from any input as

<INPUT type="text" name="numeric" maxlength="8"  onkeyup="numericOnly(this,1)">
give alert

or

<INPUT type="text" name="numeric" maxlength="8"  onkeyup="numericOnly(this,0)">

no alert
thans for the points