Search
Recommended Sites
Related Links






   

Informative Articles

7 steps to set your website on the fast track to success
To get on the fast track to success one needs to: * Create a website that is dynamic and distinctive. The website name should match the domain name. Bad or broken links must not exist. JavaScript errors must be eliminated. The company's...

Get More Out of Your Tabbed Browser
Being someone that spends most of the day on the internet, I have often looked for ways to simplify and speed up my daily tasks on the web. A few years back, I luckily stumbled onto the Mozilla browser. Needless to say, I am now totally hooked...

How To Tweak Your Website For More Sales
There are two pillars to a successful online business. One of them is traffic and the other is sales copy - the text on your your website. That's what this article is about - getting more sales from your website. (1) Format. The fastest selling...

More Subscribers Through Pop-Up Windows And Joint Ventures
Pop-up windows are small windows which automatically appear when you visit or leave a website. You've probably seen one at some point as they are not very uncommon, and for a good reason. Pop-up windows are effective! They get in your face, grab...

Two Easy Ways To Optimize Your JavaScript
Did you know that JavaScript can damage search engine rankings? This article gives you two easy ways to make the JavaScript on your web site more search engine friendly, improve your search engine position, and give your traffic levels a nice...

 
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.