4

I have used a required field validator followed by a regular expression validator but the required field validator is not working.....

<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px" CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" ValidationGroup="Valtxt" TabIndex="2" Rows="4"> </asp:TextBox> <asp:RegularExpressionValidator ID="regValSummary" runat="server" ControlToValidate="txtSummary" Display="Dynamic" ValidationExpression="[^&lt;&gt;&amp;#!]*" ValidationGroup="Valtxt"> Invalid characters(&lt;&gt;&amp;#!) are not allowed </asp:RegularExpressionValidator> <asp:RequiredFieldValidator ID="reqvalSummary" runat="server" ControlToValidate="txtSummary" ErrorMessage="Summary is required" ValidationGroup="Valtxt" Display="Dynamic"> </asp:RequiredFieldValidator> 

can anyone sees the problem???

4
  • 3
    Do you have the validation group on any submit buttons? Commented Nov 1, 2010 at 13:11
  • But the RegularExpressionValidator does work? Commented Nov 1, 2010 at 13:13
  • ensure the validation group of the validation summary matches? Commented Nov 1, 2010 at 13:14
  • FYI: Your ValidationExpression does not look right at all. Those characters should not be html encoded. Also, you need to reference the beginning and end of the string to ensure that all characters are taken into account. I think you want to use something more like ValidationExpression="^[^<>&#!]*$" Commented Nov 1, 2010 at 13:22

5 Answers 5

6

The RequiredFieldValidator is triggered by the client side onchange event. It sounds like you're expecting it to be triggered by the onblur event, such that tabbing away from the textbox would fire the validation.

Before jumping to that, I suspect this is what you are seeing and to validate that it's actually working you need to trigger onchange. To do so, enter some text in the textbox, tab away, tab back to it, clear the textbox, then tab away once more. You should now see the RequiredFieldValidator's error message since it's contents have changed.

Back to the onblur issue. To accomplish that behavior you could add the onblur attribute in your code-behind and have it call the ValidatorValidate(...) JavaScript method as follows:

void Page_Load(object sender, EventArgs e) { txtSummary.Attributes.Add("onblur", "ValidatorValidate(" + reqvalSummary.ClientID + ")"); } 

Alternately, you could accomplish the same thing in markup. First, add this script block:

<script type="text/javascript"> function rfvBlur() { var rfv = document.getElementById("<%= reqvalSummary.ClientID %>"); ValidatorValidate(rfv); } </script> 

Second, update the <asp:TextBox.../> markup by adding onblur="rfvBlur()" so that it now looks like this:

<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px" CausesValidation="true" CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" ValidationGroup="Valtxt" TabIndex="2" Rows="4" onblur="rfvBlur()" /> 

Yet another option is to validate the entire ValidationGroup by adding the following attribute to your <asp:TextBox.../> markup (no additional script block needed):

onblur="Page_ClientValidate('Valtxt')" 
Sign up to request clarification or add additional context in comments.

2 Comments

Jerry Scannell's comment: "I'm trying to incorporate the rfvBlur() recommended but I can't figure out what ClientID was referencing in your example."
@PeterO. thanks for posting Jerry's comment. @Jerry: the ClientID is being called off of a asp:RequiredFieldValidator with an ID of reqvalSummary. You can see this validator in the original question, which is validating the txtSummary TextBox control. Use whatever ID you've given to your RequiredFieldValidator control. You'll need to do this for each control/validator you intend this to work with.
4

Adding this line to <appSettings> section of web.config worked for me (I had a problem when all validators stopped working when project was upgraded to .NET 4.5):

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> 

Source:

http://forums.asp.net/t/1876231.aspx?ASP+Net+4+5+Validation+Controls+not+working+with+AJAX+ToolkitScriptManager1

Comments

1

Why don't you change the regular expression of the "RegEx" validator to check if the textbox is empty instead of use another validator?

Anyway probabily you have not specify ValidationGroup="Valtxt" for the button or the control that raise the postback. Just add ValidationGroup="Valtxt" to the button or the server control that raise the post to the page

1 Comment

As odd as it may sound, the RegularExpressionValidator is not triggered on empty text and must be paired up with a RequiredFieldValidator. From MSDN: "If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control."
0

I put the following at the top of my btn_Click event handler (to prevent further code execution) and upon 'return', the rfv messages show...

 Page.Validate("your validation group"); if (!Page.IsValid) { return; } 

Comments

-1
<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px" CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" TabIndex="2" Rows="4"> </asp:TextBox> <asp:RegularExpressionValidator ID="regValSummary" runat="server" ControlToValidate="txtSummary" ErrorMessage="Invalid characters(&lt;&gt;&amp;#!) are not allowed" Text="*" ValidationExpression="[^&lt;&gt;&amp;#!]*" ValidationGroup="Valtxt"> </asp:RegularExpressionValidator> <asp:RequiredFieldValidator ID="reqvalSummary" runat="server" ControlToValidate="txtSummary" ErrorMessage="Summary is required" Text="*" ValidationGroup="Valtxt"> </asp:RequiredFieldValidator> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.