Swetha Bindu
Swetha Bindu

Reputation: 649

Data Attributes, asp.net mvc3 razor

I had a phone number in which the entered data should be only numeric. I would like to differentiate it in Model some thing like..

[Required]
//i want to place some thing here//
public string PhoneNumber
{
    get;
    set;

}

Can any one please help me to find the solution..

Upvotes: 1

Views: 300

Answers (2)

Ian Routledge
Ian Routledge

Reputation: 4042

You could do this with a [RegularExpressionAttribute][1]

[Required]
[RegularExpression(@"\d+")]
public string PhoneNumber
{
    get;
    set;

}

That regular expresion just allows numbers (like you asked) but you could use a more complex regular expression to allow a certain format of phone number if you want to be more strict, for example see http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7

Upvotes: 2

Related Questions