www.webdeveloper.com
Results 1 to 7 of 7

Thread: JavaScript Form Validation - Struggling With Email Validation

  1. #1
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59

    Exclamation JavaScript Form Validation - Struggling With Email Validation

    Hi everybody,

    This is for a university project so I wouldn't like it completing for me. Any tips or hints would be more than welcome though!

    I'm finally getting the feel for form validation but I'm struggling with the email validation part. I've been deleting and swapping bits and pieces for hours now and I can't seem to get it working. Below is the code I've got so far... The "firstname" and "lastname" validation doesn't work with the current code, but when the red text is taken out it works fine. As you may have guessed, the code I'm struggling with has been made red.

    Thanks for any help on this matter; very much appreciated!

    Kind regards,

    Amph.

    Code:
    function validateFormFields()
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    {
    	valid = true;
    
            if (document.frmCreateAcc.firstname.value == "")
            {
                    alert("Please enter your first name.");
                    valid = false;
    				document.forms[0].firstname.focus()
    				return false;
            }
    		if (document.frmCreateAcc.lastname.value == "")
    		{
    				alert("Please enter your last name.");
    				valid = false;
    				document.forms[0].lastname.focus()
    				return false;
    		}
    		with (document.frmCreateAcc.email.value)
      		{
      				if (apos<1||dotpos-apos<2)
        			alert("Please enter a valid email address");
      				valid = false;
      		}
    		else
    				confirm("Thank you for creating an O2 account.");
    				
            return valid;
    }
    Last edited by Amphidamas; 04-23-2009 at 04:56 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    36
    If you are allowed to use regular expressions, that's what I'd do.

    Otherwise, you probably want brackets around your email alert and valid=false. I would check maybe if apos==-1 || dotpos<=apos, but there's probably special cases it wouldn't catch, that's what's great about regex.

  3. #3
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59
    Quote Originally Posted by dingbat View Post
    If you are allowed to use regular expressions, that's what I'd do.

    Otherwise, you probably want brackets around your email alert and valid=false. I would check maybe if apos==-1 || dotpos<=apos, but there's probably special cases it wouldn't catch, that's what's great about regex.
    Regular expressions are extremely complicated to do. Even if I managed to code it, I would then have to incorporate it into my program which I wouldn't know how to do.

    Found this, but don't have a clue how to incorporate it:
    Code:
    emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/

  4. #4
    Join Date
    Mar 2009
    Posts
    36
    Writing a regular expression is complicated, but using them isn't.

    emailRe.test(document.frmCreateAcc.email.value)

    That's all there is to it. Returns a boolean true if it matches.

  5. #5
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59
    What have I done wrong with this?

    EDIT: Reading through it, I'm guessing the RegExp should be "== true" for the test to come back negative? Confusing! But I suspect there is still something else wrong with it, or am I just overcomplicating things?

    Code:
    function validateFormFields()
    {
    	validateEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
    	valid = true;
    
            if (document.frmCreateAcc.firstname.value == "")
            {
                    alert("Please enter your first name.");
                    valid = false;
    				document.forms[0].firstname.focus()
    				return false;
            }
    		if (document.frmCreateAcc.lastname.value == "")
    		{
    				alert("Please enter your last name.");
    				valid = false;
    				document.forms[0].lastname.focus()
    				return false;
    		}
    	    if validateEmail.test(document.frmCreateAcc.email.value == false)
      		{
      				alert("Please enter a valid email address");
    				valid = false;
      		}
    		else
    				confirm("Thank you for creating an O2 account.");
    				
            return valid;
    }
    Last edited by Amphidamas; 04-23-2009 at 05:54 PM.

  6. #6
    Join Date
    Mar 2009
    Posts
    36
    Missing a parenthesis.

    if (validateEmail.test(document.frmCreateAcc.email.value) == false)

    If you aren't using it already, the Firebug plugin for Firefox is a great tool for debugging Javascript errors.

  7. #7
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59
    Quote Originally Posted by dingbat View Post
    Missing a parenthesis.

    if (validateEmail.test(document.frmCreateAcc.email.value) == false)

    If you aren't using it already, the Firebug plugin for Firefox is a great tool for debugging Javascript errors.
    It's ALWAYS the smallest of mistakes! Even though it's right under my nose, I still can't see it! Lol. Working perfect now, thanks m8!

    EDIT: I will install that plugin now. Thanks very much for the tip!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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