Home arrow JavaScript arrow Page 5 - Form Validation with JavaScript

Hammer Time - JavaScript

javascriptForm validation can help to reduce the amount of bad data that gets saved to your database. In this article, find out how you can write a simple JavaScript form validator for basic client-side validation, and learn a little bit about JavaScript OOP in the process as well.

TABLE OF CONTENTS:
  1. Form Validation with JavaScript
  2. Check Point
  3. Object Lessons
  4. Rock On
  5. Hammer Time
  6. How Things Work
  7. A Little Space
  8. Expressing Yourself
  9. Under Construction
  10. A Quick Snack
By: Nariman K, (c) Melonfire
Rating: starstarstarstarstar / 464
December 01, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Next, how about modifying the Band() object to accept some arguments.

<script language="JavaScript">
// object constructor
function Band (n, g) 
{
// object properties
this.name = n;
this.genre = g;
}
</script>


The object Band now has two properties, "name" and "genre." With this, it is possible to create an instance of the object Band, and pass it two parameters ("n" and "g"), which are then stored as object properties. Note my use of the "this" keyword, which provides a convenient way to access variables (and functions) which are "local" to the object.

Just as you can define object properties, it's also possible to define object methods - essentially, simple JavaScript functions. A little more evolution, and the Band object now sports a whoRules() method, which uses the value stored in the object property "name."

<script language="JavaScript">
// object constructor
function Band(n, g) 
{
// object properties
this.name = n;
this.genre = g;
// object methods
this.whoRules = whoRules;
}
// object method whoRules()
function whoRules() 
{
alert (this.name + " rules!");
}
</script>


A couple of interesting things here: first, the object method whoRules() is actually defined outside the object constructor block, though it references object properties using the "this" keyword. Second, just as object properties are defined using "this," object methods need to be defined in the same manner. Witness my addition of…

function Band(n, g) 
{
...
// object methods
this.whoRules = whoRules;
...
}


… to the object constructor block.Wanna see how it works? Take a look at the code...

<script language="JavaScript">
obj = new Band("SledgeHammer", "Classic Rock");
obj.whoRules();
</script>


... and the output.

SledgeHammer rules!




 
 
>>> More JavaScript Articles          >>> More By Nariman K, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: