Home arrow JavaScript arrow Page 5 - 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 email address
(Page 5 of 5 )

There exists several ways to deal with validation of email addresses.  One of the most popular methods involves using “regular expressions”.  The discussion of “regular expressions” is beyond the scope of this article and I suggest you investigate by searching online.

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

<html>
      <head>
            <script id="clientEventHandlersJS" language="javascript">
<!--
function isValidEmail(val){
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!val.match(re)) {
        return false;
    } else {
        return true;
    }
}
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>

Within the above code, I mainly created a simple text box and a button.  The textbox is named “txtEMail” 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("txtEMail").value;
alert(isValidEmail(v));
}

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

function isValidEmail(val){
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!val.match(re)) {
        return false;
    } else {
        return true;
    }
}

The first statement within the above function is the “regular expression” which is used to check for the validity of an email address.  JavaScript contains a method “match” (for a string object) especially to deal with “regular expressions”.

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

- 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