Reputation: 649
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
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
Reputation: 26737
Try the data annotation extension (NumericAttribute
)
http://dataannotationsextensions.org/
http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx
Upvotes: 1