Home arrow JavaScript arrow Page 4 - 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 / 70
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

Developing a simple validation library in JavaScript - Validation for single digit or single alphabet: another way
(Page 4 of 5 )

This sections deals with an alternative way to accomplish the same task we did in the previous section.  Take a look at the following code: 

<html>
      <head>
            <script id="clientEventHandlersJS" language="javascript">
<!--
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;
      }
function Button1_onclick() {
var v = document.all("txtName").value;
alert(isSingleDigit(v));
}
//-->
            </script>
      </head>
      <body>
            <form id="form1">
                  Enter single digit:<input type="text" id="txtName" NAME="txtName"> <input type="button" value="Validate" id="Button1" 
name="Button1" onclick="return Button1_onclick()">
            </form>
      </body>
</html>



The above code is very similar to the one presented in the previous section, except for 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;
      }

The above function simply accepts any value (as a parameter value) into the variable “val”.  The first “if” statement in the function “isBlank” simply checks for “null”.  The second “if” statement in the same function counts the number of characters available.  If no characters are found or if it finds more than one, it returns “false”.   

The third “if” statement tries to check the first character (the character present at the 0th location) for its existence between “0” and “9”.  The logic hidden behind this range is available from ASCII codes. 

In a similar fashion, to check for a single alphabet, we can rewrite the above function as follows:

function isSingleAlphabet(val){
      if(val==null){return false;}
      if (val.length==0 || val.length>1){return false;}
      if ((val.charAt(0) >= "a") && (val.charAt(0) <= "z") || (val.charAt(0) >= "A") && (val.charAt(0) <= "Z")) {return true;}
      return false;
      }


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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