views:

518

answers:

3

I have a regular expression validation control initialized to validate a textbox control. I want users to be able to enter U.S. Currency values ($12,115.85 or 1500.22 etc.). I found a regular expression off of regexlib website that does the trick. The validation control seems to be working except for one crucial thing. If invalid data is entered, the validation text dispalys (a red "*" next to the textbox), but the page will still submit and the error message won't pop up... I thought that the error message is supposed to display and the page won't submit if the validation control detects invalid data. Isn't this automatic with ASP .NET? I have searched extensively on how to create validation controls, but haven't found anything different than what I am already doing. Can anyone tell me what I am doing wrong here?

<asp:TextBox ID="txtActualCost" runat="server" Width="120px" CausesValidation="true"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="regExValActualCost"
                         ControlToValidate="txtActualCost"
                         Text="*"
                         ValidationExpression="^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"
                         ErrorMessage="Please enter a valid currency value for 'Actual Cost'"
                         Display="Dynamic"
                         EnableClientScript="true"
                         runat="server" />
A: 

in your Submit_Click (or whatnot), you need to check Page.IsValid. ASP.NET doesn't automatically assume you have validators on the page. You might have to call Page.Validate() first.

steve_d
Thanks. I added a check for Page.IsValid and functioning how I want it
Rudi Ramey
+2  A: 

Hi Rudi,

Yes, on the client side the validation should be automatic. There are a few things you will want to check.

Is there some other javascript on the page that is erroring and causing the validation script to fail?

Are you using validation groups? If so, make sure that the submit button is included in this validation group.

Does your submit button have the property "CausesValidation" set to false?

Are you sure that your validation expression is correct? Try downloading a tool such as Expresso in order to make sure your expression behaves as you expect with different input.

Another point to check, the regex validator does not fire on an empty string. Are you testing with an empty input and expecting the expression to fail? If so, then please try providing an additional requiredfieldvalidator to catch this extra fail condition.

Finally, you can call the client side page validation yourself to check that what's happening. Try placing a temporary button on the page with an OnClientClick attribute like:

<input  type="button" value="Fire Validation" name="foo" onClick="Page_ClientValidate();alert('Valid: '+Page_IsValid);return false;" />

This will fire the validation checking on the client side and provide an alert with the outcome of the validation.

Brian Scott
Thank you. A lot of good suggestions here for me to look into :-)
Rudi Ramey
+1  A: 

It should fire off just fine. Do the following:

  1. Remove the CauseValidation="true" on your TextBox as you don't need it and make sure this property is not set in your Button.
  2. Verify that your button isn't attached to a validation group or if it is, your validator must be set to the same group.
  3. If you want a pop up you will need to add a ValidationSummary and set it to show a popup. The ValidationSummary control uses the value stored in the validator's ErrorMessage property.
  4. Also make sure to do a if (Page.IsValid) in your button handler so that a server side check happens as well incase javascript is disabled.

Other that those things, it should be working as you expected.

Kelsey
Thank you looks like I need to add the validation summary!
Rudi Ramey