user1828759 user1828759 - 1 year ago 189
ASP.NET (C#) Question

RegularExpressionValidator works but RequiredFieldValidator does not

I am trying to set RegularExpressionValidator and RequiredFieldValidator for my textbox in an 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

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download