Mandatory field validator does not work

I used the required field validator, followed by the regular expression validator, but the required field validator does not work .....

<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 see the problem ???

+3
source share
4 answers

RequiredFieldValidatortriggered onchangeby a client-side event . It looks like you expect this to be triggered by an event onblur, so stacking from a text box will result in rejection of the check.

, , , , , , onchange. , , , , . RequiredFieldValidator, .

onblur. , onblur ValidatorValidate(...) JavaScript :

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

. script:

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

-, <asp:TextBox.../>, onblur="rfvBlur()", :

<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()" />

- ValidationGroup, <asp:TextBox.../> ( script ):

onblur="Page_ClientValidate('Valtxt')"
+6

<appSettings> web.config ( , , .NET 4.5):

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

:

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

+3

"RegEx", , , ?

In any case, you did not specify ValidationGroup = "Valtxt" for the button or control that increase the postback. Just add ValidationGroup = "Valtxt" to the button or server control that brings up the message on the page

+1
source
<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>
-1
source

Source: https://habr.com/ru/post/1772460/


All Articles