Solved

entering numbers only

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now