Abe Miessler
Abe Miessler

Reputation: 85126

Possible to fire asp.net validation from jQuery?

I have a form with several text boxes on it. I only want to accept floats, but it is likely that users will enter a dollar sign. I'm using the following code to remove dollar signs and validate the content:

jQuery:

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

asp.net validation:

<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 a dollar sign in the TextBox "tb" and changes focus the validation happens first and THEN the jQuery removes the dollar sign. Is it possible to have the jQuery run first or to force the validation to run again after the jQuery executes?

Upvotes: 4

Views: 2447

Answers (3)

user3180309
user3180309

Reputation:

Try calling jquery's .change() method.

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

Sometimes calling just .val() is not enough.

Upvotes: 0

womp
womp

Reputation: 116987

All the ASP.Net validators have a client-side API that you can hook into. You can see some documentation and discussion on it here.

Specifically what you probably want to do is call the javascript function

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

to make the validator re-run it's client-side validation routine and update its display.

Upvotes: 6

Raja
Raja

Reputation: 3618

Instead of using the change event why don't you use KeyPress event and handle the $ sign there. By this way the user wont be able to type in $.

HTH

Upvotes: 0

Related Questions