INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Validation

Non-RegExp Email Validation Script by BillyRayPreachersSon
Posted: 21 Feb 05 (Edited 27 Jun 07)

If you are looking for an email validation script that doesn't require RegExp (and would therefore be easier to understand if you don't know RegExp very well, like myself), consider something like this:

CODE

function validateEmailAddress(emailToValidate) {
    if (emailToValidate == '' || typeof(emailToValidate) == 'undefined') return(false);
    emailToValidate = stringTrim(emailToValidate);
    
    var emailLength = emailToValidate.length;
    var atPos = emailToValidate.indexOf('@');
    var firstDotPos = emailToValidate.indexOf('.');
    var lastDotPos = emailToValidate.lastIndexOf('.');
    
    if (emailLength < 7) return(false);                // minimum email address length ("x@xx.xx")
    if (atPos < 1) return(false);                    // traps for no @, or @ as first character
    if (firstDotPos < 1) return(false);                // traps for no ., or . as first character
    if (atPos == emailLength-1) return(false);        // traps for @ as penultimate or last character
    if (lastDotPos >= emailLength-2) return(false);    // traps for too few digits after last . (must be at least 2)
    // traps for @ character next to . character
    if (emailToValidate.charAt(atPos-1) == '.' || emailToValidate.charAt(atPos+1) == '.') return(false);
    return(true);            
}

I just find this sort of thing much easier to (a) understand and (b) modify without RegExp knowledge.

It may not be 100% effective against all email addresses, but will probably trap 99% of common mistakes (much like the RegExp version, IMHO).

Dan


Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch
http://www.codecouch.com/dan/

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close