user1828759 user1828759 - 3 months ago 10
ASP.NET (C#) Question

RegularExpressionValidator works but RequiredFieldValidator does not

I am trying to set RegularExpressionValidator and RequiredFieldValidator for my textbox in an asp.net web form. My RegularExpressionValidator works and give the error message but my RequiredFieldValidator does not show the error message when I click the submit button. I have also tried adding ValidationGroup to my textbox and button and RegularExpressionValidator and RequiredFieldValidator, but nothing changed. Could you please help we with it?
By the way I have already added

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


to my webconfig. Thank you.

<input type="text" id="user" placeholder="username" runat="server"/>
<asp:RegularExpressionValidator runat="server" ErrorMessage="Username is not suitable" Display="Dynamic"
ValidationExpression="^([a-zA-Z0-9]{0,8})$" ID="req1"
ForeColor="Red" ControlToValidate="user" />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="Username required" ID="req2"
ForeColor="Red" ControlToValidate="user"/>
<button id="submitbtn" type="button" runat="server">Sign up</button>

Answer

oh buddy,you have made a typo mistake,type must be submit

<button id="submitbtn" type="submit" runat="server">Sign up</button>

Type="submit" will post the page back to the server whereas type doesnt post the page to the server.

Another way is using Page.isValid property.

Comments