Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:48:57+00:00 2026-06-03T06:48:57+00:00

This is my Model: [RegularExpression(@^08[589][0-9]{8}$, ErrorMessage = Invalid Number!)] public string Phone { get;

  • 0

This is my Model:

[RegularExpression(@"^08[589][0-9]{8}$", ErrorMessage = "Invalid Number!")]
public string Phone { get; set; }

[ForeignKey]
public long PhoneType { get; set; } // 1-CellPhone , 2-Phone

So I think to change RegularExpression Validation by Change PhoneType if I want say more specific:

if user select CellPhone from DropDownList the validation be

[RegularExpression(@"^08[589][0-9]{8}$", ErrorMessage = "Invalid Number!")] 

and if select Phone the validation be

 [RegularExpression("^[1-9][0-9]{9}$", ErrorMessage = "Invalid Number!")]

What is your suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:48:59+00:00Added an answer on June 3, 2026 at 6:48 am

    You could write a custom validation attribute:

    public class PhoneAttribute : ValidationAttribute
    {
        private readonly string _phoneTypeProperty;
        public PhoneAttribute(string phoneTyperoperty)
        {
            _phoneTypeProperty = phoneTyperoperty;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var property = validationContext.ObjectType.GetProperty(_phoneTypeProperty);
            if (property == null)
            {
                return new ValidationResult(string.Format("Unknown property: {0}", _phoneTypeProperty));
            }
    
            var phone = Convert.ToString(value, CultureInfo.CurrentCulture);
            if (string.IsNullOrEmpty(phone))
            {
                return null;
            }
    
            var phoneType = (long)property.GetValue(validationContext.ObjectInstance, null);
            Regex regex = null;
            if (phoneType == 1)
            {
                regex = new Regex(@"^08[589][0-9]{8}$");
            }
            else if (phoneType == 2)
            {
                regex = new Regex("^[1-9][0-9]{9}$");
            }
            else
            {
                return new ValidationResult(string.Format("Unknown phone type: {0}", phoneType));
            }
    
            var match = regex.Match(phone);
            if (match.Success && match.Index == 0 && match.Length == phone.Length)
            {
                return null;
            }
    
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
    }
    

    and then decorate your view model property with this attribute:

    public class MyViewModel
    {
        [Phone("PhoneType", ErrorMessage = "Invalid Number!")]
        public string Phone { get; set; }
    
        public long PhoneType { get; set; }
    }
    

    Another possibility (and which I would more than strongly recommend) if you want to make your life easier with validation is to use FluentValidation.NET. Just look at how easier it is to define validation rules instead of writing gazzilions of lines of plumbing code and no longer be able to understand which part is plumbing and which part is actual validation. With FluentValidation.NET there’s no plumbing. You express your validation requirements in a fluent way:

    public class MyViewModelValidator : AbstractValidator<MyViewModel>
    {
        public MyViewModelValidator()
        {
            RuleFor(x => x.Phone)
                .Matches(@"^08[589][0-9]{8}$").When(x => x.PhoneType == 1)
                .Matches("^[1-9][0-9]{9}$").When(x => x.PhoneType == 2);
        }
    }
    

    Simply compare this validator with the previous one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set;
Let's consider this model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time =
I'm using FluentNHibernate but NHibernate XML will do. Say I have this model public
Data annotation to validate an inbound model in MVC: public class ValidNumber { [RegularExpression(@^\d+$,
I have the following model : public class ContratoDetailsViewModel { [StringLength(50)] [RegularExpression(^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$)] [DisplayName(E-Mail Adm.)]
I'm a nub in MVC.I've a Model: public class Usuarios { [Required(ErrorMessage = **TxtOPID
I need to match a number of 1-5 digits or empty string for model
I ask a similar question here . This is My Model: [DisplayName(National Code)] [Required(ErrorMessage
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have this model var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.