Home arrow JavaScript arrow 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
(Page 1 of 4 )

All of the examples in this series can be directly tested by simply copying and pasting the entire code (of each section) into any text file with the extension .HTM, then opening it using a browser.

Validation for email addresses – another way

There exist several ways to deal with validation of email addresses.  One of the most popular methods is using “regular expressions” (which I already discussed in my previous article).  In this section, we shall deal with manual validation of email addresses.

Now, let us try to develop a simple script (JavaScript) to validate email addresses.  Have a look at the following code:

<html>
      <head>
            <script id="clientEventHandlersJS" language="javascript">
<!--
function
isValidEmail(val){
    val = val.toLowerCase( );
    if (val.indexOf("@") > 1) {
        var addr = val.substring(0, val.indexOf("@"));
        var domain = val.substring(val.indexOf("@") + 1, val.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length - 1 && oneChar == 46)) {
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123))
{
                continue;
            } else {
                return false;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || 
                ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46))
{
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123))
{
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
    return false;
}

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

From the above heavy JavaScript, you should now conclude that using “regular expression” (as specified in my previous article) is the best simple method.


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