Solved

entering numbers only

Posted on 2003-11-12
5
576 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

786 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