www.webdeveloper.com
Results 1 to 3 of 3

Thread: Function stops working with 2nd if statement, works fine without...

  1. #1
    Join Date
    May 2012
    Posts
    1

    Function stops working with 2nd if statement, works fine without...

    Hey guys, so I'm trying to make a single function that will check to make sure the e-mails in both fields match AND to make sure the e-mail is in proper format. Strangely, with both if statements included the form will return no messages at all. If I comment out the if statement checking for proper format, the if statement to check for matching e-mail works just fine.

    I want this all to be in one function because I'm wanting it to be executed onSubmit with the form. Let me know what you guys think, any help is greatly appreciated:

    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    	function verifyEmail()
    	{
    			var x = document.forms["emailForm"]["enterEmail"].value;
    			var y = document.forms["emailForm"]["confirmation"].value;
    			var atpos = x.indexOf("@");
    			var dotpos = x.lastIndexOf(".");
    			
    				if (x != y) {
    					  var msg = "The email addresses entered do not match, please enter matching email addresses";
    					  document.getElementById("error").innerHTML = msg;
    					  return false;
    				}
    			
    				if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 > = x.length) {
    					  var msg = "Improper e-mail format";
    					  document.getElementById("error").innerHTML = msg;
    					  return false;
    				}
    				
    				else {
    					return;
    				}
    	}
    </script>
    </head>
    
    <body>
    
    	<form name="emailForm" onsubmit="return verifyEmail();" method="post">
    		E-Mail Address: <input type="text" name="enterEmail">
    		Confirm E-Mail Address: <input type="text" name="confirmation">
    		<input type="submit" value="Submit"> <span id="error"></span>
    	</form>
    	
    </body>
    
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    no space needed

    Code:
    Inline script compilation
    Syntax error at line 15 while loading: expected expression, got '='
    + 2 || dotpos + 2 > = x.length) {
    --------------------^
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Exclamation

    Although it is not strickly necessary, it always helps when you can decode the logic without a lot of effort:

    Code:
    	if ( (atpos < 1) || (dotpos < (atpos + 2)) || ((dotpos + 2) >= x.length) ) {
    ...

Thread Information

Users Browsing this Thread

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

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