CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Double confirmation of e-mail address
Pages: [1]

Author Topic: Double confirmation of e-mail address  (Read 2899 times)

Simon147

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 3
  • CiviCRM version: 4.4.6
  • CMS version: Drupal 7.30
  • MySQL version: 5.0.92
  • PHP version: 5.2.9
Double confirmation of e-mail address
December 11, 2011, 04:52:02 am
Hey all,

Since the  e-mail address  is often the most important information collected from a contact, many organisations ask to enter it twice, thus  drastically reducing  the chance that an e-mail address contains typos.

Does anything like that exist in CiviCRM or could it be set up easily somehow?

Simon
« Last Edit: December 11, 2011, 04:59:42 am by Simon147 »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Double confirmation of e-mail address
December 11, 2011, 07:58:40 am

You can do this via customized templates and jQuery

check:

http://en.flossmanuals.net/civicrm-developer-guide/

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Double confirmation of e-mail address
August 17, 2012, 07:52:14 am
I have done what Lobo suggested, and produced some jQuery code which, when executed on a page containing a CiviCRM profile's create or edit form, will duplicate any email entry fields and will not let the form be submitted until the fields match.

Just put the following code in <CustomTemplatesDir>/CRM/Profile/Form/Edit.extra.tpl, then try out a profile create or edit form, e.g. /civicrm/profile/create?reset=1&gid=2. You should see that any email fields have been doubled up.

Code: [Select]
{literal}
<script type="text/javascript">
var emailMismatchErrorMessage = "Email address fields must match.";

// Check that a form's email fields match.
function compareEmailFields() {
var allEmailsMatch = true;

cj("div.crm-section[class*=email-]", this).not(".email-section-clone").each(function () {
var emailRowElems = cj(this).next(".email-section-clone").andSelf();
if (cj("[type=text]", emailRowElems.first()).val() != cj("[type=text]", emailRowElems.last()).val()) {
// Mismatch: Add error indicators (if not present already).
cj("[type=text]", emailRowElems).parent().not(":has(span.crm-error:contains(" + emailMismatchErrorMessage + "))").append(" <span class=\"crm-error\">" + emailMismatchErrorMessage + "</span>");
cj("label", emailRowElems).not(":has(span.crm-error)").wrapInner("<span class=\"crm-error crm-error-label\" />");
allEmailsMatch = false;
} else {
// Match: Remove error indicators (except any needed for other, unrelated errors).
cj("[type=text]", emailRowElems).nextAll("span.crm-error:contains(" + emailMismatchErrorMessage + ")").remove();
cj("label span.crm-error", emailRowElems.not(":has([type=text] ~ span.crm-error)")).contents().unwrap();
}
});

// If all values match, delete the clone fields so that they don't get submitted with the form.
if (allEmailsMatch) cj("div.email-section-clone", this).remove();

return allEmailsMatch;
}

cj(function () {
cj("form#Edit, form#Register").each(function () {
// Clone all email fields.
cj("div.crm-section[class*=email-]", this).each(function () {
var cloneEmailRowElem = cj(this).clone();

// Differentiate the cloned field.
cj("label:not(:has(span.crm-error)), label span.crm-error", cloneEmailRowElem).prepend("Retype ");
cloneEmailRowElem.addClass("email-section-clone");
cj.each(["id", "name", "for"], function (index, attr) {
cloneEmailRowElem.filter("[" + attr + "]").add(cj("[" + attr + "]", cloneEmailRowElem)).attr(attr, function (index, attr) {
return attr + "-clone";
});
});

cloneEmailRowElem.insertAfter(this); // Insert the cloned field after the original.
});

// Add event handler to the form to check email fields on submission.
cj(this).submit(compareEmailFields);

// If the form's submit button has an onclick attribute, that could bypass the form's submit handler, so insert the check into it.
var formId = this.id;
cj("input[type=submit][onclick]", this).attr("onclick", function (index, attr) {
return "if (!compareEmailFields.call(document.getElementById('" + formId + "'))) return false; " + attr;
});
});
});
</script>
{/literal}

I have tried to make the code fairly resilient and fit in with the way that CiviCRM displays fields and indicates input errors on profile forms. I have tested it on Drupal 7.15 and CiviCRM 4.2, in Chrome, FireFox and IE9.

EDIT: It now works for built-in profile pages, embedded profile HTML snippets and event registration forms. It works when there are multiple email fields in a form, and when there are multiple profile forms on a page.

I think this code could be quite useful for other people. I don't have the time to take it further, but developers are welcome to take it as is, improve on it and integrate it into CiviCRM.
« Last Edit: August 29, 2012, 01:28:26 pm by kirk »

Simon147

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 3
  • CiviCRM version: 4.4.6
  • CMS version: Drupal 7.30
  • MySQL version: 5.0.92
  • PHP version: 5.2.9
Re: Double confirmation of e-mail address
August 25, 2012, 12:37:14 pm
Hello Kirk,

Thanks a lot for your work on that. I've tested it as you describe (not sure I got everything right though).

1. I've added a file called "Edit.extra.tpl" to [path_to_drupal]/sites/all/modules/civicrm/templates/CRM/Profile/Form
2. I've put your code into that file

