views:

59

answers:

2

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?

+3  A: 

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
hmm so i added this to my jQuery code: ValidatorValidate("<%= CompareValidator4.ClientID %>"); but it errors out. Is this the right way to call it?
Abe Miessler
@Abe Miessler: Try calling it this way http://tinyurl.com/yblv8pm
R0MANARMY
I hate you, and thanks. For the record I DID Google it. Just thought that what I posted would to the same thing.
Abe Miessler
@Abe Miessler - sorry, I should have made that function call clearer. Yeah, you have to pass the actual element in, not a string. I'll update the code.
womp
A: 

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
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