Is it possible to perform asp.net validation from jQuery?

I have a form with several text fields on it. I just want to accept floats, but most likely users will enter a dollar sign. I use the following code to remove dollar signs and check the contents:

JQuery

$("#<%= tb.ClientID %>").change(function() { var ctrl = $("#<%= tb.ClientID %>"); ctrl.val(ctrl.val().replace('$','')) }); 

Checking asp.net:

 <asp:CompareValidator ID="CompareValidator4" runat="server" Type="Double" ControlToValidate="tb" Operator="DataTypeCheck" ValidationGroup="vld_Page" ErrorMessage="Some error" /> 

My problem is that when someone enters the dollar sign into the TextBox "tb" and the changes are focused, the check is done first, and THEN jQuery removes the dollar sign. Is it possible to start jQuery first or force a validation after jQuery?

+4
source share
3 answers

All ASP.Net validators have a client-side API that you can connect to. You can see the documentation and discussion here .

In particular, what you probably want to do is call the javascript function

 ValidatorValidate(document.getElementById("<%= myValidator.ClientID %>")); 

to make the validator re-run its client-side validation procedure and update its display.

+6
source

Instead of using the change event, why don't you use the KeyPress event and don't handle the $ sign. Thus, the user will not be able to enter $.

NTN

0
source

Try calling the jquery .change() method.

Like this:
$("#Symbol").val(""); $("#Symbol").change();

Sometimes calling just .val() not enough.

0
source

Source: https://habr.com/ru/post/1306276/


All Articles