7 Replies Latest reply on Nov 14, 2007 6:34 PM by cf_dev2

    Validate Email

    fs22
      I have a form that has two email fields. One is used to confirm the other. I have validated an email field before, but I have never validated one email field against another. Any thoughts?

      ~Clay
        • 1. Re: Validate Email
          cf_dev2 Level 1
          What do you mean validate one against the other? Ensure they are the same..?
          • 2. Re: Validate Email
            fs22 Level 1
            That is exactly what I mean. My collueage wants to ensure that the email is as accurate as possible. So in addition to the usual email validation in CF, he wants me to have to fields that should equal each other. So I am looking for tips.

            Thanks.
            • 3. Re: Validate Email
              cf_dev2 Level 1
              Just use a simple equality comparison. Probably case insensitive.

              You can use javascript on the client side. Then back it up with CF server side validation. Are you looking for help with the javascript or CF part?


              • 4. Re: Validate Email
                fs22 Level 1
                I would imagine it needs to be done with JS on client side since we want it to check before submission. Can you point me in the right direction please?
                • 5. Re: Validate Email
                  cf_dev2 Level 1
                  Here's a quick and dirty example. There are more elegant ways to write it.

                  <script type="text/javascript">
                  function compareAddresses() {
                  var email1 = document.getElementById('email1').value.replace(/\s/g, '');
                  var email2 = document.getElementById('email2').value.replace(/\s/g, '');
                  if (email1.length == 0 || email2.length == 0) {
                  alert('Please fill in both email addresses!');
                  }
                  else if (email1.toLowerCase() != email2.toLowerCase()) {
                  alert('The addresses are not equal');
                  }
                  }
                  </script>
                  <form>
                  <input type="text" id="email1" name="email1">
                  <input type="text" id="email2" name="email2">
                  <input type="button" onClick="compareAddresses();">
                  </form>
                  • 6. Validate Email
                    SafariTECH Level 1
                    quote:

                    Originally posted by: fs22
                    I would imagine it needs to be done with JS on client side since we want it to check before submission. Can you point me in the right direction please?



                    Doing it with JS is fine, but keep in mind that some people and a certain percentage of businesses have JS turned of as a security precaution, therefore any time you use client side validations, you should always use server-side validations as well.

                    If you detect a problem you simply return the user to the form.


                    • 7. Re: Validate Email
                      cf_dev2 Level 1
                      > You can use javascript on the client side. Then back it up with CF server side validation.

                      I didn't bother to post a CF example since I'm sure you already know how to compare two strings in CF :-)