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 7873053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:23:53+00:00 2026-06-03T02:23:53+00:00

I am trying to implement a custom validator for phone number on an ASP.NET

  • 0

I am trying to implement a custom validator for phone number on an ASP.NET MVC 3 App I am writing. I have wriiten the code for the custom validator as below

public class PhoneNumberValidator : ValidationAttribute 
{


    public PhoneNumberValidator() : base("The Phone Number is not Valid")
    {
    }
    public override bool  IsValid(object value)
    {
        if (value != null)
        {
            string phonenumber = value.ToString();

            var regex = new Regex(@"^(?:[0-9]+(?:-[0-9])?)*$");

            if (regex.IsMatch(phonenumber))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }
}

Then in my Model class I have the following :

    [Display(Name = "PhoneNumber")]
    [Required(ErrorMessage = "Is Phone Number Required")]
    [PhoneNumberValidator]
    public string PhoneNumber { get; set; }

However when I run my app and click the proceed button on the page it does not throw an error if the value entered is letters although if I set a breakpoint I can see that the value is being read in to string phonenumber ok. Am I missing something simple?

  • 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-03T02:23:55+00:00Added an answer on June 3, 2026 at 2:23 am

    You seem to be reinventing a wheel. Why not use the existing regex validator:

    public class MyViewModel
    {
        [Display(Name = "PhoneNumber")]
        [Required(ErrorMessage = "Is Phone Number Required")]
        [RegularExpression(@"^(?:[0-9]+(?:-[0-9])?)*$")]
        public string PhoneNumber { get; set; }
    }
    

    This being said validation is triggered by the model binder, so make sure that the controller action you are submitting the form to takes the view model as argument:

    [HttpPost]
    public ActionResult Process(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // the model is invalid => redisplay view
            return View(model);
        }
    
        // at this stage the model is valid => you could do some processing here 
        // and redirect
        ...
    }
    

    or use the TryUpdateModel method (personally I prefer the previous approach though):

    [HttpPost]
    public ActionResult Process(FormCollection some_Dummy_Parameter_Thats_Not_Used_At_All_But_Which_We_Need_To_Avoid_The_Method_Overloading_Error_With_The_GET_Action_Which_Has_The_Same_Name)
    {
        var model = new MyViewModel();
        if (!TryUpdateModel(model))
        {
            // the model is invalid => redisplay view
            return View(model);
        }
    
        // at this stage the model is valid => you could do some processing here 
        // and redirect
        ...
    }
    

    Also in order to display the error message somewhere make sure that you have a corresponding placeholder in your view:

    @Html.EditorFor(x => x.PhoneNumber)
    @Html.ValidationMessageFor(x => x.PhoneNumber)
    

    or use a validation summary helper:

    @Html.ValidationSummary(false)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ASP.NET, and I'm trying to implement a custom ObjectDataSource Insert method.
I'm trying to implement a .NET Custom Validator that uses $.ajax to query a
I'm trying to implement a custom validation annotation in Seam. We have a list
I am trying to implement a Custom HttpHandler (for the first time), I have
I'm trying to implement validation in my ASP.NET Webforms application. Right now, the only
I am trying to implement Custom Error handling via Action Filter Attributes. My code
I'm trying to implement custom suggestions for qsb. I have added CREATE TABLE IF
I am new to ASP.Net MVC 3, facing some issues while trying to implementing
I am trying to implement some custom security code into SSRS 2008 (not R2)
I'm trying to write a custom validation method in my app - I have

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.