JavaScript
  Home arrow JavaScript arrow Page 4 - 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 
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 / 60
    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 - Validation for single digit or single alphabet: another way


    (Page 4 of 5 )

    This sections deals with an alternative way to accomplish the same task we did in the previous section.  Take a look at the following code: 

    <html>
          <head>
                <script id="clientEventHandlersJS" language="javascript">
    <!--
    function isSingleDigit(val){
          if(val==null){return false;}
          if (val.length==0 || val.length>1){return false;}
          if ((val.charAt(0) >= "0") && (val.charAt(0) <= "9")) {return true;}
          return false;
          }
    function Button1_onclick() {
    var v = document.all("txtName").value;
    alert(isSingleDigit(v));
    }
    //-->
                </script>
          </head>
          <body>
                <form id="form1">
                      Enter single digit:<input type="text" id="txtName" NAME="txtName"> <input type="button" value="Validate" id="Button1" 
    name="Button1" onclick="return Button1_onclick()">
                </form>
          </body>
    </html>



    The above code is very similar to the one presented in the previous section, except for the following function.

    function isSingleDigit(val){
          if(val==null){return false;}
          if (val.length==0 || val.length>1){return false;}
          if ((val.charAt(0) >= "0") && (val.charAt(0) <= "9")) {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 “isBlank” simply checks for “null”.  The second “if” statement in the same function counts the number of characters available.  If no characters are found or if it finds more than one, it returns “false”.   

    The third “if” statement tries to check the first character (the character present at the 0th location) for its existence between “0” and “9”.  The logic hidden behind this range is available from ASCII codes. 

    In a similar fashion, to check for a single alphabet, we can rewrite the above function as follows:

    function isSingleAlphabet(val){
          if(val==null){return false;}
          if (val.length==0 || val.length>1){return false;}
          if ((val.charAt(0) >= "a") && (val.charAt(0) <= "z") || (val.charAt(0) >= "A") && (val.charAt(0) <= "Z")) {return true;}
          return false;
          }

    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

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek