AbanteCart Community

Shopping Cart Operations => Support => General Support => Topic started by: Sam_78 on November 05, 2021, 11:05:02 AM

Title: Validate Zip code
Post by: Sam_78 on November 05, 2021, 11:05:02 AM
Hello,

On customer registration form I need only 5 digit numeric value from customer but customers are able to put some big random email address in that field. Is there any validation from backend side that can stop this? I have added this code on submit click but still I don't understand how are they able to enter it.

Code: [Select]
$('#AccountFrm_postcode').attr('maxlength', '5');
$("#AccountFrm_postcode").on("keypress keyup blur",function (event) {   
    $(this).val($(this).val().replace(/[^\d].+/, ""));
    if ((event.which < 48 || event.which > 57)) {
    event.preventDefault();
    }
});
var zipCode = $('#AccountFrm_postcode').val();
if(zipCode.length < 5){
alert("Please enter 5 digit Zip Code");
event.preventDefault();
$('#AccountFrm_postcode').focus();
}
Title: Re: Validate Zip code
Post by: abolabo on November 08, 2021, 03:16:18 AM
you can try to add hook

public function onModelAccountCustomer_ValidateData(){
    $that = $this->baseObject;
    //do check
    if(mb_strlen($that->request->post['postcode'])>5){
     $that->error['postcode] = 'Your error text here';
   }

}

See $this->extensions->hk_ValidateData($this, [__FUNCTION__]);  call inside model/account/customer for details.
Also you should add the same hook for guests
Title: Re: Validate Zip code
Post by: Sam_78 on November 08, 2021, 09:38:36 AM
Hi Abolabo, how do I call this function? Can I just use this function instead  public function validateRegistrationData( $data ) {

and add the lines

 if(mb_strlen($that->request->post['postcode'])>5){
     $that->error['postcode] = 'Your error text here';
   }


Because I see validation in this function that are already being used like:

if ((mb_strlen($data['city']) < 3) || (mb_strlen($data['city']) > 128)) {
         $this->error['city'] = $this->language->get('error_city');
      }




Title: Re: Validate Zip code
Post by: abolabo on November 08, 2021, 10:10:29 AM
Hi Abolabo, how do I call this function? Can I just use this function instead  public function validateRegistrationData( $data ) {

and add the lines

 if(mb_strlen($that->request->post['postcode'])>5){
     $that->error['postcode] = 'Your error text here';
   }


Because I see validation in this function that are already being used like:

if ((mb_strlen($data['city']) < 3) || (mb_strlen($data['city']) > 128)) {
         $this->error['city'] = $this->language->get('error_city');
      }

Yes you can, but future upgrades will overwrite it.
Best way is to create your own extension (for example clone current template as extension via our developer_tools) and put validation into the hook-file as i posted above.