Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The DataAnnotations [Phone] Attribute

Tags:

What is the default, valid format of the [Phone] attribute? In the data table, the phone column is navrchar (16) If I enter a phone # like 1112223333, I get "field is not a valid phone number." If I enter 01112223333, I get "The value '11112223333' is invalid."

Also, how to override it? I understand that I could do something like this, but is this the best practice in this case?

[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",ErrorMessage="Invalid Phone Number!")]

Related code:

    [Required]
    [Phone]
    public string Phone { get; set; }

    <div class="editor-field">
       @Html.EditorFor(model => model.Phone)
       @Html.ValidationMessageFor(model => model.Phone)
    </div>

Update I guess there was a mapping issue when I changed the phone column from int to navrchar. Updating the model was not enough, so I had to change the value manually using the Table Mapping.

Error 2019: Member Mapping specified is not valid. The type 'Edm.Int32[Nullable=False,DefaultValue=]' of member 'Phone' in type 'UserDBModel.UserProfile' is not compatible with 'SqlServerCe.nvarchar[Nullable=False,DefaultValue=,MaxLength=16,Unicode=True,FixedLength=False]' of member 'Phone' in type 'UserDBModel.Store.UserProfile'.

like image 852
usefulBee Avatar asked Oct 25 '13 16:10

usefulBee


People also ask

How to check valid phone number in c#?

There are different ways to validate phone numbers in C#. You can use Regex or the libphonenumber-csharp library to validate phone number format and ensure proper phone number input. Also, you can utilize a phone number validation API.

What are DataAnnotations in net core?

"Data Annotation provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls." Why would I use Data Annotation? There are 3 main areas where you may use it; two of them are related to your data presentation to your end user and one is to design your database.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


2 Answers

The default regular expression for the PhoneAttribute can now be handily found by browsing the source code with .NET Reference Source (.NET Framework 2.7.2) or source.dot.net (.NET Core)

There it shows the (ugly) Regex as being defined as:

private static Regex _regex = new Regex(@"^(\+\s?)?((?<!\+.*)\(\+?\d+([\s\-\.]?\d+)?\)|\d+)([\s\-\.]?(\(\d+([\s\-\.]?\d+)?\)|\d+))*(\s?(x|ext\.?)\s?\d+)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

That answers your direct question, but whether it helps or not remains to be seen. Maybe it would be a good base to create your own modified phone number regex.

Sample Regex Matches

like image 83
Matt Tester Avatar answered Oct 21 '22 00:10

Matt Tester


Try this -

  [Required(ErrorMessage = "Mobile no. is required")]
  [RegularExpression("^(?!0+$)(\\+\\d{1,3}[- ]?)?(?!0+$)\\d{10,15}$", ErrorMessage = "Please enter valid phone no.")]
  public string Phone { get; set; }
like image 21
Sayli Vaidya Avatar answered Oct 21 '22 02:10

Sayli Vaidya