NeverTrust
NeverTrust

Reputation: 6177

The value '(SomeNumber)' is not valid for Number

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

Answers (4)

Muhammad Gouda
Muhammad Gouda

Reputation: 869

Phone Numbers should be stored as strings rather than numbers

Because:

  1. Some numbers start with 0 or + which cannot be stored in any numeric value (e.g. "002121132434" or "+9772323212"
  2. You will need to search for phone numbers as string (e.g. numbers starting with specific substrings represent specific regions or specific service providers)
  3. You will need to do some string formatting (e.g. "+977-232-3212")
  4. You will never need to do mathematical operation (e.g. you will never add or subtract 2 phone numbers)

Upvotes: 1

Moumit
Moumit

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

Georg Patscheider
Georg Patscheider

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

Bernhard Hiller
Bernhard Hiller

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

Related Questions