javascript - ASP.NET Web Forms - Client side validation not working when field populated via jquery? -


i have following asp.net webform address field required field. address field populated via jquery value of location field.

when click submit without filling in fields, client side validation fires , error message displayed below address control.

if enter value location field, jquery fires , address field populated value previous error message doesn't clear - clears when manually enter value?

<div>     <label>location</label>     <asp:textbox id="location" runat="server" cssclass="form-control" clientidmode="static" placeholder="enter location"></asp:textbox> </div> <div>     <label >address</label>     <asp:textbox id="address" runat="server" cssclass="form-control" clientidmode="static" placeholder="enter address"></asp:textbox>     <asp:requiredfieldvalidator id="requiredfieldvalidator5" runat="server" errormessage="address required" display="dynamic" controltovalidate="address" cssclass="field-validation-error" enableclientscript="true"></asp:requiredfieldvalidator> </div> <asp:button id="submit" runat="server" text="save" onclick="btnsubmit_click" cssclass="btn btn-primary" />    <script>     $("#location").on('change', function (e) {         var location =  $("#location").val();         $("#address").val(location);     }); </script> 

how can fix if client side error displayed below control, clears error message if validation logic fullfilled via jquery in example above?

when manually enter value in address input change event gets triggered when input loses focus or press enter. doesn't happen when using jquery val() method.

to work have 2 options, either manually trigger change event:

option 1: jquery solution triggering change event

$("#location").on('change', function (e) {     var location =  $("#location").val();     $("#address").val(location).trigger("change"); }); 

or alternatively can manually call webforms validation methods directly. bit more risky there's no guarantee these methods won't change in future, i've included completeness can useful in situations.

option 2: call webforms validation method trigger revalidation

$("#location").on('change', function (e) {     var location =  $("#location").val();     $("#address").val(location);     if (typeof (page_clientvalidate) == 'function') {         page_clientvalidate();     } }); 

i've chosen page_clientvalidate method here revalidate validators given validation group (validation group not supplied here it's not applicable). 1 of simpler , safer methods call, in more complex scenarios may want validatorvalidate() or 1 of other other methods in webuivalidation.js instead. happy expand on that, think it's out of scope of question.

i prefer option 1 it's simpler , less open being broken in future webforms changes. hope helps.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -