Home arrow JavaScript arrow Page 3 - Developing a simple validation library in JavaScript: continued
JAVASCRIPT

Developing a simple validation library in JavaScript: continued


This series of articles mainly lists some of the most commonly used JavaScript functions for client side validation of HTML forms. You can reuse these scripts to inject into server side controls easily.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
February 22, 2006
TABLE OF CONTENTS:
  1. · Developing a simple validation library in JavaScript: continued
  2. · Validation for a positive integer (only digits, no symbols)
  3. · Validation for an integer (both positive and negative)
  4. · Validation for a number (both integers and floating point numbers, either positive or negative)

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials