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 / 17
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

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

- 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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials