Custom field to store date and ability to check the date format Custom field to store date and ability to check the date format
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

Custom field to store date and ability to check the date format

Started by jimmym, November 30, 2011, 03:32:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jimmym

I've configured Custom field (Profile 4 name) for entering birthdate.  I've modified the register.php and register.js according to several forum postings http://forum.coppermine-gallery.net/index.php/topic,37086.0.html, http://forum.coppermine-gallery.net/index.php/topic,70390.msg344548.html#msg344548 and others...

I'm now stucked cos I'm not able to code logic for date format check in the register.js

I've made the Profile 4 name mandatory:
$form_data = array(
        array('label', $lang_register_php['required_info']),
        array('input', 'username', $icon_array['username'] . $lang_register_php['username'], 25),
        !empty($CONFIG['global_registration_pw']) ? array('password', 'global_registration_pw', $icon_array['password'] . $lang_register_php['global_registration_pw'], 25) : '',
        array('password', 'password', $icon_array['password'] . $lang_register_php['password']),
        array('password', 'password_verification', $icon_array['password'] . $lang_register_php['password_again']),
        array('input', 'email', $icon_array['email'] . $lang_register_php['email'], 255),
        !empty($CONFIG['user_profile1_name']) ? array('input', 'user_profile1', $icon_array['blank'] . $CONFIG['user_profile1_name'], 255) : '',
        !empty($CONFIG['user_profile2_name']) ? array('input', 'user_profile2', $icon_array['blank'] . $CONFIG['user_profile2_name'], 255) : '',
        !empty($CONFIG['user_profile3_name']) ? array('input', 'user_profile3', $icon_array['blank'] . $CONFIG['user_profile3_name'], 255) : '',
        !empty($CONFIG['user_profile4_name']) ? array('input', 'user_profile4', $icon_array['blank'] . $CONFIG['user_profile4_name'], 255) : '',
        array('label', $lang_register_php['optional_info'])
    );
    $optional_data = 0;


I've put in the error messages if the field is blank or entered with the incorrect format.  This is called by the register.js:
if ($element[1] == 'user_profile4') {
                $warning1 = '<div id="user_profile4_warning1" class="cpg_message_validation formFieldWarning" style="display:none;">';
                $warning1 .= 'The ' . $CONFIG['user_profile4_name'] . ' mustn\'t be empty!';
                $warning1 .= '</div>';
                $warning2 = '<div id="user_profile4_warning2" class="cpg_message_validation formFieldWarning" style="display:none;">';
                $warning2 .= 'The ' . $CONFIG['user_profile4_name'] . ' must be dd.mm.yyyy format! eg. 09.06.2006';
                $warning2 .= '</div>';
            }



I've changed this:
$profile4 = $superCage->post->getEscaped('user_profile4');

to this:
$profile4 = trim(get_post_var('user_profile4'));


I've added this to check the entered date.  This is in the function check_user_info(&$error) in the registered.php:
if ($profile4 != ' ') {
    $date_format = 'd.m.Y';
    $my_input = trim($profile4);
    $time = strtotime($my_input);
    $is_valid = date($date_format, $time) == $my_input;

    if ($is_valid == '0') {
      $error .= '<li style="list-style-image:url(images/icons/stop.png)">Value \'' . $CONFIG['user_profile4_name'] . '\' must be in dd.mm.yyyy format! eg. 09.06.2006</li>';
      $error .= $is_valid;
     }
    }


In the registered.js I've added this but not working fully yet:
  if($('#user_profile4').val() == '') {
        $('#user_profile4_warning1').show();
        errors++;
    } else {
          if ($('#user_profile4').val().search(/^(0?[1-9])/(0?[1-9]/((19|20)\\d\\d)$/) == -1) {
            $('#user_profile4_warning2').show();
            errors++;
        }
    }


Here's where I need help from the Java guru.  Could you help to provide the correct code to check the date format?

Thanks in advance.


jimmym

Quote from: jimmym on November 30, 2011, 03:32:37 AM
I've added this to check the entered date.  This is in the function check_user_info(&$error) in the registered.php:
if ($profile4 != ' ') {
    $date_format = 'd.m.Y';
    $my_input = trim($profile4);
    $time = strtotime($my_input);
    $is_valid = date($date_format, $time) == $my_input;

    if ($is_valid == '0') {
      $error .= '<li style="list-style-image:url(images/icons/stop.png)">Value \'' . $CONFIG['user_profile4_name'] . '\' must be in dd.mm.yyyy format! eg. 09.06.2006</li>';
      $error .= $is_valid;
     }
    }


In the registered.js I've added this but not working fully yet:
  if($('#user_profile4').val() == '') {
        $('#user_profile4_warning1').show();
        errors++;
    } else {
          if ($('#user_profile4').val().search(/^(0?[1-9])/(0?[1-9]/((19|20)\\d\\d)$/) == -1) {
            $('#user_profile4_warning2').show();
            errors++;
        }
    }


How to code the java logic for checking for date format d.m.Y  which I have in the registered.php?
if ($profile4 != ' ') {
    $date_format = 'd.m.Y';
    $my_input = trim($profile4);
    $time = strtotime($my_input);
    $is_valid = date($date_format, $time) == $my_input;

    if ($is_valid == '0') {
      $error .= '<li style="list-style-image:url(images/icons/stop.png)">Value \'' . $CONFIG['user_profile4_name'] . '\' must be in dd.mm.yyyy format! eg. 09.06.2006</li>';
      $error .= $is_valid;
     }
    }

jimmym

I've resolved my problem.  I basically change the regular expression to check the date format dd.mm.yyyy

As originally posted:
if($('#user_profile4').val() == '') {
        $('#user_profile4_warning1').show();
        errors++;
    } else {
          if ($('#user_profile4').val().search(/^(0?[1-9])/(0?[1-9]/((19|20)\\d\\d)$/) == -1) {
            $('#user_profile4_warning2').show();
            errors++;
        }
    }


Resolved by this:
if($('#user_profile4').val() == '') {
        $('#user_profile4_warning1').show();
        errors++;
    } else {
          if ($('#user_profile4').val().search(/^(((0[1-9]|[12]\d|3[01])[.](0[13578]|1[02])[.](([2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)[.](0[13456789]|1[012])[.](([2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])[.]02[.](([2-9]\d)\d{2}))|(29[.]02[.](([2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00))))$/) == -1) {
            $('#user_profile4_warning2').show();
            errors++;
        }
    }


I'm pretty happy about it. It work exactly as what I wanted.