Problem with extending validation in registration area

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
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
10 years ago
Can anyone please reply my message?
10 years ago
Still waiting for the answer ...
10 years ago
Did you debug it?  Is your function getting called?
7 years ago
You need to add to resource file Account.Fields.Phone.NotAValidFormat

As well as to the SQL Table [LocaleStringResource]
7 years ago
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!
7 years ago
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.