Validating Form Input in JavaScriptWritten by Amrit Hallan
This time we'll make a form that collects information about 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 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 Name field [1,2,3,4,.], and in Email field, no email should be without "@" sign. We can carry out more complex validations, but at moment, these three should suffice. /// Remove extra dots while testing. They have been just inserted so that some email programs don't freak out at presence of a JavaScript in email. <..script language="JavaScript1.2"> function CheckName(HoldName) { NoNumThere='true'; for(i=0; i
| | Using External JavaScript FilesWritten 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/paste 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, enclosing common JavaScript routines in external files keeps your web pages uncluttered and search engine spiders and crawlers are able to reach important content easily. They don't have to shuffle through tens of lines of scripts. In first edition [that appeared in my newsletter - BYTESWORTH REACHOUT] of my article I had omitted process of creating external JavaScript files, but when one of my readers asked me how to do it, I had to re-write article. It's very simple. Use any text editor of your liking (if you hand-code your HTML, you can use same editor that you use for your HTML editing). Write required code, and save file with .js (dot js) extension.
|