Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving a error message when out of range

I am making a app, and in my app i have a textfield with a range of 4 - 64

Now i am looking for a way to give the user a error message when he tries to put a letter outside of this range and push the clicks the button submit it needs to say something like invalid ...

So by example he put a 2 inside the textfield and clicks submit it needs to give the error message in a alert box or something.

The textField in my model:

[DisplayName("Length")]
        [Range(4, 64, ErrorMessage = "Test")] //errormessage isn't working
        public int LengthField { get; set; }

Textfield + submit button on my view:

@Html.LabelFor(m => m.LengthField):
@Html.TextBoxFor(m => m.LengthField) (4 - 64 chars)

<button type="submit" id="btn_submit">Submit</button> 

On the internet i read something about @Html.ValidationSummary maybe i can use this? But when someone knows a good way it's fine

like image 256
user2181474 Avatar asked Feb 01 '26 05:02

user2181474


1 Answers

Underneath your TextBoxFor, add a ValidationMessageFor, like so:

@Html.ValidationMessageFor(m => m.LengthField)

That will display the error message next to the text box.

To display it at the top of the page, add this to the top of your view:

@Html.ValidationSummary()
like image 71
mattytommo Avatar answered Feb 03 '26 20:02

mattytommo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!