Home > other >  Force the user to enter an specified range of numbers in ASP.NET Core Model
Force the user to enter an specified range of numbers in ASP.NET Core Model

Time:01-11

Is there any way to force the user to enter an specified range of numbers in ASP.NET Core Model and if its not entered the desired range, shows a non validated message in the view after submitting the form?

    [Key]
    public int ExampleId { get; set; }

    [Display(Name = "Number")]
    [Required(ErrorMessage = "Please enter the {0}, It should be between 1 and 100.")]
    public int Number { get; set; }

I know how to display a message if user didn't enter the required field. But is it possible to show a non validated data when a user enters a number for example when its above 100? I don't want to check it by javascript. I just want to know if its possible in this way?

CodePudding user response:

What you're looking for is Range. You can specify what values are min and max they can add.

[Range(0, 101, ErrorMessage = "The field {0} must be greater than {1} and less than {2}.")]

This above says that the value must be between 1 and 100.

For more information please see this

  •  Tags:  
  • Related