Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento age verification on create account page using javascript

Use following script in your create account page for age verification of new user before creating new account of user.

<script>
function check_dob()
{
    var month = document.getElementById('month').value;
    var day = document.getElementById('day').value;
    var year = document.getElementById('year').value;
    var dbDate = year+'-'+month+'-'+day;
    var today = new Date();
    var birthDate = new Date(dbDate);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
    }

    if(age<=20)
    {
        alert("You are under "+age+" Year")
    }
}
</script>

Please support us, Like us on Facebook.

  1. Good post. Helping developers in coding. This sort of post will encourage the beginners to learn more and question more.Expecting this kind of more post to learn.

    ReplyDelete
  2. How i can call this script before customer is registered?

    ReplyDelete
    Replies
    1. You need to call "check_dob()" function in your submit button and it will return the age of customer.

      Thanks,

      Delete
    2. What registration page do I need to put this on? How do you call the function from the button?

      Delete
    3. IF you want verify customer DOB before register you need to call this function in your register page
      "app/design/frontend/default/[Your theme]/template/customer/form/register.phtml"

      put this script at bottom of this page and call this function in submit button as following. Button is already there at line near by 179.

      just need to add onclick="check_dob();" in this button code.

      Delete
    4. Hi I am just using the default theme and when I go to default/default/template there seems to be a load of folders missing including the customer one.
      When I go to default/base/template I can then find the rest of the files and folders. I tried adding the code to the files in the base directory but nothing seems to happen.

      Should I copy the missing folders that are in the base directory to the default one?

      Delete
    5. Make sure your register page is not comming from "persistent/customer/form/register.phtml"

      Delete
  3. This gets me the alert box properly on the account registration page, but that's all it does. How can I stop the user from trying to create an account if they're underage?

    I've tried using this in the IF statement:

    window.location.replace("http://www.somewebsite.com");
    return false;

    However it fails to redirect the user to whatever website I put in there, nor does it stop Magento from creating the account. The user still sees the confirmation page after you hit "Ok" on the alert box.

    ReplyDelete
    Replies
    1. < function check_dob()
      {
      var month = document.getElementById('month').value;
      var day = document.getElementById('day').value;
      var year = document.getElementById('year').value;
      var dbDate = year+'-'+month+'-'+day;
      var today = new Date();
      var birthDate = new Date(dbDate);
      var age = today.getFullYear() - birthDate.getFullYear();
      var m = today.getMonth() - birthDate.getMonth();
      if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
      age--;
      }
      **********************************************
      if(age<=17)

      {
      alert("You are "+age+"! years old. You must be 18 years or older to use blahblah.com")

      return false;
      }
      else
      {
      return true;

      }
      }
      ***********************************

      and in the button put


      < button ************* onclick="return check_dob()" *********** type="submit" title="< ?php echo $this->__('Submit') ?>" class="button">< span>< span>< ?php echo $this->__('Submit') ?>< /span>< /span>< /button>

      did some Googleing on function statments. this help just put the thing in between the "***" in your registration.phtml located in /app/design/frontend/base/default/template/persistent/customer/form

      Delete

 

Copyright @ 2017 HKBlog.