4

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?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • Could you just apply a filtered input field similar to this (http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) question and not have to worry about stripping the dollar sign out yourself? – Roman Apr 07 '10 at 17:31

3 Answers3

6

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.

womp
  • 115,835
  • 26
  • 236
  • 269
0

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

Raja
  • 3,608
  • 3
  • 28
  • 39
  • Not a bad idea. My only concern is that users might get confused when they enter a $ and nothing shows up. At least if they enter it and it disappears when they click off they will be more likely to assume the web page is doing it rather than a problem with their keyboard or something. – Abe Miessler Apr 07 '10 at 18:09
0

Try calling jquery's .change() method.

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

Sometimes calling just .val() is not enough.

jmbmage
  • 2,487
  • 3
  • 27
  • 44