How to refresh @Html.ValidationMessageFor when @Html.TextBoxFor is updated by jQuery?
How to refresh @Html.ValidationMessageFor when @Html.TextBoxFor is updated by jQuery?
I'm developing web application by ASP.NET-MVC.
Model:
[Display(Name = "REGISTERED ADDRESS")]
[StringLength(255)]
public string ADDRESS get; set;
Html:
@Html.TextBox("textBoxFillToAnothertextBox", null, new id = "textBoxFillToAnothertextBox", @class = "form-control", placeholder = "Fill in to apply for textBoxAddress" )
@Html.TextBoxFor(m => m.ADDRESS, new id = "textBoxAddress" + i, @class = "form-control" )
@Html.ValidationMessageFor(m => m.ADDRESS, "", new @class = "text-danger" )
jQuery:
$('#textBoxFillToAnothertextBox').on('keyup change', function ()
$('#textBoxAddress').val($('#textBoxFillToAnothertextBox').val());
);
Case 1: I fill text to "textBoxAddress". The text length is more than 255 and then the "ValidationMessage of Address" is show up as normal.
Case 2: I fill text to "textBoxFillToAnothertextBox" that set to "textBoxAddress" by jQuery. The text length is more than 255 and then the "ValidationMessage of Address" isn't show up.
Could someone help to suggested me in case 2, please?
ValidationMessageFor
$('#textBoxAddress').val().length
Set
maxlength
on the input. Note a user can manipulate this from browser dev tools but most won't– charlietfl
Sep 13 '18 at 4:20
maxlength
JQuery validation is triggered initially on the
blur()
event and thereafter on the .keyup()
event, so you need to trigger that event on the other textbox (e.g. $('#textBoxAddress').trigger('keyup');
). But what is the point of this - why copy the text from one textbox to another?– user3559349
Sep 13 '18 at 4:46
blur()
.keyup()
$('#textBoxAddress').trigger('keyup');
@TetsuyaYamamoto, understand and thank you.
– akkapolk
Sep 13 '18 at 5:24
That is typically done by using a checkbox (i.e check it if same address as above)
– user3559349
Sep 13 '18 at 5:28
2 Answers
2
Use maxlength
attributes:
maxlength
@Html.TextBox("textBoxFillToAnothertextBox", null, new id = "textBoxFillToAnothertextBox",maxlength = 250 , @class = "form-control", placeholder = "Fill in to apply for textBoxAddress" )
Or
$('#textBoxFillToAnothertextBox').on('keyup change', function()
if ($('#textBoxFillToAnothertextBox').val().length > 250)
$('#ADDRESS').text('error message');
else
$('#textBoxAddress').val($('#textBoxFillToAnothertextBox').val());
);
@shree, understand and thank you.
– akkapolk
Sep 13 '18 at 5:25
Currently I use concept from @StephenMuecke.
jQuery:
$('#textBoxFillToAnothertextBox').on('keyup change', function ()
$('#textBoxAddress').val($('#textBoxFillToAnothertextBox').val());
$('#textBoxAddress').focus();
$('#textBoxFillToAnothertextBox').focus();
);
It's work for me.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
ValidationMessageFor
used to validate input controls created by HTML helper in server-side. If you want to check textbox length which value modified in client-side, use$('#textBoxAddress').val().length
and put it inside jQuery validator settings.– Tetsuya Yamamoto
Sep 13 '18 at 4:17