Author Topic: Validate Zip code  (Read 250 times)

Offline Sam_78

  • Full Member
  • ***
  • Posts: 231
  • Karma: +41/-1
    • View Profile
Validate Zip code
« 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();
}
« Last Edit: November 08, 2021, 12:35:45 AM by Basara »

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1976
  • Karma: +301/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Validate Zip code
« Reply #1 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
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline Sam_78

  • Full Member
  • ***
  • Posts: 231
  • Karma: +41/-1
    • View Profile
Re: Validate Zip code
« Reply #2 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');
      }





Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1976
  • Karma: +301/-12
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Validate Zip code
« Reply #3 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.
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

 

Powered by SMFPacks Social Login Mod