Webmasters Heaven, Free submission, Free Promotion, Free Tools
Backword Forward Home add this page to favoirates send this page to a friend print this page icons articles Webmaster Tools Web Site Submit
sitemap
Free Icons Free Articels Free Tools Website submission

Articles Category  ]

Google
 
Advertising
Affiliates
Blogs
CGI
CSS
DHTML
Domain Names
ECommerce
Email
HTML
Internet
Javascript
Link Popularity
Marketing
MLM
Newsletters
Promotion
RSS
Search Engines
Site Security
Traffic Analysis
Web Hosting
Web Design
Webmasters


articles -> javascript
Article Title Author
A Recursive Filtering Workbench in Java Richard G. Baldwin
7 Reasons You Should Avoid JavaScript Dynamic Navigation Iain Row
Using External JavaScript Source Files Sasch Mayer
dhtmlxTabbar - Creating Dynamic Tabbed Interface Has Never Been Easier! Ivan Petrenko
dhtmlxTree version 1.3 - Add Flexibility to Your Web Trees Ivan Petrenko
Java tips & tutorials - best Subscriptions for learning Java Groshan Fabiola
Ajaxgear Toolkit Allan Spartacus Mangune
Evaluation of Keywords for an AdSense Oriented Website F. Terrence Markle
Submit form conditionally - JavaScript SiteArticles.com
New Customizable JavaScript Menu for Web Applications Ivan Petrenko
Capturing Video And Audio Streams How-To John Deprice
How to handle web surfers, who disabled JavaScript Michael Kashirin
Simplified form to mail: Unlimitted input Endar WS
Anti right-click pop up : pop up will appear if visitors right-clicks his mouse Endar WS
Password protected document : Protect your web page with specific password for each member Endar WS
Building HTML codes automatically, just copy&paste your content Endar WS
Javascript Password Protection Timur Abdrakhmanov
How to test for the Javascript DOM? Riaan Pieterse
Interactive Forms Brian Zimmer
Validating Numerical Input with JavaScript David Morse
Instant plug-in scripts that help you profit... "Special offer expiry date with a twist" Michel Komarov
Javascript Basics 01 Lisa Spurlin
Javascript; Browser Detection and Page Redirection William J. Tolson
Ask Mr. D - JavaScript Bill Daugherty
Grow Your Subscribers Exponentially With These Javascript E-mail Capture Boxes! Vishal Rao
Some Useful JavaScript Tricks Mitchell Harper
Validating Form Input in JavaScript Amrit Hallan
Using External JavaScript Files Amrit Hallan
Guide to creating simple chromeless popup windows Michael Bloch
Dirty Web Promotion Tricks #1 - Legitimate and Malicious Javascripts Michael Bloch
Spice Up Your Web Site with JavaScript Shelley Lowery
Make your website scream with excitement with Java! Johnathan Wyka-Warzecha

Validating Numerical Input with JavaScript   by David Morse


What? Make a mistake entering data? Who me? NO WAY! Right.

Every form of data input by a user should be validated in some form or fashion. If you get

clean data in, you won't get garbage out. This tutorial is going to explain how to validate

numerical data entered into a form using JavaScript.

First, let us begin with the code to insert the JavaScript into your HTML document.

Place these lines between the <BODY> and </BODY> tags.

This line tells the web browser to expect some JavaScript code and signal the beginning of

the script:

<SCRIPT LANGUAGE = "JavaScript">

Next let us hide the code from web browsers that are not capable of reading the script with

this line:

<!--HIDE

Then to finish the standard JavaScript entry, place these lines after your code:

//STOP HIDING--> </SCRIPT>

So now the format should look something like this:

<HTML> <HEAD> <TITLE>My Title</TITLE> </HEAD> <BODY>

<SCRIPT LANGUAGE="JavaScript"> <!--HIDE Insert YOUR code here... //STOP HIDING--> </SCRIPT>

</BODY> </HTML>

Now on to validating the numerical input.

First we will create a function with one arument:

function validate(mydata){

These lines will test for a blank enty then prompt the user for input:

if (mydata == ""){ alert("Please enter a number.") }

Next we will create a for loop which will look at each character in the data until it

reaches the end:

for(var i=0;i < mydata.length;i++){

Now create a variable and assign the counter variable value to it:

var mydigit = mydata.charAt(i)

To screen out symbols, punctuation, and letters, place an if statement in the loop:

if(mydigit < "0" || mydigit > "9"){

The || in the if statement scans for both conditions.

The next line will alert the user to any mistakes he/she has made:

alert(mydigit + " is not a number.")

Here is the complete code including HTML: ================================ <HTML> <HEAD> <TITLE>Numerical Validation</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!--HIDE function validate(mydata){ if (mydata == ""){ alert("Please enter a number.") } for(var i=0;i < mydata.length;i++){ var mydigit = mydata.charAt(i) if(mydigit < "0" || mydigit > "9"){ alert(mydigit + " is not a number.") return false } } document.forms[0].submit("#") return true } //STOP HIDING--> </SCRIPT> </HEAD> </BODY> <FORM> <H3>Guess a number between 1 and 1000:</H3> <INPUT TYPE="text"> <INPUT TYPE="submit" VALUE="Submit" onClick="return validate(this.form.elements[0].value)"> </FORM> </BODY> </HTML> ================================

You can test the above code by copying and pasting it into a text document then view it in

your browser.

And that's how easy it is to test user input for numerical input.

About the Author

(c)2004 BiosBrain David Morse is the owner of http://www.biosbrain.com which is a full service support site featuring articles on programming, graphics, web-based business and more! Come visit us today!


[Advertisement ]