• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

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 ?
0
yazan3000
Asked:
yazan3000
  • 3
  • 2
1 Solution
 
knightEknightCommented:
<INPUT type="text" name="numeric" maxlength="8"  onkeyup='this.value=this.value.replace(/[^\d]*/gi,"");' />
0
 
knightEknightCommented:
The above will prevent the user from entering non-numeric characters, so there is no need for an error message box.
0
 
James RodgersWeb Applications DeveloperCommented:
what knight gave you will replace/delete any non-numeric value

if you want an alert

<INPUT type="text" name="numeric" maxlength="8"  onkeyup='if(!(/\d/gi).test(this.value)){alert("Invalid Chracter");this.value=this.value.replace(/[^\d]*/gi,"");}' />

also uses the replace from knight
0
 
James RodgersWeb Applications DeveloperCommented:
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
0
 
James RodgersWeb Applications DeveloperCommented:
thans for the points
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now