Author Topic: Zip/Postal code field validation  (Read 7933 times)

marlvass

  • Beginner
  • *
  • Posts: 7
Zip/Postal code field validation
« on: September 05, 2011, 18:09:37 pm »
hi everyone,

I need to set the postcode field to be validated to have at least one number in it but seems like im missing something becuase it is not working, i inserted the following code under ps_userfield.php (line 720):

  if( isset( $required_fields['zip'] )) {
      
         echo '
         if( =(/^(?=.*[0-9])(?=.*[a-zA-Z])+$/.test(form.zip.value))) {
            alert( \'Please enter a valid Zip Code\');
            return false;
         }';

      }

can someone tell me what is wrong please?

Thanks in advance

david_a

  • Beginner
  • *
  • Posts: 7
Re: Zip/Postal code field validation
« Reply #1 on: August 31, 2012, 12:20:44 pm »
Hi, this works for me - it's still being tested so please be careful if you use it:

       if( isset( $required_fields['zip'] )) {
      
            echo '
         if( !(/^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Hh][Ll]|[Dd][Cc][Uu]|[Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))$/.test(form.zip.value))) {
            alert( \''. str_replace("'","\\'",$VM_LANG->_('REGWARN_ZIP',false)) .'\');
            return false;
         }';

      }

You also need to create a new entry in the VMLanguage file (as REGWARN_ZIP in this case) to display the error message

Cheers

David

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: Zip/Postal code field validation
« Reply #2 on: September 19, 2012, 20:08:42 pm »
If you are working in the UK this is what you may want, as this will ignore the regex test if country is not GBR

ps_uesrfiled.php around line 720

Code: [Select]
        $optional_check = '';
if( VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION') {
$optional_check = '&& form.register_account.checked';
}
   
//Added uk specific regex zip code check
        if( isset( $required_fields['zip'] )) {
echo '
if( form.country.value == "GBR"  && !(/^([a-zA-Z]{1,2}[0-9R][0-9a-zA-Z]? [0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2})$/.test(form.zip.value))) {
alert( \'Please enter a valid post code space needed\');
return false;
}';
}

//end
   
    // We have skipped email in the first loop above!
    // Now let's handle email address validation
   
    if( isset( $required_fields['email'] )) {
   
    echo '
if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.email.value))) {
alert( \''. str_replace("'","\\'",$VM_LANG->_('REGWARN_MAIL',false)) .'\');
return false;
}';

}
if( isset( $required_fields['username'] )) {


I cannot see the point in adding a separate language file entry when the code is already modified, just chnge the warning as required
regards
A

Joomla 3.8.13
php 7.1