Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 08, 2024, 10:26:18 AM]
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
   MegaPixelShop
   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 Form Input in JavaScript
Pages: [1]   Go Down
  Print  
Author Topic: Validating Form Input in JavaScript  (Read 2336 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
Validating Form Input in JavaScript
« Posted: September 29, 2007, 10:54:12 AM »


This time we'll make a form that collects information about the visitor at your site. You must have filled-in copious registration forms or survey forms where you had to enter your name, your email, your address, etc. Sometimes users, intentionally or unintentionally, enter wrong information that can either spoil your database scheme or give you lots of useless data and hence, waste your precious server space.

To avoid such problems, as much as it can be managed, we programmatically try to make sure, that data is entered in an orderly fashion, and no unusable fields are entered. Checking individual fields of the form does this.

We'll see a form here with three fields: Name, Phone and Email. In this form, no field should be left blank, there should be no numbers in the Name field [1,2,3,4,.], and in the Email field, no email should be without the "@" sign. We can carry out more complex validations, but at the moment, these three should suffice.

/// Remove the extra dots while testing. They have been just inserted so that some email programs don't freak out at the presence of a JavaScript in the email.


<..script language="JavaScript1.2"> function CheckName(HoldName) { NoNumThere='true'; for(i=0; i<HoldName.length; i++) { for(j=0; j<10; j++) { if(HoldName.charAt(i)==j.toString()) { NoNumThere='false'; break; } } if(NoNumThere=='false') { break; } } return NoNumThere; } function CheckMail(HoldMail) { IsValid='true'; if(HoldMail.indexOf("@")<=0) { IsValid='false'; } return IsValid; } function checkfields() { var AllFilled='true'; for(i=0; i<3; i++) { if(visitor.elements.value.length==0) { alert("The field [" + visitor.elements.name + "] can not be left blank."); AllFilled='false'; visitor.elements.focus; return; } } if(AllFilled=='true') { var NameValid=true; var EmailValid=true; NameValid=CheckName(visitor.vname.value); EmailValid=CheckMail(visitor.vemail.value); if(NameValid=='false') { alert("Sorry, your name can not contain numbers."); visitor.vname.focus; } if(EmailValid=='false') { alert("Sorry, this does not seem like a valid email address."); } } if(NameValid=='true' & EmailValid=='true') { alert("RIGHTO!!!"); } } </script> <body> <form name="visitor"> Enter your name: <input name="vname" type="Text">
Enter you phone: <input name="vphone" type="Text">
Enter your email: <input name="vemail" type="Text">
<input type="Button" name="submit" value="Submit" onclick="checkfields();"> <input type="Reset" name="reset" value="Reset"> </form>

</body> </html>

Copy and paste the code as it is, and save the entire content as a new HTML page. Then load it on to your browser. Unless you see the result, it'll be difficult to follow the script if you do not have prior programming background. The first condition is, none of the fields can be submitted blank. Click on the submit button without entering anything and observe the reaction.

Here, we are making ample use of the recently learnt for(){.} loop. Then we have used function too, to carry out certain validations. Our main function, checkfields(), is associated with the OnClick attribute of the "Submit" button, that is, when you click on the "Submit" button, this function gets triggered.

Some new terms in today's script are: true, false, charAt(), toString(), break, indexOf(), string.length, and orm.elements[ ].

A quick explanation to make things easier:

If at 10:30 pm, I say, "It is night", then

var fact='true'

and if I say at 10:30 pm that "It's afternoon", then

var fact='false'

Which explains the use of true and false, which are also called Boolean operators, which means, a Boolean variable can either be true or false, but NEVER both.

Until we learn about arrays, every character in a string has an index position. For instance, if we have

var city="Delhi"

then city.charAt(0)="D", city.charAt(1)="e", city.charAt(2)="l"...city.charAt(4)="i".

toString(), converts another data type to a string data-type. For example,

var num1=31 var num2=21 var char1=num1.toString() var char2=num2.toString()

So,

num1+num2=52 and char1+char2=3221

In the second case, instead of being added, the variables are being concatenated, which indicates that they are strings, not numbers. We'll see its application later.

break, true to its name, breaks something. In this case, it breaks the loop in which it occurs, and takes the execution of the program to the line immediately after the loop, without meeting the condition required to complete the loop.

indexOf() tells us about the position of a particular character in a string. Look its use in the following code:

var city="Delhi"

Referring to this code, city.indexOf("e") should give us a value 1 and city.indexOf("h") should give us a value 3. city.indexOf("z") should give us a value less than zero, indicating that it does not belong to the given string.

String.length gives us the length of the string, for instance, if city="Delhi", then city.length would give us 5.

Again, elements[ ] is an array, and we haven't dealt with them yet, so we leave the rest of the explanation to the next section.

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.183 seconds with 24 queries.