www.webdeveloper.com
Results 1 to 4 of 4

Thread: Confirm Email Address

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Posts
    8

    Confirm Email Address

    I have the following but the validation isn't working any ideas? I basically want it to validate the email and then validate the confirm email address. The confirm email address should match the email address input. Thanks.

    Code:
    function checkData() {
    warn = "";
    	df = document.buytickets;
    	errorLayer = document.getElementById("error");
    if(df.emailAddress.value != "") {
    		warn += "Email Address\n";
    		}
    if(email == confirmEmail) {
    		warn += "Confirm Email Address\n";
    		}
    if(warn == "") { return true; }
    	else {
    		warn = "Please correct the following:\n\n" + warn;
    		errorLayer.innerHTML = warn;
    		return false;
    		}
    	}
    HTML Code:
    <form name="buytickets" id="buytickets" onsubmit="return checkData()">
    <input type="text" id="emailAddress" />
    <input type="text" id="confirmEmailAddress"/>
    </form>
    <div id="error"></div>

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    You have the logic reversed.

    Code:
    if(df.emailAddress.value != "") {
    		warn += "Email Address\n";
    		}
    This is saying "If the email address field is NOT blank, throw a warning."

    Code:
    if(email == confirmEmail) {
    		warn += "Confirm Email Address\n";
    		}
    This is saying "If the email and confirm fields match, throw a warning."

    Code:
    function checkData() {
    warn = "";
    	df = document.buytickets;
    	errorLayer = document.getElementById("error");
    if(df.emailAddress.value == "") {
    		warn += "Email Address\n";
    		}
    if(df.emailAddress.value != df.confirmEmail.value) {
    		warn += "Confirm Email Address does not match Email Address\n";
    		}
    if(warn == "") { return true; }
    	else {
    		warn = "Please correct the following:\n\n" + warn;
    		errorLayer.innerHTML = warn;
    		return false;
    		}
    	}
    This is how it should look.

    ^_^

    PS.. looking at it, since the error message is not being displayed in an alert box, replace all instances of "\n" with "<br />".

    PPS.. also, all this does is check for SOMETHING in the fields. If you want to make sure that the email address is in a particular format (user@domain.com), then we can use Regular Expressions for a mask.
    Last edited by WolfShade; 03-03-2011 at 08:51 AM.

  3. #3
    Join Date
    Feb 2011
    Posts
    8

    Thanks, question regarding regular expressions

    Thanks Wolfshade, I corrected the javascript and it now works. I would like to apply regular expressions but I am unsure about the syntax. Please could you provide an example...such as the email field:

    Code:
    if(df.emailAddress.value != "") {
    		warn += "Email Address\n";
    		}
    Thanks again

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    I sent you a PM with some RegEx masks for validation.

    For email validation, I usually do the following:
    Code:
    ...
    			if(df.emailTo.value.length < 6) {
    				warn += "Please enter a valid TO: email address.\n";				}
    			else {
    				if(!checkEmail(df.emailTo.value)) { // Exclamation mark means 'not', 'false', or just plain 'didnt'
    					warn += "Please make sure TO: email address is valid.\n";					}
    				}
    ...
    And then I'll have another function for checking the format:
    Code:
    function checkEmail(inputvalue){	
        var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        if(pattern.test(inputvalue)){         
    		return true;   
        }else{   
    		return false; 
        }
    }
    ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles