Search
Recommended Sites
Related Links






   

Informative Articles

A must-know about computer and internet glossary
Computer-related things tend to have a language all their own. While you do not need to know all of it, there are many confusing words and phrases that you are going to come across sooner or later. Bandwidth. Bandwidth is the amount of data...

Displaying RSS Feeds
RSS offers webmasters a unique opportunity to display fresh content on websites. While publishing an RSS feed is a great way to generate site interest and increase communication, syndicating and displaying feeds from related relevant sources can...

How secure is your data? Preventing the theft of your internet property.
Monday morning first thing and the phone rings. It's a typical call, since the previous Thursday a small business has noticed that some of the images on it's' website are starting to appear on other sites. They know enough to understand that once...

Pop-Under Windows - The Latest Pop-Window Trend
Although there has been a great deal of controversy over the use of pop-up windows, the fact remains; pop-up windows are highly effective. The latest pop-window trend to hit the Internet is the pop-under window. Pop-under windows are less...

XSS Vulnerabilities, So understimated, so dangerous
In this little paper I will try to convince admins, webmaster and in general everyone is concerned to secure a web site of how dangerous can be a XSS hole. I will not cover in depth what XSS is because there's a huge library on this topic...

 
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.