Search
Recommended Sites
Related Links






   

Informative Articles

3 Ways To STOP Affiliate Link
As money gets tighter and product prices rise, people who know how to manipulate the system will sometimes replace your affiliate ID with theirs and "hijack" your commissions. Let's face the facts! Almost everyone online today is...

5 Little Known Ways To Generate Free Traffic
Most people have the notion that generating traffic is a one-off event, and it involves spending huge sums of money on advertising. Nothing can be further from the truth. First of all, it's possible to open up streams of autopilot...

5 Simple Search Engine Optimization (SEO) Techniques
The lifeblood of any website/ecommerce business is traffic, and every webmaster knows the best type of traffic is natural, organic search engine traffic. There are two very important reasons for this: (1) it is extremely targeted, and (2) It is...

Adding Flash 4 to your website
Macromedia has taken the internet world by storm with its introduction of Flash a few years ago. What started as an animation tool has evolved into a full website development tool. After clinching several deals with major supporters like...

Meta tags and how to use them: Part 2
This tutorial continues on from Meta tag Tutorial: Part 1 and shows some of the less well known but often more interesting tags. <u><meta name="googlebot" content="nosnippet"></u> You know all those extra features like...

 
Validating Numerical Input with JavaScript

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 and tags.

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

the script:



So now the format should look something like this:



My Title








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:
=============================================


Numerical Validation




Guess a number between 1 and 1000:





=============================================

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!

Sign up for PayPal and start accepting credit card payments instantly.