Home arrow JavaScript arrow Page 4 - 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 a number (both integers and floating point numbers, either positive or negative)
(Page 4 of 4 )

In the previous sections, we worked on validating multi-digit numbers.  Now, it is time to work with floating point numbers (or real numbers).  Let us look at the following code:

<html>
      <head>
            <script id="clientEventHandlersJS" language="javascript">
<!--
function
isValidNumber(val){
      if(val==null){return false;}
      if (val.length==0){return false;}
      var DecimalFound = false
      for (var i = 0; i < val.length; i++) {
            var ch = val.charAt(i)
            if (i == 0 && ch == "-") {
                  continue
            }
            if (ch == "." && !DecimalFound) {
                  DecimalFound = true
                  continue
            }
            if (ch < "0" || ch > "9") {
                  return false
            }
      }
      return true
}
function
Button1_onclick() {
var
v = document.all("txtNumber").value;
alert(isValidNumber(v));
}
//-->
            </script>
      </head>
      <body>
            <form id="form1">
                  Enter Number:<input type="text" id="txtNumber" NAME="txtNumber"> <input type="button" value="Validate" id="Button1" 
name="Button1" onclick
="return Button1_onclick()">
            </form>
      </body>
</html>

The new function from above is defined as follows:

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

The above function is slightly different from the functions in the previous sections.  In the case of a negative number, we need to test for the negative symbol (or hyphen) only at the first position as follows:

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

But the decimal point could occur at any location, and that too can occur only once.  As I need to test for a “once occurrence”, I declared a flag variable (“DecimalFound” of type Boolean).  Initially, I start with “DecimalFound” as false (which means that no decimal is found).  I update it to “true” once it finds the decimal point.  If it again finds a decimal point (when “DecimalFound” is true), it returns false automatically based on the logic defined above.

If all of the functions in this series are kept in a single JavaScript file, we can even reuse the functions within another.  This reduces the code and maintenance.  But you need to keep in mind that unnecessary code also gets emitted if you don’t use all the functions in your web page.

Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials