1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try the data annotation extension (NumericAttribute)

http://dataannotationsextensions.org/

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.