If then I go to [my_site.com]/civicrm/profile/create?reset=1&gid=2 I see the double form.

What I couldn't figure it is how to make that double-check form appear on an event registration page. I've tried to add it to templates/CRM/Event/Form/Registration/ as Register.extra.tpl, but that didn't affect the event registration form. Any idea?

Thanks again for your great work.

S.

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Double confirmation of e-mail address
August 29, 2012, 12:47:13 pm
Hi Simon,

Thanks for your feedback. The reason it wasn't working for event registration forms is that they use different HTML to built-in profile pages. I have rewritten the code above so that it now works for event registration forms and profile HTML snippets as well as for built-in profile pages.

For each type of page where you need double-entry of email addresses (e.g. built-in profile pages, pages with embedded profile HTML snippets, event registration pages, etc) the code needs to be in a different place. So, instead of copying the above code to several locations, I recommend putting the JavaScript code into a file of its own where it can be included from anywhere. I've put mine in <CustomPhpDir>/js/jquery/jquery.emaildoubleentry.js. I've then included that JavaScript file from <CustomTemplatesDir>/CRM/Profile/Form/Edit.extra.tpl for built-in profile pages, from <CustomTemplatesDir>/CRM/Event/Form/Registration/Register.extra.tpl for event registration forms, and at the bottom of any page that contains a profile HTML snippet.

Kirk
« Last Edit: August 29, 2012, 01:21:56 pm by kirk »

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Double confirmation of e-mail address
August 30, 2012, 04:06:46 am
FWIW although my solution seems to work pretty well, I don't think that JavaScript is the best way to enforce double entry of email addresses. All other form validation in CiviCRM is done after the form post, on the server side. That is more robust, and ideally I think that double entry of email addresses should work that way too.

One way that this could be controlled would be to have a checkbox in a profile's Advanced Settings called something like "Require double entry of email addresses?" Ticking this checkbox would cause all email fields on the profile's create and update forms to double up, and their values to be compared as part of the server-side form validation.

It would be great if such a feature could be added to a future release of CiviCRM but that would be more involved than my JavaScript solution and I don't have time to do it at the moment.

thaeli

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.4
Re: Double confirmation of e-mail address
January 31, 2014, 11:07:24 pm
Here's a quick update of this code to work on the Registration form in Civi 4.4 - I'm sure with a little more polish it could be made to work on the other fields too, Registration is just the one I needed it on.

Code: [Select]
{literal}
<script type="text/javascript">
var emailMismatchErrorMessage = "Email address fields must match.";

// Check that a form's email fields match.
function compareEmailFields() {
        var allEmailsMatch = true;

        cj("div.crm-section[class*=email-]").not(".email-section-clone").each(function () {
                var emailRowElems = cj(this).next(".email-section-clone").andSelf();
                if (cj("[type=text]", emailRowElems.first()).val() != cj("[type=text]", emailRowElems.last()).val()) {
                        // Mismatch: Add error indicators (if not present already).
                        cj("[type=text]", emailRowElems).parent().not(":has(span.crm-error:contains(" + emailMismatchErrorMessage + "))").append(" <span class=\"crm-error\">" + emailMismatchErrorMessage + "</span>");
                        cj("label", emailRowElems).not(":has(span.crm-error)").wrapInner("<span class=\"crm-error crm-error-label\" />");
                        allEmailsMatch = false;
                } else {
                        // Match: Remove error indicators (except any needed for other, unrelated errors).
                        cj("[type=text]", emailRowElems).nextAll("span.crm-error:contains(" + emailMismatchErrorMessage + ")").remove();
                        cj("label span.crm-error", emailRowElems.not(":has([type=text] ~ span.crm-error)")).contents().unwrap();
                }
        });
        return allEmailsMatch;
}

function compareEmailFieldsForSubmit() {
        var allEmailsMatch = compareEmailFields();
        // If all values match, delete the clone fields so that they don't get submitted with the form.
        if (allEmailsMatch) {
                cj("div.email-section-clone").remove();
                cj('.form-submit').enable();
        }
        return allEmailsMatch;
}

cj(window).load(function () {
        cj("form#Register").each(function () {
                // Clone all email fields.
                cj("div.crm-section[class*=email-]", this).each(function () {
                        var cloneEmailRowElem = cj(this).clone();
                        cj(this).find("input").focus(function(){cj('.form-submit').enable()});

                        // Differentiate the cloned field.
                        cj("label:not(:has(span.crm-error)), label span.crm-error", cloneEmailRowElem).prepend("Retype ");
                        cloneEmailRowElem.addClass("email-section-clone");
                        cloneEmailRowElem.find("input").val("");
                        cloneEmailRowElem.find("input").focus(function(){cj('.form-submit').enable()});
                        cj.each(["id", "name", "for"], function (index, attr) {
                                cloneEmailRowElem.filter("[" + attr + "]").add(cj("[" + attr + "]", cloneEmailRowElem)).attr(attr, function (index, attr) {
                                        return attr + "-clone";
                                });
                        });

                        cloneEmailRowElem.insertAfter(this); // Insert the cloned field after the original.
                });
        });
cj('form#Register').submit(compareEmailFieldsForSubmit);
});
</script>
{/literal}

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Double confirmation of e-mail address

This forum was archived on 2017-11-26.