JavaScript
  Home arrow JavaScript arrow Page 2 - Developing a simple validation library in ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Developing a simple validation library in JavaScript
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 52
    2006-02-15

    Table of Contents:
  • Developing a simple validation library in JavaScript
  • Validations for spaces, tabs, carriage returns, new line characters etc.
  • Validation for single digit or single alphabet
  • Validation for single digit or single alphabet: another way
  • Validation for email address

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing a simple validation library in JavaScript - Validations for spaces, tabs, carriage returns, new line characters etc.


    (Page 2 of 5 )

    Now, let us try to develop a simple script (JavaScript) to validate spaces and tabs.  Take a look at the following code: 

    <html>
          <head>
                <script id="clientEventHandlersJS" language="javascript">
    <!--
    function isContainSpacesOrTab(val){
          if(val==null){return false;}
          if(val.length==0) {return false;}
          for(var i=0;i<val.length;i++) {
                if ((val.charAt(i)==" ") || (val.charAt(i)=="\t")) {return true;}
          }
          return false;    
          }
    function Button1_onclick() {
    var v = document.all("Textarea1").value;
    alert(isContainSpacesOrTab(v));
    }
     
    //-->
                </script>
          </head>
          <body>
                <form id="form1">
                      <P>Enter Remarks:</P>
                      <P><TEXTAREA id="Textarea1" name="Textarea1" rows="6" cols="20">
    </
    TEXTAREA></P>
                      <P><input type="button" value="Validate" id="Button1" 
    name="Button1" onclick="return Button1_onclick()"></P>
                </form>
          </body>
    </html>

    Within the above code, I mainly created a TEXTAREA (multi-line text box) and a button.  The textbox is named “Textarea1” and the button is named “Button1”.  The button is defined with an “onclick” event which calls a JavaScript function named “Button1_onclick”, which is defined as follows:

    function Button1_onclick() {
    var v = document.all("Textarea1").value;
    alert(isContainSpacesOrTab(v));
    }

    The above function defines a variable “v”, which is assigned with a value available (or typed) in the textarea “Textarea1”.  The same variable is passed as a parameter to another JavaScript function, “isContainSpacesOrTab” (which is defined as follows).  The value returned by the function “isContainSpacesOrTab” is finally displayed in the form of a message box, using an “alert” statement.  Now, let us look into the “isContainSpacesOrTab” function.

    function isContainSpacesOrTab(val){
          if(val==null){return false;}
          if(val.length==0) {return false;}
          for(var i=0;i<val.length;i++) {
                if ((val.charAt(i)==" ") || (val.charAt(i)=="\t")) {return true;}
          }
          return false;     
          }

    The above function simply accepts any value (as a parameter value) into the variable “val”.  The first “if” statement in the function “isContainSpacesOrTab” simply checks for “null”.  The second “if” statement in the same function counts the number of characters available.  If no characters are found, it returns “true”. 

    If any characters are found, we go through each character (using a “for” loop) and check whether the character matches with a space or a tab character (\t).  If any character is found to be a space or tab, we return true.  Finally, if none of the “if” statements work, we return “false”.

    Within the above function, we checked only spaces and tabs.  If we needed to check for “carriage returns”, we would work with the character “\r”.  Similarly, if we needed to check for new line, we would work with the character “\n”.  You can test for these functionalities by modifying very few lines in the above code!

    More JavaScript Articles
    More By Jagadish Chaterjee


       · Hai, I am giving away my simple library of JavaScript routines (which worked the...
       · This is a poor example of validation.Why not use Regular Expression to check...
       · Hai, this is aimed at beginners, not to the professionals (or any experienced) at...
       · RegEx is very slow function. Anybody who has experience with programming will use if...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT