Thursday, September 8, 2011

How to Add Confirmation email field in magento registration form using class validate-cemail?

The following method allows you to add confirmation email address field in your customer registration or newsletter page.
Create a javascript file called validation.js under "js/email/validation.js"
Validation.addAllThese([

    ['validate-cemail', 'Please make sure your emails match.', function(v) {
                var conf = $('confirmation') ? $('confirmation') : $$('.validate-cemail')[0];
                var pass = false;
  var confirm;
                if ($('email')) {
                    pass = $('email');
                }
  confirm =conf.value;
  if(!confirm && $('email2'))
  {
  confirm = $('email2').value;
  }
                return (pass.value == confirm);
            }],
]); 
Add the new js file into your customer.xml file

    
 
On the register form add a new field to contain the email confirmation field "template/customer/form/register.phtml".

Thats all. You are done.

2 comments:

  1. Hi,

    Was looking for a fix for email entries and found your article thanks!

    But, tried it and it did not work?

    Even though the EXACT email was entered in each email field, it still said they were incorrect?

    Any ideas?

    Couple of other points:

    1 - You forgot to mention to add the following to the head text to call the js script:
    (This form would not let me show you)

    2 - Which Customer.xml file do you add this content too, the admin or frontend version? There is one in
    app/design/adminhtml/default/default/layout/customer.xml and also
    app/design/frontend/base/default/layout/customer.xml

    ?

    3 - You also say add this in the XML:

    email/validation.js

    but do we need to call the URL as:
    email/validation.js or js/email/validation.js
    ?

    Hope you can help as this would prove a great Magento addon for all!

    Many thanks - PJ

    ReplyDelete
  2. the reason for the validation not working is simple:
    The elements are identified by ID not name: the original email field you are comparing against has the id 'email_address', not 'email'.
    So I suggest sou change validation.js and change all 'email' to 'email_address', and 'email2' to 'email_address2'.
    Then, accordingly,the id in register.phtml has to read id="email_address2".

    Enjoy

    ReplyDelete