Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[May 13, 2025, 02:04:25 PM]

[May 13, 2025, 02:04:25 PM]

[May 13, 2025, 02:04:25 PM]

[May 13, 2025, 02:04:25 PM]

[April 12, 2025, 01:54:20 PM]

[April 12, 2025, 01:54:20 PM]

[April 12, 2025, 01:54:20 PM]

[April 12, 2025, 01:54:20 PM]

[March 12, 2025, 03:05:30 PM]

[March 12, 2025, 03:05:30 PM]

[March 12, 2025, 03:05:30 PM]

[March 12, 2025, 03:05:30 PM]

[January 03, 2025, 03:29:12 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » Javascript
  Validating Numerical Input with JavaScript
Pages: [1]   Go Down
  Print  
Author Topic: Validating Numerical Input with JavaScript  (Read 1137 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
Validating Numerical Input with JavaScript
« Posted: September 29, 2007, 10:40:36 AM »




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

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

the script:

<SCRIPT LANGUAGE = "JavaScript">

Next let us hide the code from web browsers that are not capable of reading the script with

this line:

<!--HIDE

Then to finish the standard JavaScript entry, place these lines after your code:

//STOP HIDING--> </SCRIPT>


So now the format should look something like this:

<HTML> <HEAD> <TITLE>My Title</TITLE> </HEAD> <BODY>

<SCRIPT LANGUAGE="JavaScript"> <!--HIDE Insert YOUR code here... //STOP HIDING--> </SCRIPT>

</BODY> </HTML>

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: ============================================= <HTML> <HEAD> <TITLE>Numerical Validation</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!--HIDE function validate(mydata){ if (mydata == ""){ alert("Please enter a number.") } for(var i=0;i < mydata.length;i++){ var mydigit = mydata.charAt(i) if(mydigit < "0" || mydigit > "9"){ alert(mydigit + " is not a number.") return false } } document.forms[0].submit("#") return true } //STOP HIDING--> </SCRIPT> </HEAD> </BODY> <FORM> <H3>Guess a number between 1 and 1000:</H3> <INPUT TYPE="text"> <INPUT TYPE="submit" VALUE="Submit" onClick="return validate(this.form.elements[0].value)"> </FORM> </BODY> </HTML> =============================================

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.

Articles Source - Free Articles

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.303 seconds with 25 queries.