Post by keithdarr » Sat Aug 03, 2024 12:50 am

I need to have users input their birthday on the online checkout form. This needs to verify that they are 21. I have tried to look for plug-ins that do this but haven't seen anything for OpenCart 4. Can someone help me out here?
Last edited by keithdarr on Mon Aug 05, 2024 11:53 pm, edited 1 time in total.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 1:16 am

I need to store their birthdate including with their user address. I can't have anonymous checkouts (guests).

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 2:15 am

I am required to get their address, phone number, email address, and date of birth for all online orders. So I need those fields recorded in the registration page. I figured out Customer->Custom Field so I can probably use that to do a phone number, email address, and date of birth. But there needs to be age verification.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 4:35 am

So far I have been able to get the fields to show up on the registration page. I edited catalog/view/template/account/register.twig

<!-- Added these fields inside the registration form to make the fields -->

Code: Select all

<div class="row mb-3 required">
    <label for="input-address" class="col-sm-2 col-form-label">{{ entry_address }}</label>
    <div class="col-sm-10">
        <input type="text" name="address" value="{{ address }}" placeholder="{{ entry_address }}" id="input-address" class="form-control" />
        {% if error_address %}
            <div class="text-danger">{{ error_address }}</div>
        {% endif %}
    </div>
</div>
<div class="row mb-3 required">
    <label for="input-phone" class="col-sm-2 col-form-label">{{ entry_phone }}</label>
    <div class="col-sm-10">
        <input type="text" name="phone" value="{{ phone }}" placeholder="{{ entry_phone }}" id="input-phone" class="form-control" />
        {% if error_phone %}
            <div class="text-danger">{{ error_phone }}</div>
        {% endif %}
    </div>
</div>
<div class="row mb-3 required">
    <label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
    <div class="col-sm-10">
        <input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control" />
        {% if error_dob %}
            <div class="text-danger">{{ error_dob }}</div>
        {% endif %}
    </div>
</div>
I'm having problems where it always shows that the field is not populated.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 4:36 am

I edited catalog/controller/account/register.php to do the validation but am not having any luck

Code: Select all

<?php
namespace Opencart\Catalog\Controller\Account;

/**
 * Class Register
 *
 * @package Opencart\Catalog\Controller\Account
 */
