Reputation: 6177
i'm using data annotation and it's a simple project about phone book I want to add a person ,I don't know why I am receiving
The value '09124573957' is not valid for Number
error under my editor box while I press the button Create
here is my dto in view model
[Required(ErrorMessageResourceType = typeof(App_Globaliz.Resource1), ErrorMessageResourceName = "Number")]
[DisplayName("Number")]
// [Phone]
[Range (0, 09889999999, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof(App_Globaliz.Resource1))]
public int Num { get; set; }
you should know that i'm using a resource file which I wrote some errors text in it but this error I don't know where did it came from
Upvotes: 1
Views: 2955
Reputation: 869
Phone Numbers should be stored as strings rather than numbers
Because:
Upvotes: 1
Reputation: 9540
Your range validator
is not working because numeric do not support leading zero .. and range validator works on numeric type
It will be working using RegularExpression validator
... something like this ...
[Required(ErrorMessageResourceType = typeof(App_Globaliz.Resource1), ErrorMessageResourceName = "Number")]
[Display(Name = "Number")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid number")]
public string PhoneNumber { get; set; }
Upvotes: 3
Reputation: 9463
Integers do not support leading zeros. You should not see this error if you try to set the value 9124573957
. EDIT: this will only work if you use long
instead of int
to store the value, because 9124573957 is bigger than int.MaxValue
.
If you have to support leading zeros, you have to store the value as string
.
Upvotes: 0
Reputation: 2397
Number data types (int, double, etc.) are for doing Math! Just ask yourself: could it be useful to add two of those numbers, e.g. what's the meaning of the sum of your telephone number and your friend's? If that does not make sense at all, it is not a number.
Telephone numbers are strings, though they are commonly called numbers.
Upvotes: 2