Shopping Cart Operations > General Support

Validate Zip code

(1/1)

Sam_78:
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: ---$('#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();
}
--- End code ---

abolabo:
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

Sam_78:
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');
      }




abolabo:

--- Quote from: 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');
      }

--- End quote ---

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.

Navigation

[0] Message Index

Go to full version
Powered by SMFPacks Social Login Mod