Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    confirm email field javascript

    Hi,

    One of my clients has an html form that is validated server-side by the FormMail perl script.

    They would like to add a "confirm email" field after the email field to try to eliminate users who enter their email address incorrectly.

    I was wondering if this is possible to do using javascript to perhaps display an alert box should the users email address not match in both fields, and then send the email and verify using the perl script still after this javascript is validated on the client side?

    Could anyone please let me know about this as soon as possible, and / or if you have an alternative solution please let me know?

    Thanks very much.

    Regards,

    Grant

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <form name = "myform">
    <p> Enter Password (minimum 6 characters): - <input type = "password" name = "password" id = "password" size = "12"> </p>
    <p> Confirm Password: - <input type = "password" name = "conpassword" id = "conpassword" size = "12" onblur = "checkPwd()"> </p>
    </form>

    <script type = "text/javascript">
    function checkPwd() {
    pwd = document.myform.password.value;
    cpwd = document.myform.conpassword.value;
    if ((pwd.length <6) || (pwd != cpwd)) {
    alert ("Password too short or passwords do not match!")
    document.myform.password.value = "";
    document.myform.conpassword.value = "";
    document.myform.password.focus();
    }
    else {
    alert ("Passwords match!")
    }
    }

    </script>

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Philip, not exactly what I'm after but I might be able to figure something out from that.

    I don't want the alert box to appear if the emails do match, I just want the form to be sent.

    However if they don't, I want the alert box to popup and allow the user to change his/her entires.

    Grant

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Grant7 View Post
    Thanks Philip, not exactly what I'm after but I might be able to figure something out from that.

    I don't want the alert box to appear if the emails do match, I just want the form to be sent.

    However if they don't, I want the alert box to popup and allow the user to change his/her entires.

    Grant

    Well, as I see it that is what you have got. You could always delete the lines I put in simply for test purposes, so you could see that it works.

    else {
    alert ("Passwords match!")
    }


    If the passwords do not match then the user gets the message:-

    alert ("Password too short or passwords do not match!")

    You can alter this to:-

    alert ("Password too short or passwords do not match! Please re-enter them.")

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I must apologise - for some stupid reason I mis-read your request as being for password validation and not the email validation/confirm that you actually wanted.

    Try this:-

    <form name = "myform">
    <p> Enter E-Mail Address: - <input type = "text" name = "emailadd" id = "emailadd" size = "42" onblur = "checkValidEmail()"> </p>
    <p> Confirm Email Address: - <input type = "text" name = "conemail" id = "conemail" size = "42" onblur = "checkConfirmEmail()"> </p>
    </form>

    <script type = "text/javascript">

    function checkValidEmail() {
    email1= document.myform.emailadd.value;
    if (/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email1)) {
    alert ("Valid Address"); // simply comment out this line if not required
    }
    else {
    alert ("Invalid Email Address - Please Re-Enter");
    document.myform.emailadd.value = "";
    document.myform.emailadd.focus();
    }
    }

    function checkConfirmEmail() {
    email2 = document.myform.conemail.value;
    if (email2 != email1) {
    alert ("Email addresses do not match - please re-enter!");
    document.myform.emailadd.value = "";
    document.myform.conemail.value = "";
    document.myform.emailadd.focus();
    }
    else { // delete these lines if alert not required.
    alert ("Passwords match!");
    }
    }

    </script>

    Once more, I am sorry for this misunderstanding. Too early on Monday morning!

  • Users who have thanked Philip M for this post:

    Grant7 (09-04-2007)

  • #6
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hehe no worries Philip, thanks that's great.

    I actually had it working from what you wrote, but the last script you wrote is even better, checking the validity of the emails too, so that's awesome.

    Thanks a lot for your help.

    Regards,

    Grant


  •  

    Posting Permissions

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