Search
Recommended Sites
Related Links






Valid XHTML 1.0 Transitional

Valid CSS!
   

Informative Articles

50 Surefire Web Design Tips
Tips to brand your website Include your logo in all pages. Position it at the top left or each page. Complement your logo with a tagline or catchy sentence that summarizes your business purpose. For example "Always low prices" is...

DESIGNING WITH STYLE
One important aspect of good design is to give your site a theme and coherence. A great deal of this can be done using a stylesheet. Using stylesheets is not difficult. In fact it makes the task of designing a website that much easier. However...

Flash - To Use Or No To Use?
Out there in the WWW there are thousands sites using flash for their needs. But the majority sites are not in this list. Let's summaries some facts about flash using on YOUR web site and after that it's you who will have to decide: to use or not...

How to Add a Search Engine to Your Site
When visitors arrive at your web site you want them to find the information quickly otherwise they will lose patience and move on. A great way achieve this is to add a search engine or search box to your web site. Since most visitors are already...

Teacher Websites - A Student's Perspective
I have been a student for many years. I even took the 7-year plan to go through college for my Bachelor's degree. Every so often I still take another course. Since much of my studies and background involve web-related stuff, I tend to notice...

 
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.