[RESOLVED]Validation of data type int is not working correctly

I have a field in a table of type int, they can be Null. I did a validation for the models.
[Range(0, 9999, ErrorMessage = "Invalid value")]
But if you make an incorrect entry, there will be no warning and the database will be written Null.

I also tried to use a regular expression
[RegularExpression("[0-9]+", ErrorMessage = "Invalid value")]
But it does not help.

tuwilof

I have a field in a table of type int, they can be Null. I did a validation for the models.
[Range(0, 9999, ErrorMessage = "Invalid value")]

in .NET the field is

int?

or 

int

?

The table has a column of type int. For this table, I have a model. I’m doing adding Validation to the Model.

tuwilof

I have a model.

of course. Please share the model property declaration ( the one with [Range…

0 is a valid integer. how about changing the range to greater than 1

https://answers.yahoo.com/question/index?qid=20101228075637AA2lULK

Have you write ValidationFor in your view? And are you using that property inside form. As without form validations won’t work.

If you want to ensure that the value actually exists and isn’t null, you could just use the [Required] attribute on it. Additionally, if you wanted to handle this through a Regular Expression to match valid integers (that didn’t start with leading zeroes),
you could use the following :

[Required]
[RegularExpression(@"^(?!0d+)d+$", ErrorMessage = "Invalid Value")]

Is your database value a nullable integer (e.g. an integer that allows nulls)? If that is the case, you could just use something like this :

[Required]
public int? YourProperty { get; set; }

Leave a Reply