Search
Recommended Sites
Related Links






Valid XHTML 1.0 Transitional

Valid CSS!
   

Informative Articles

Instant Attention Popups, Just Add Common Sense
Instant Attention Popups, Just Add Common Sense by Robert Plank There's a very easy way to create instant attention, unblockable "popups" that aren't popups that don't look like popups and technically aren't popups at all. They don't even require...

Microsoft CRM Customization - processing in/out-going email messages
We would like to give you several situations, when you may need custom development and programming to improve Microsoft CRM functionality. This overview is for programmer, software developer, IT specialist, database administrator. Microsoft...

SEO's Relationship With Website Architecture
Search engine optimization for today's search engine robots requires that sites be well-designed and easy-to-navigate. To a great degree, organic search engine optimization is simply an extension of best practices in web page design. SEO's...

Server Side Programming Languages
Server-side programming languages are scripts that are executed on the server, and are then translated into HyperText Markup Language (HTML) which can be viewed by all web browsers. The two most popular server-side scripting ...

Throw Away Your Money on Search Engine Optimization
Search engine optimization consists of some relatively arcane issues that are not obvious to anyone. That I can be thankful for, I suppose, or I wouldn't continue to be in demand as an SEO specialist. But why do clients throw money out the window...

 
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.