Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 28th, 2008, 12:58 PM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default validate email address

Hi all,
I've got some javascript to validate entries on a webform. It checks to make sure that each required field has something in it. I would like to also validate that the email address at least looks like a valid one. Here's the javascript:

    <script type="text/javascript">
        function checkform(of)
        {
        // Test if DOM is available and there is an element called required
            if(!document.getElementById || !document.createTextNode){return;}
            if(!document.getElementById('required')){return;}

        // Define error messages and split the required fields
            var errorID='errormsg';
            var errorClass='error'
            var errorMsg='Please enter or change the fields marked with a ';
            var errorImg='alert.gif';
            var errorAlt='Error';
            var errorTitle='This field has an error!';
            var reqfields=document.getElementById('required').valu e.split(',');

        // Cleanup old mess
            // if there is an old errormessage field, delete it
            if(document.getElementById(errorID))
            {
                var em=document.getElementById(errorID);
                em.parentNode.removeChild(em);
            }
            // remove old images and classes from the required fields
            for(var i=0;i<reqfields.length;i++)
            {
                var f=document.getElementById(reqfields[i]);
                if(!f){continue;}
                if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
                {
                    f.parentNode.removeChild(f.previousSibling);
                }
                f.className='';
            }
        // loop over required fields
            for(var i=0;i<reqfields.length;i++)
            {
        // check if required field is there
                var f=document.getElementById(reqfields[i]);
                if(!f){continue;}
        // test if the required field has an error,
        // according to its type
                switch(f.type.toLowerCase())
                {
                    case 'text':
                        if(f.value=='' && f.id!='email'){cf_adderr(f)}
        // email is a special field and needs checking
                        if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}
                    break;
                    case 'textarea':
                        if(f.value==''){cf_adderr(f)}
                    break;
                    case 'checkbox':
                        if(!f.checked){cf_adderr(f)}
                    break;
                    case 'select-one':
                        if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}
                    break;
                }
            }
            return !document.getElementById(errorID);

            /* Tool methods */
            function cf_adderr(o)
            {
                // create image, add to and colourise the error fields
                var errorIndicator=document.createElement('img');
                errorIndicator.alt=errorAlt;
                errorIndicator.src=errorImg;
                errorIndicator.title=errorTitle;
                o.className=errorClass;
                o.parentNode.insertBefore(errorIndicator,o);

            // Check if there is no error message
                if(!document.getElementById(errorID))
                {
                // create errormessage and insert before submit button
                    var em=document.createElement('div');
                    em.id=errorID;
                    var newp=document.createElement('p');
                    newp.appendChild(document.createTextNode(errorMsg) )
                    // clone and insert the error image
                    newp.appendChild(errorIndicator.cloneNode(true));
                    em.appendChild(newp);
                    // find the submit button
                    for(var i=0;i<of.getElementsByTagName('input').length;i++)
                    {
                        if(/submit/i.test(of.getElementsByTagName('input')[i].type))
                        {
                            var sb=of.getElementsByTagName('input')[i];
                            break;
                        }
                    }
                    if(sb)
                    {
                        sb.parentNode.insertBefore(em,sb);
                    }
                }
            }
            function cf_isEmailAddr(str)
            {
                     var at="@"
                                var dot="."
                                var lat=str.indexOf(at)
                                var lstr=str.length
                                var ldot=str.indexOf(dot)
                                if (str.indexOf(at)==-1){
                                    return str.match();
                                }

                                if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                                    return str.match();
                                }

                                if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                                    return str.match();
                                }

                                 if (str.indexOf(at,(lat+1))!=-1){
                                    return str.match();
                                }

                                 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                                    return str.match();
                                }

                                 if (str.indexOf(dot,(lat+2))==-1){
                                    return str.match();
                                }

                                 if (str.indexOf(" ")!=-1){
                                    return str.match();
                                }

                      return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
                             ***Note this was the original - didn't work.
                             //return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
            }
        }
    </script>


Thanks in advance!

dartcoach
__________________
dartcoach
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate multiple email addresses mat41 Javascript 1 August 29th, 2007 12:45 AM
How to validate email address crmpicco Javascript How-To 2 February 16th, 2005 10:56 AM
Validate Email address richardtr BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 6 November 23rd, 2004 01:45 PM
Email Validate nskannan2007@yahoo.com Javascript How-To 3 July 3rd, 2003 11:46 PM
Email Validate nskannan2007@yahoo.com HTML Code Clinic 3 June 9th, 2003 10:49 AM



All times are GMT -4. The time now is 11:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.