FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Become a Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
div
 

JavaScript Tutorial: Validating a text box for numeric data(integer or decimal) & disallow invalid characters

by: jawahar, 7/5/2007 4:43:39 AM, Views: 2075
 
There will be a case where you have to limit the HTML form inputs to integers or decimals, so that invalid character entries can be prevented. The following tutorial and code can be used to validate a text box and allow only numeric data including decimal.

Place the following code in the head section of the html page.

<script language="JavaScript">
function fn_validateNumeric(thi,dec)
{
   if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 46))
      event.returnValue = false;
   if(dec=="n" && event.keyCode == 46)
   {
      event.returnValue = false;
   }
   else
   {
      if(event.keyCode == 46 && instr(thi.value,".")>=0)
         event.returnValue = false;
   }
}</script>


Place the below code inside the html form or just copy the highlighted text and place it in your text box code:

Syntax: To allow only integers

<input ONKEYPRESS="fn_validateNumeric(this,'n')" name="txt_numeric" type="text" id="txt_numeric" size="10" maxlength="10">


Syntax: To allow integers & decimals

<input ONKEYPRESS="fn_validateNumeric(this,'')" name="txt_numeric" type="text" id="txt_numeric" size="10" maxlength="10">


The above code requires the Javascript InStr function

So, whenever a key is pressed when the focus is in the text box, the function will be called and validated. The function will not allow any characters other than the numeric data(including decimal). If you dont want the decimal, just remove the keycode condition which checks for 46 and the second if statement)
Next Steps:
Add this Tutorial to:
Rating Booth
Not Yet Rated!
Rate:
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
     
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
arrow
sep
Get Rid of Spyware with Anti Spyware Software
Options
»  Submit a Tutorial
» Ask a Question
» Add to KB
» View all your Q's
JavaScript RSS Feed
Most Popular Tutorial
1.InStr in JavaScript
2.Mid() function in Ja...
3.Capturing Mouse Posi...
4.Setting IFRAME heigh...
5.Dynamically create/a...
6.Creating floating me...
7.View Clipboard conte...
8.Validating a text bo...
9.Dynamically changing...
10.Javascript Input Box...
Most Popular Solution
1.How to reload a fram...
Top Rated
1.InStr in JavaScript
2.Mid() function in Ja...
3.Dynamically create/a...
4.Opening a New window...
5.Capturing Mouse Posi...
6.Creating floating me...
7.Dynamically changing...
8.View Clipboard conte...
9.How to reload a fram...
Top Rankers
Overall
sep
1. jawahar1400
sep

Yearly - 2008
sep
1. jawahar200
sep
Expertsforge Sponsors
1. Tech News
2. Wii Reviews
bnrtop