Search code examples
asp.netwebformsasp.net-validators

Validation Not Firing On TextBox


So, basically I have multiple TextBoxes on my page and some of them have validations with RequiredFieldValidator.
I am using OnTextChanged event on txtMobileNo TextBox Field to fetch the data from database.
If the number exists in the database the the rest of my TextBoxes filled from server side.

Now, I am facing here a problem: my MobileNo Text Field skips the RegularExpressionValidator validation even if it is wrong.
I want to redirect to server side only if my number matches the validation logic.

<div class="col-md-4">
    <asp:Label ID="lblfortxtMobileNo" runat="server" Text="Mobile No"></asp:Label>
    <asp:TextBox ID="txtMobileNo" runat="server" AutoPostBack="true" OnTextChanged="txtMobileNo_TextChanged" MaxLength="10"></asp:TextBox>
    <asp:RequiredFieldValidator ID="rqfvtxtMobileNo" runat="server" ErrorMessage="*" ControlToValidate="txtMobileNo" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
    <asp:RegularExpressionValidator
        ID="RegularExpressionValidator1"
        runat="server"
        ErrorMessage="Please Enter Valid Number"
        ControlToValidate="txtMobileNo"
        Display="Dynamic"
        ForeColor="Red"
        ValidationExpression="^([0-9]{10})$"
    ></asp:RegularExpressionValidator>
</div>
<div class="col-md-4">
    <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
    <asp:TextBox ID="txtName" runat="server" AutoPostBack="true"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtName" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
</div>
<div class="col-md-4">
    <asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />
</div>

When I am set CauseValidation = "true" the result is somewhat what I want .
But it checks all the other RequiredFieldValidator validation as well before calling OnTextChanged function.
I just want to check Regular Expression Validation at this point for txtMobileNo and based on the result wants to display error.


Solution

  • Cross check your regex id it is look like an duplicated id here.