Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

entering numbers only

Posted on 2003-11-12
5
578 Views
Last Modified: 2008-02-01
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
Comment
Question by:yazan3000
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 9733159
<INPUT type="text" name="numeric" maxlength="8"  onkeyup='this.value=this.value.replace(/[^\d]*/gi,"");' />
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 9733174
The above will prevent the user from entering non-numeric characters, so there is no need for an error message box.
0
 
LVL 25

Accepted Solution

by:
James Rodgers earned 50 total points
ID: 9733221
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
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9733291
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
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9779393
thans for the points
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question