Reputation: 121
In My model I use
[Display(Name ="No Of Children")]
public Nullable<int> NoOFChildren { get; set; }
And in my view I use
<div class="col-lg-6">
@Html.EditorFor(model => model.NoOFChildren, new { htmlAttributes = new { @class = "form-control", placeholder = "No Of Children" } })
</div>
When I run the page it will show textbox like following image
I want to restrict this textbox to accept number less than zero. When number is zero the down arrow should be disable. How I can do that?
jQuery validation may be one option but I don’t want to use that until and unless no alternative is valuable. Is there any DataAnotation validation attributes is there that can do it or any HTML5 elements that may do it automatically without writing additional scripts?
Upvotes: 4
Views: 7737
Reputation: 1305
You can either do this using jquery or you can add a data anotation on your model
[Display(Name ="No Of Children")]
[Range(0, 15, ErrorMessage = " ")]
public Nullable<int> NoOFChildren { get; set; }
This should restrict the user.
Also leaving the ErrorMessage
blank (it has a space inside, not technically blank) will not display error message on the view
Update You can try this:
@Html.EditorFor(model => model.NoOFChildren, new { htmlAttributes = new { @class = "form-control", placeholder = "No Of Children",min="0" ,max="100",step="5" } })
this will set min as 0 max as 100 and increment on every click by 5.
Upvotes: 3