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: continued
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2006-02-22

    Table of Contents:
  • Developing a simple validation library in JavaScript: continued
  • Validation for a positive integer (only digits, no symbols)
  • Validation for an integer (both positive and negative)
  • Validation for a number (both integers and floating point numbers, either positive or negative)

  • 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: continued - Validation for a positive integer (only digits, no symbols)


    (Page 2 of 4 )

    In my previous article, we worked on single digit validation with 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;
          }

    Now we need to extend the same function to work with more than one digit.  Let us look at the following code:

    <html>
          <head>
                <script id="clientEventHandlersJS" language="javascript">

    <!--
    function
    isPositiveInteger(val){
          if(val==null){return false;}
          if (val.length==0){return false;}
          for (var i = 0; i < val.length; i++) {
                var ch = val.charAt(i)
                if (ch < "0" || ch > "9") {
                return false
                }
          }
          return true;
    }

    function
     Button1_onclick() {
    var
    v = document.all("txtPositiveInteger").value;
    alert(isPositiveInteger(v));
    }
    //-->
                </script>
          </head>
          <body>
                <form id="form1">
                      Enter Positive Integer:<input type="text" id="txtPositiveInteger"
     NAME="txtPositiveInteger"> <input type="button" value="Validate" id="Button1"
     name="Button1" onclick
    ="return Button1_onclick()">
                </form>
          </body>
    </html>

    Within the above code, I mainly created a simple text box and a button.  The textbox is named “txtPositiveInteger” and the button is named “Button1”.  The button is defined with an “onclick” event which calls a JavaScript function, “Button1_onclick”, which is defined as follows:

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

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

    function isPositiveInteger(val){
          if(val==null){return false;}
          if (val.length==0){return false;}
          for (var i = 0; i < val.length; i++) {
                var ch = val.charAt(i)
                if (ch < "0" || ch > "9") {
                return false
                }
          }
          return true;
    }

    The above function simply accepts any value (as a parameter value) into the variable “val”.  The first “if” statement in the function “isPositiveInteger” 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 “false”. 

    If any characters are found, we go through each character (using a “for” loop) and check whether the character is NOT within the range of “0” through “9” (based on ASCII).  If any character is found, we return false.  Finally, we return “true” if no other character apart from digits is found.

    More JavaScript Articles
    More By Jagadish Chaterjee


       · Hi guys, enjoy the second part of my series. Do let me know your ideas. thanks
     

    JAVASCRIPT ARTICLES

    - 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...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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