Home arrow JavaScript arrow Page 2 - Developing a simple validation library in JavaScript
JAVASCRIPT

Developing a simple validation library in JavaScript


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: 4 stars4 stars4 stars4 stars4 stars / 62
February 15, 2006
TABLE OF CONTENTS:
  1. · Developing a simple validation library in JavaScript
  2. · Validations for spaces, tabs, carriage returns, new line characters etc.
  3. · Validation for single digit or single alphabet
  4. · Validation for single digit or single alphabet: another way
  5. · Validation for email address

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Developing a simple validation library in JavaScript - Validations for spaces, tabs, carriage returns, new line characters etc.
(Page 2 of 5 )

Now, let us try to develop a simple script (JavaScript) to validate spaces and tabs.  Take a look at the following code: 

<html>
      <head>
            <script id="clientEventHandlersJS" language="javascript">
<!--
function isContainSpacesOrTab(val){
      if(val==null){return false;}
      if(val.length==0) {return false;}
      for(var i=0;i<val.length;i++) {
            if ((val.charAt(i)==" ") || (val.charAt(i)=="\t")) {return true;}
      }
      return false;    
      }
function Button1_onclick() {
var v = document.all("Textarea1").value;
alert(isContainSpacesOrTab(v));
}
 
//-->
            </script>
      </head>
      <body>
            <form id="form1">
                  <P>Enter Remarks:</P>
                  <P><TEXTAREA id="Textarea1" name="Textarea1" rows="6" cols="20">
</
TEXTAREA></P>
                  <P><input type="button" value="Validate" id="Button1" 
name="Button1" onclick="return Button1_onclick()"></P>
            </form>
      </body>
</html>

Within the above code, I mainly created a TEXTAREA (multi-line text box) and a button.  The textbox is named “Textarea1” and the button is named “Button1”.  The button is defined with an “onclick” event which calls a JavaScript function named “Button1_onclick”, which is defined as follows:

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

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

function isContainSpacesOrTab(val){
      if(val==null){return false;}
      if(val.length==0) {return false;}
      for(var i=0;i<val.length;i++) {
            if ((val.charAt(i)==" ") || (val.charAt(i)=="\t")) {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 “isContainSpacesOrTab” 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 “true”. 

If any characters are found, we go through each character (using a “for” loop) and check whether the character matches with a space or a tab character (\t).  If any character is found to be a space or tab, we return true.  Finally, if none of the “if” statements work, we return “false”.

Within the above function, we checked only spaces and tabs.  If we needed to check for “carriage returns”, we would work with the character “\r”.  Similarly, if we needed to check for new line, we would work with the character “\n”.  You can test for these functionalities by modifying very few lines in the above code!


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