Problem with extending validation in registration area

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 10 años
Yesterday I tried to validation of phone number in the registration area. Default validation is to check wtether the field is empty or not with the following code:

if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
{
  RuleFor(x => x.Phone).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Phone.Required"));
}


But now I want to validate whether the number is  a valid Bangladeshi mobile number. To do so I write a private method in the
Nop.Web.Validators.Customer.RegisterValidator
class:


private bool BeAValidMobileNumber(string mobileNumber)
{
  int mn = 0;
       if (!Int32.TryParse(mobileNumber, out mn))
           return false;

       if (!mobileNumber.StartsWith("880"))
           return false;

       if (mobileNumber.Trim().Length != 13)
          return false;

       return true;
}


finally, modify the rule for phone with this code:

if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
{
  RuleFor(x => x.Phone).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Phone.Required"));
       RuleFor(x => x.Phone).Must(ph => BeAValidMobileNumber(ph)).WithMessage(localizationService.GetResource("Account.Fields.Phone.NotAValidFormat"));
}


But unfortunately, the rule which I add, did not work. :( Can anyone please tell me what is the mistake in my code?

Thank you
Hace 10 años
Can anyone please reply my message?
Hace 10 años
Still waiting for the answer ...
Hace 10 años
Did you debug it?  Is your function getting called?
Hace 7 años
You need to add to resource file Account.Fields.Phone.NotAValidFormat

As well as to the SQL Table [LocaleStringResource]
Hace 7 años
Changes done in RegisterValidator.cs class works perfectly on register form. I have also modified the code and its working fine. I have applied the length validation and added resource setting from admin panel. Below is my modified code.

if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
            {
                RuleFor(x => x.Phone).NotEmpty().Length(10).WithMessage(localizationService.GetResource("Account.Fields.Phone.Minlength"));
            }


Hope this help!
Hace 7 años
how to add more Register validation for register area in custom plugin
without changing the original?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.