News:

AbanteCart v1.4.2 is released.

Main Menu

Do you like AbanteCart? Please rate AbanteCart or share your experience with other eCommerce entrepreneurs. Go to Softaculous rating page to add your rating or write a review

Validate Zip code

Started by Sam_78, November 05, 2021, 11:05:02 AM

Previous topic - Next topic

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.

$('#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();
}

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
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

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

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

Forum Rules Code of conduct
AbanteCart.com 2010 -