JavaScript
  Home arrow JavaScript arrow Page 5 - Developing a simple validation library in ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Developing a simple validation library in JavaScript
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 54
    2006-02-15

    Table of Contents:
  • Developing a simple validation library in JavaScript
  • Validations for spaces, tabs, carriage returns, new line characters etc.
  • Validation for single digit or single alphabet
  • Validation for single digit or single alphabet: another way
  • Validation for email address

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Hai, I am giving away my simple library of JavaScript routines (which worked the...
       · This is a poor example of validation.Why not use Regular Expression to check...
       · Hai, this is aimed at beginners, not to the professionals (or any experienced) at...
       · RegEx is very slow function. Anybody who has experience with programming will use if...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT