Tax not calculated when Zip code is 5+4

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi,

I have noticed that when a customer enters a Zip+4 in their address, during checkout, the system will not calculate tax.  This works perfectly if they only enter the first 5 digits of the Zip code (my customers are in USA).

This makes sense as my tax setup is using Tax by County & State & Zip and I have tax rates for each Zip code I collect for but the zip codes only have the 5 digits.

How can I force the Zip Code field to only allow 5 Digits?

I looked at Nop.Web.Validators.Customer.CustomerInfoValidator and added a rule for the Zip code as follows:

if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
{
RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));
RuleFor(x => x.ZipPostalCode).length(5).WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Length"));
}

However, the rule did not fire when I tested.

Any ideas on how to resolve my problem?
6 years ago
chris.navarro7 wrote:
Hi,

I have noticed that when a customer enters a Zip+4 in their address, during checkout, the system will not calculate tax.  This works perfectly if they only enter the first 5 digits of the Zip code (my customers are in USA).

This makes sense as my tax setup is using Tax by County & State & Zip and I have tax rates for each Zip code I collect for but the zip codes only have the 5 digits.

How can I force the Zip Code field to only allow 5 Digits?

I looked at Nop.Web.Validators.Customer.CustomerInfoValidator and added a rule for the Zip code as follows:

if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
{
RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"));
RuleFor(x => x.ZipPostalCode).length(5).WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Length"));
}

However, the rule did not fire when I tested.

Any ideas on how to resolve my problem?


Where exactly are you adding this new rule?
..in the solution (and then re-compiled it), or directly in a view?

Look for this section in the /Views/Shared/_CreateOrUpdateAddress.cshtml

    @if (Model.ZipPostalCodeEnabled)
    {
        <div class="inputs">
            @Html.LabelFor(model => model.ZipPostalCode, new { }, ":")
                @Html.EditorFor(model => model.ZipPostalCode)
            @if (Model.ZipPostalCodeRequired)
            {
                @Html.RequiredHint()
            }
            @Html.ValidationMessageFor(model => model.ZipPostalCode)
        </div>
    }


Try injecting your if/then condition there.
6 years ago
I added the rule inside the class at Nop.Web.Validators.Customer.CustomerInfoValidator

Then I recompiled and moved the nop.web.dll to my test server and tested it.  (It takes too long for my machine to run the website within Visual Studio so I find myself copying DLLs to my test server to test things and if it doesn't work, then I debug it).  

I guess I'll have to step through it in debug...

My question is though, is this the right place to add this; Nop.Web.Validators.Customer.CustomerInfoValidator?
6 years ago
Looking closely, I found another validator that was related at Nop.Web.Validators.Common.AddressValidator, I added my rule and now the form will not submit unless the Zip code length is 5.

That will do for me!
6 years ago
Just wanted to add this bit to limit the validation to a specific state.
For example, California:


RuleFor(x => x.ZipPostalCode).Length(5).When(x=>x.ZipPostalCode.StartsWith("9")).WithMessage("Please use a valid 5 Digit California Zip Code")
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.