Validating Form Input in JavaScript

Written by Amrit Hallan

This time we'll make a form that collects information aboutrepparttar 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 ofrepparttar 119124 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 inrepparttar 119125 Name field [1,2,3,4,.], and inrepparttar 119126 Email field, no email should be withoutrepparttar 119127 "@" sign. We can carry out more complex validations, but atrepparttar 119128 moment, these three should suffice.

/// Removerepparttar 119129 extra dots while testing. They have been just inserted so that some email programs don't freak out atrepparttar 119130 presence of a JavaScript inrepparttar 119131 email.

<..script language="JavaScript1.2"> function CheckName(HoldName) { NoNumThere='true'; for(i=0; i

Enter your name:

Enter you phone:

Enter your email:

Using External JavaScript Files

Written by Amrit Hallan

When you have lots of JavaScript functions and you plan to use them on many web pages, it is always advisable to keep them in a separate file, and then include that file on every page where those functions are required. This way, you can better organize them, you don't have to copy/pasterepparttar code again and again (imagine if it runs to 100 or more lines!!), and you'll be forced to write more generalized code for better integration and scalability.

Besides, enclosingrepparttar 105602 common JavaScript routines in external files keeps your web pages uncluttered and search engine spiders and crawlers are able to reachrepparttar 105603 important content easily. They don't have to shuffle through tens of lines of scripts.

Inrepparttar 105604 first edition [that appeared in my newsletter - BYTESWORTH REACHOUT] of my article I had omittedrepparttar 105605 process of creatingrepparttar 105606 external JavaScript files, but when one of my readers asked me how to do it, I had to re-writerepparttar 105607 article.

It's very simple. Use any text editor of your liking (if you hand-code your HTML, you can userepparttar 105608 same editor that you use for your HTML editing). Writerepparttar 105609 required code, and saverepparttar 105610 file with .js (dot js) extension.

Cont'd on page 2 ==> © 2005
Terms of Use