Custom Web Software Development for the 21st CenturySM

PHP, SQLs, JavaScript, Ajax, HTML5, XHTML, & CSS3: Innovative Enterprise level Scripting for interactive sites, SaaS, & cross-platform desktop apps
  • Home
  • Automotive Dealer Website Development
  • Free software: PHP and JavaScript
  • PHP Rainbow-Maker
  • JavaScript MasterColorPicker
  • UniDOM.js x-browser solution +
  • JavaScript <input type="picker">
  • FormFieldGenie Text-Input Auto-Pop-Up
  • JavaScript <input type="range">
  • Form Input eMail Validation Script
  • PHP eMail Validation Script
  • PHP Utility Functions
  • Web Developer Resources
  • Form eMail Input Validation

    This JavaScript function validates the format of an eMail address for use in conjunction with an HTML form.  It allows almost all valid formats with the following exceptions:  (a) it does not allow comments in the address (b) it does not allow backslash escaped quotes (c) it uses the old specifications for legal domain names.  We felt that allowing comments & backslash escaped quotes were extreme situations that were unnecessary to support for most uses, especially as it overly-complicates the Perl compatible regular expression used to validate the email address.  This was developed before the official specifications for domain names was changed to allow international characters; but we have not updated it because it works better this way with most English speaking websites.  We do plan on re-formulating the regular expression to allow international domain names, given time; for now we offer the more restrictive version.  Note, though, that it does allow quoted international characters in the domain name, per the old specifications.

    Below is a list of some examples of the formats this function will validate.  There are many combinations not shown in these examples, but you should be able to interpolate them.  In addition, it will validate a list of email addresses, separated with commas.  It returns true if the email address(es) are valid and false if not.  It also creates a JavaScript alert (popup box) to tell the user the email did not validate.  The example code shows how to use this function in two different ways with an HTML form.

    It’s implementation is simple.  It may be linked into the form control and the form submit as shown.  Try the example form input; there is no submit button, but you will be hounded with alerts until you type in an email with a valid format.  Note it does not verify if the email address exists, only if it has a valid format; the companion PHP function for email validation we offer can take the many formats this JavaScript function validates, pull out the active address, and then using the PHP Class by Manuel Lemos you can verify if it is truly active. See our PHP email validation page for more information and links and to download our source code.

    <script type="text/javascript">
    function checkEmail(eadd) {
    eadd=eadd.replace(/^[\s]+/, ""); eadd=eadd.replace(/[\s]+$/, "");
    if (eadd=="") {alert('Please enter an eMail address'); return false;}
    var literal="[\x2D^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+";
    var quoted='"[\x20-\x7F]+"';
    var word="("+literal+"|"+quoted+")";
    var domn="([-0-9A-Z_a-z]+|"+quoted+")";
    var emailAdd=word+"([.]"+word+")*@("+domn+"[.])+[a-zA-Z]{2,6}";
    var litName="[\x2D\x20^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+";
    var quoName='"[\x00-\x09\x0E-\x21\u0023-\uFFFF]+"[\x20\t]*';
    var emailFormat="("+emailAdd+"|("+litName+"|"+quoName+")<"+emailAdd+">)";
    var emailList=emailFormat+"(,[\x20\t\n]*"+emailFormat+")*";
    var emailValid=new RegExp("^"+emailList+"$");
    if (emailValid.test(eadd)) return true;
    else {
    alert('The eMail address you entered has an invalid format. Please check it and try again');
    return false; } }

    <form action="SoftMoon.checkEmail-js_instructions.htm#content"
     onsubmit="return checkEmail(document.getElementById('emailInput').value);">
    <label>Please enter a valid email address
    <input type='text' name='emailInput' id='emailInput' maxlength='255' size='37'
     onblur='if (!checkEmail(this.value)) this.focus();' /></label>