class Register extends \Opencart\System\Engine\Controller {
    /**
     * @return void
     */
    public function index(): void {
        if ($this->customer->isLogged()) {
            $this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']));
        }

        $this->load->language('account/register');

        $this->document->setTitle($this->language->get('heading_title'));

        $data['breadcrumbs'] = [];

        $data['breadcrumbs'][] = [
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
        ];

        $data['breadcrumbs'][] = [
            'text' => $this->language->get('text_account'),
            'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language'))
        ];

        $data['breadcrumbs'][] = [
            'text' => $this->language->get('text_register'),
            'href' => $this->url->link('account/register', 'language=' . $this->config->get('config_language'))
        ];

        $data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', 'language=' . $this->config->get('config_language')));

        $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));

        $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
        $data['config_telephone_display'] = $this->config->get('config_telephone_display');
        $data['config_telephone_required'] = $this->config->get('config_telephone_required');

        $this->session->data['register_token'] = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26);

        $data['register'] = $this->url->link('account/register.register', 'language=' . $this->config->get('config_language') . '&register_token=' . $this->session->data['register_token']);
        $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language'));

        $data['customer_groups'] = [];

        if (is_array($this->config->get('config_customer_group_display'))) {
            $this->load->model('account/customer_group');

            $customer_groups = $this->model_account_customer_group->getCustomerGroups();

            foreach ($customer_groups as $customer_group) {
                if (in_array($customer_group['customer_group_id'], $this->config->get('config_customer_group_display'))) {
                    $data['customer_groups'][] = $customer_group;
                }
            }
        }

        $data['customer_group_id'] = $this->config->get('config_customer_group_id');

        // Custom Fields
        $data['custom_fields'] = [];

        $this->load->model('account/custom_field');

        $custom_fields = $this->model_account_custom_field->getCustomFields();

        foreach ($custom_fields as $custom_field) {
            if ($custom_field['location'] == 'account') {
                $data['custom_fields'][] = $custom_field;
            }
        }

        // Captcha
        $this->load->model('setting/extension');

        $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));

        if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
            $data['captcha'] = $this->load->controller('extension/'  . $extension_info['extension'] . '/captcha/' . $extension_info['code']);
        } else {
            $data['captcha'] = '';
        }

        $this->load->model('catalog/information');

        $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));

        if ($information_info) {
            $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information.info', 'language=' . $this->config->get('config_language') . '&information_id=' . $this->config->get('config_account_id')), $information_info['title']);
        } else {
            $data['text_agree'] = '';
        }

        $data['language'] = $this->config->get('config_language');

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        $this->response->setOutput($this->load->view('account/register', $data));
    }

    /**
     * @return void
     */
    public function register(): void {
        $this->load->language('account/register');

        $json = [];

        $keys = [
            'customer_group_id',
            'firstname',
            'lastname',
            'email',
            'telephone',
            'custom_field',
            'password',
            'confirm',
            'agree'
        ];

        foreach ($keys as $key) {
            if (!isset($this->request->post[$key])) {
                $this->request->post[$key] = '';
            }
        }

        if (!isset($this->request->get['register_token']) || !isset($this->session->data['register_token']) || ($this->session->data['register_token'] != $this->request->get['register_token'])) {
            $json['redirect'] = $this->url->link('account/register', 'language=' . $this->config->get('config_language'), true);
        }

        if (!$json) {
            // Customer Group
            if ($this->request->post['customer_group_id']) {
                $customer_group_id = (int)$this->request->post['customer_group_id'];
            } else {
                $customer_group_id = (int)$this->config->get('config_customer_group_id');
            }

            $this->load->model('account/customer_group');

            $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);

            if (!$customer_group_info || !in_array($customer_group_id, (array)$this->config->get('config_customer_group_display'))) {
                $json['error']['warning'] = $this->language->get('error_customer_group');
            }

            if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
                $json['error']['firstname'] = $this->language->get('error_firstname');
            }

            if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
                $json['error']['lastname'] = $this->language->get('error_lastname');
            }

            if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
                $json['error']['email'] = $this->language->get('error_email');
            }

            $this->load->model('account/customer');

            if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
                $json['error']['warning'] = $this->language->get('error_exists');
            }

            if ($this->config->get('config_telephone_required') && (oc_strlen($this->request->post['telephone']) < 3) || (oc_strlen($this->request->post['telephone']) > 32)) {
                $json['error']['telephone'] = $this->language->get('error_telephone');
            }

            // Validate Address
            if ((utf8_strlen(trim($this->request->post['address'])) < 1) || (utf8_strlen(trim($this->request->post['address'])) > 128)) {
                $json['error']['address'] = 'Address must be between 1 and 128 characters!';
            }

            // Validate Phone
            if ((utf8_strlen(trim($this->request->post['phone'])) < 3) || (utf8_strlen(trim($this->request->post['phone'])) > 32)) {
                $json['error']['phone'] = 'Phone number must be between 3 and 32 characters!';
            }
            
            
            // Validate Date of Birth for age verification
            $dob = new DateTime($this->request->post['dob']);
            $today = new DateTime();
            $age = $today->diff($dob)->y;

            if ($age < 21) {
                $json['error']['dob'] = 'You must be 21 years or older to register.';
            }

            // Custom field validation
            $this->load->model('account/custom_field');

            $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);

            foreach ($custom_fields as $custom_field) {
                if ($custom_field['location'] == 'account') {
                    if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
                    } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
                    }
                }
            }

            if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 4) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
                $json['error']['password'] = $this->language->get('error_password');
            }

            if ($this->request->post['password'] != $this->request->post['confirm']) {
                $json['error']['confirm'] = $this->language->get('error_confirm');
            }

            // Captcha
            $this->load->model('setting/extension');

            $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));

            if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
                $captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');

                if ($captcha) {
                    $json['error']['captcha'] = $captcha;
                }
            }

            // Agree to terms
            $this->load->model('catalog/information');

            $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));

            if ($information_info && !$this->request->post['agree']) {
                $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
            }
        }

        if (!$json) {
            $this->load->model('account/customer');

            $customer_id = $this->model_account_customer->addCustomer($this->request->post);

            // Custom field validation
            $this->load->model('account/custom_field');

            $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);

            foreach ($custom_fields as $custom_field) {
                if ($custom_field['location'] == 'account') {
                    if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
                    } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
                    }
                }
            }

            // Custom field validation
            foreach ($custom_fields as $custom_field) {
                if ($custom_field['location'] == 'address') {
                    if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
                    } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                        $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
                    }
                }
            }

            // Validate password
            if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 4) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
                $json['error']['password'] = $this->language->get('error_password');
            }

            // Captcha
            $this->load->model('setting/extension');

            $extension_info = $this->model_setting_extension->getExtensionByCode('captcha', $this->config->get('config_captcha'));

            if ($extension_info && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
                $captcha = $this->load->controller('extension/' . $extension_info['extension'] . '/captcha/' . $extension_info['code'] . '.validate');

                if ($captcha) {
                    $json['error']['captcha'] = $captcha;
                }
            }

            // Agree to terms
            $this->load->model('catalog/information');

            $information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));

            if ($information_info && !$this->request->post['agree']) {
                $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
            }

            // Add the customer
            $customer_id = $this->model_account_customer->addCustomer($this->request->post);

            // Login if no approval is required
            $this->customer->login($this->request->post['email'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8'));

            // Add to activity log
            $this->load->model('account/activity');

            $activity_data = [
                'customer_id' => $customer_id,
                'name'        => $this->request->post['firstname'] . ' ' . $this->request->post['lastname']
            ];

            $this->model_account_activity->addActivity('register', $activity_data);

            $json['success'] = $this->language->get('text_success');
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}
?>

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 4:37 am

I also edited. catalog/language/en-gb/account/register.ph

Code: Select all

$_['entry_address'] = 'Address';
$_['entry_phone'] = 'Phone';
$_['entry_dob'] = 'Date of Birth';

$_['error_address'] = 'Address must be between 1 and 128 characters!';
$_['error_phone'] = 'Phone number must be between 3 and 32 characters!';
$_['error_dob'] = 'You must be 21 years or older to register.';

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 4:38 am

I also did to the customer table

Code: Select all

ALTER TABLE `customer` ADD `address` VARCHAR(128) NOT NULL;
ALTER TABLE `customer` ADD `phone` VARCHAR(32) NOT NULL;
ALTER TABLE `customer` ADD `dob` DATE NOT NULL;

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 8:31 am

To make the fields, in registration.twig
<!-- Added these inside the registration form to make the needed fields -->

Code: Select all

  <div class="row mb-3 required">
    <label for="input-address" class="col-sm-2 col-form-label">{{ entry_address }}</label>
    <div class="col-sm-10">
      <input type="text" name="address" value="" placeholder="{{ entry_address }}" id="input-address" class="form-control"/>
      <div id="error-address" class="invalid-address"></div>
    </div>
  </div>

Code: Select all

<div class="row mb-3 required">
    <label for="input-phone" class="col-sm-2 col-form-label">{{ entry_phone }}</label>
    <div class="col-sm-10">
        <input type="text" name="phone" value="{{ phone }}" placeholder="{{ entry_phone }}" id="input-phone" class="form-control" />
            <div class="text-danger"></div>

    </div>
</div>

Code: Select all

<div class="row mb-3 required">
    <label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
    <div class="col-sm-10">
        <input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control" />
            <div class="text-danger"></div>
    </div>
</div>

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sat Aug 03, 2024 8:33 am

I'm having problems with field verification. I'm getting the JSON parse error, unexpected EOF. and I don't know what to do with that error.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by softmonke » Sun Aug 04, 2024 6:33 am

Woah, first of all, please don't dump all the code into your replies - they are unnecessary, very difficult to read, and can be kind of spammy since you've basically made about 8 posts just to post your code. Next time, just post snippets of code that you've written instead, and use the "code" tags to group your snippets of code so that it's easier for users to read and help you out. Don't just dump your entire files' codes into multiple posts.

Anyway, I did a quick glance through your code and saw an error right away - you're using "utf8_strlen" when validating the "address" and "phone" fields. The function "utf8_strlen" is an old function used in OpenCart 3 and below. In OpenCart 4, it has changed to "oc_strlen" instead. You can see that in the validation code before the code you've written! They are clearly using "oc_strlen" instead of "utf8_strlen" - so try changing that up and follow the existing validation code.

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by keithdarr » Sun Aug 04, 2024 1:31 pm

Thank you for looking at my code. I've been trying to figure it out and have had a hard time. It it little stuff like this that matters.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sun Aug 04, 2024 1:39 pm

// Validate Date of Birth for age verification
// THIS ISN'T WORKING YET

Code: Select all

 $dob = new DateTime($this->request->post['dob']);
        $today = new DateTime();
        $age = $today->diff($dob)->y;

        if ($age < 21) {
            $json['error']['dob'] = $this->language->get('error_dob');
        }

        $json['age'] = $age; // Add age to the JSON response

        error_log('User age: ' . $age); // This line is to print the age to the error log

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by softmonke » Sun Aug 04, 2024 5:20 pm

keithdarr wrote:
Sun Aug 04, 2024 1:39 pm
// Validate Date of Birth for age verification
// THIS ISN'T WORKING YET

$dob = new DateTime($this->request->post['dob']);
$today = new DateTime();
$age = $today->diff($dob)->y;

if ($age < 21) {
$json['error']['dob'] = $this->language->get('error_dob');
}

$json['age'] = $age; // Add age to the JSON response

error_log('User age: ' . $age); // This line is to print the age to the error log
If you're trying to write to your error logs, you should be using the following function:

Code: Select all

$this->log->write('User age: ' . $age);

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by keithdarr » Sun Aug 04, 2024 8:34 pm

I will try that. But right now, I have all logging taken out to try to simplify the process of validation of the date of birth (dob).

I tried something a little more robust but, it still acts like no date is being entered. My date field is showing the date in the 08/04/2024 format. I don't know if that is messing with things. Just a thought that crossed my mind right now.

Code: Select all

  // Validate Date of Birth for age verification
        if (!empty($this->request->post['dob'])) {
            try {
                $dob = new DateTime($this->request->post['dob']);
                $today = new DateTime();
                $age = $today->diff($dob)->y;

                if ($age < 21) {
                    $json['error']['dob'] = $this->language->get('error_dob');
                }

                // Add age to JSON response for display
                $json['age'] = $age;
            } catch (Exception $e) {
                $json['error']['dob'] = 'Invalid date format';
            }
        } else {
            $json['error']['dob'] = 'Date of Birth is required';
        }

This is what my field looks like in catalog/view/template/account/register.twig

Code: Select all

<div class="row mb-3 required">
    <label for="input-dob" class="col-sm-2 col-form-label">{{ entry_dob }}</label>
    <div class="col-sm-10">
        <input type="date" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control"/>
        <div id="error-dob" class="invalid-feedback"></div>
        <div id="age-display" class="text-info"></div> 
        <!-- This is the new div for displaying the age but this isn't working yet as the whole validation isn't working for the DOB -->
    </div>
</div>

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by keithdarr » Sun Aug 04, 2024 8:47 pm

// I think the problem is related to the date formatting.
// I believe PHP expects YYYY-MM-DD
// the field is populated MM-DD-YYYY when the calendar is selected

// I put script at the end of catalog/view/template/account/register.twig

<!-- this script is to work with the DOB problem I have been having where the date is entered in MM/DD/YYYY and php expects YYYY/MM/DD
-->

Code: Select all

<script type="text/javascript">
    $(document).ready(function() {
        $('#form-register').on('submit', function(e) {
            e.preventDefault();

            // Reformat the date to YYYY-MM-DD
            var dobInput = $('#input-dob');
            var dobValue = dobInput.val();
            var dateParts = dobValue.split('-'); // Assuming YYYY-MM-DD format
            if (dateParts.length === 3) {
                var formattedDate = dateParts[0] + '-' + dateParts[1] + '-' + dateParts[2];
                dobInput.val(formattedDate);
            } else {
                alert('Invalid date format. Please enter the date in MM/DD/YYYY format.');
                return;
            }

            $.ajax({
                url: $(this).attr('action'),
                type: 'post',
                data: $(this).serialize(),
                dataType: 'json',
                beforeSend: function() {
                    // Clear all existing error messages and classes
                    $('.text-danger').remove();
                    $('.is-invalid').removeClass('is-invalid');
                    $('#age-display').text(''); // Clear age display
                },
                success: function(json) {
                    if (json['redirect']) {
                        location = json['redirect'];
                    } else if (json['success']) {
                        alert('Registration successful!');
                    } else {
                        if (json['error']) {
                            for (var i in json['error']) {
                                var element = $('#input-' + i.replace(/_/g, '-'));

                                if (element.parent().hasClass('input-group')) {
                                    element.parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                                } else {
                                    element.after('<div class="text-danger">' + json['error'][i] + '</div>');
                                }
                                element.addClass('is-invalid');
                            }
                        }
                        if (json['age']) {
                            $('#age-display').text('Calculated Age: ' + json['age']);
                        }
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert('Error: ' + thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        });
    });
</script>

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by JNeuhoff » Sun Aug 04, 2024 11:25 pm

As softmonke pointed out, please don't post all your unformatted code across multiple posts. Use the code display button, or better, post it as an attachment.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by softmonke » Mon Aug 05, 2024 11:15 am

PHP's DateTime constructor can parse common date formats, so it doesn't expect a specific date format. For example, the following will all return the same result:

Code: Select all

$date1 = new DateTime("2024-08-04");
$date2 = new DateTime("08/04/2024");
$date3 = new DateTime("4th August 2024");
Also, OpenCart's theme already comes with a date time picker, so why not use that instead of using the HTML date input and trying to format the date using your own JavaScript code? Try the following for your date field:

Code: Select all

<div class="col mb-3 required">
  <label for="input-dob" class="form-label">{{ entry_dob }}</label>
  <div class="input-group">
    <input type="text" name="dob" value="{{ dob }}" placeholder="{{ entry_dob }}" id="input-dob" class="form-control date" />
    <div class="input-group-text"><i class="fa-regular fa-calendar"></i></div>
  </div>
  <div id="error-dob" class="invalid-feedback"></div>
</div>

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by paulfeakins » Mon Aug 05, 2024 6:57 pm

Please, if you're posting huge blocks of code, use the code blocks:

[ code] and [ /code]

without spaces.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by keithdarr » Mon Aug 05, 2024 11:53 pm

I got my POS developer to help out. I got it close with your help. Thank you for the support.

New member

Posts

Joined
Wed Nov 29, 2023 12:54 pm
Location - Minneapolis, Minnesota, US

Post by ru-lefthanded » Thu Aug 22, 2024 8:17 pm

Could you please PM me with the code that finally worked as I need to verify age on sales of some items :)

New member

Posts

Joined
Tue Sep 03, 2013 3:39 am
Who is online

Users browsing this forum: No registered users and 5 guests