问题描述:

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..

网友答案:

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

网友答案:

Try the data annotation extension (NumericAttribute)

http://dataannotationsextensions.org/

http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx

相关阅读:
Top