Results 1 to 5 of 5

Thread: [Javascript OOP] - Easy Form Validation

Threaded View

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Arrow [Javascript OOP] - Easy Form Validation

    Introduction
    This is an Object Oriented Javascript form validation framework. I created this because I have written countless forms which require client side validation, each time I did a form I found myself copy and pasting a simple Javascript into the page to do basic form validation.

    I designed this component to be very easy to use, easy to add to existing forms and require very little code from the programmer. Whilst creating this I also had the W3C web standards in mind, so you will find that it will not cause your page to fail a W3C validation test.

    Below I will explain everything about the Validator, but be sure to download the samples attached which you can play around with.

    Simple Usage
    To use the basic functionality all you have to do is include the classes in the page you want to use it on. All the classes are inside the easy_validation.js file.

    javascript Code:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    5. <title>Easy Form Validation</title>
    6.  
    7. <script type="text/javascript" src="easy_validation.js"></script>
    8. <script type="text/javascript">
    9.    
    10.     function validate()
    11.     {
    12.         var v = new Validator(); //create a new Validator object called v
    13.        
    14.         //setup the fields we want to validate
    15.        
    16.         //          NAME                VALIDATION      ELEMENT ID
    17.         v.addField("Your Number",       "NUMBER",       "my_number");
    18.         v.addField("Your Date",         "DATE",         "dob");
    19.         v.addField("Your Text",         "NOT_EMPTY",    "line1");
    20.         v.addField("Email address",     "EMAIL",        "email");
    21.         v.addField("Terms",                       "CHECKBOX",       "terms");
    22.        
    23.         //call the validateForm method and return the result to the form element
    24.         return v.validateForm();
    25.     }
    26.    
    27. </script>
    28.  
    29. </head>
    30.  
    31. <body style="font-size:13px;font-family:Arial, Helvetica, sans-serif;">
    32.  
    33. <h1>Simple Usage</h1>
    34.  
    35. <form name="contact_us" method="post" action="simple_example.html" onsubmit="return validate();" />
    36. Enter a number: <input type="text" id="my_number" value="89" /><br />
    37. Enter a date: <input type="text" id="dob" value="01/01/2008" /><br />
    38. Enter some text: <input type="text" id="line1" value="abcdefg" /><br />
    39. Email address: <input type="text" id="email" value="[email protected]" /><br />
    40. Do you accept our terms? <input type="checkbox" id="terms" />
    41.  
    42. <input type="submit" name="submit_button" />
    43. </form>
    44.  
    45. </body>
    46. </html>


    The example above will produce a popup with the list of validation errors (if any). The only special things about the form are that each element has ID which is needed for the Validator to pickup the field, and the onsubmit event of the form element. That event tells the form to call our validator function when submitted, if it fails the validation tests, the form will not be submitted.

    We create a function called validate in the head section of the page, which is where we set the parameters for the Validator object.
    The addField method is self explanatory, however I will explain each of the arguments:

    addField(NAME, VALIDATION_TYPE, ID, CUSTOM_ERROR_MESSAGE)

    • NAME – This can be anything, it is only used when showing the default error message. This is not the actual name property of the element.
    • ID - The element ID of the field to validate
    • CUSTOM_ERROR_MESSAGE - This is optional, if set it will override the default error message
    • VALIDATION_TYPE – tells the Validator what validation to do on that field.


    Current list of validation types supported
    • NUMBER – data must be numeric (can be decimal/float).
    • DATE – data must be a date in format dd/mm/yyyy.
    • BASIC – data must be at least three characters long.
    • NOT_EMPTY – data must exist, can not be blank.
    • EMAIL – data must appear to be a valid email address.
    • NAME – data must only cantain alphabetic characters, no numbers or symbols.
    • CHECKBOX – used on a checkbox/tickbox field, the checkbox must be ticked.


    You can also change the default error message for all fields by using the setDefaultErrorMsg(“ERROR MESSAGE HERE”) method.
    Attached Files Attached Files
    Last edited by the182guy; Nov 15th, 2008 at 12:19 PM.
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width