JavaScript
  Home arrow JavaScript arrow Page 3 - 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 an integer (both positive and negative)


    (Page 3 of 4 )

    In the previous section, we worked on validating a multi-digit number with the following 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;
    }

    Now we need to extend the same function to work with the negative symbol (or hyphen) as well.  Let us look at the following code:

    <html>
          <head>
                <script id="clientEventHandlersJS" language="javascript">
    <!--
    function
    isInteger(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 (i == 0 && ch == "-") {
                continue
                }
          if (ch < "0" || ch > "9") {
                return false
          }
    }
    return
     true
    }

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

    The new function is defined as follows:

    function isInteger(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 (i == 0 && ch == "-") {
                continue
                }
                if (ch < "0" || ch > "9") {
                      return false
                }
    }
    return
     true
    }

    From the above function, the only extra is the following condition:

                if (i == 0 && ch == "-") {
                continue
                }

    The above “if” condition simply ignores the hyphen symbol, if it is found at the first position (or index with 0).  Other than the above modification, the rest of the code should be very familiar.

    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 5 hosted by Hostway
    Stay green...Green IT