Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login page on which i have applied validation on text fields, the problem is when user open login page and suddenly switch to another page, without entering anything in the fields. validation summary pops up and says field cannot be null....


Here is my code...
ASP.NET
<asp:TextBox ID="TxtBxEmail" runat="server" CssClass="TextBoxClass" Width="60%" TextMode="SingleLine" ToolTip="Enter Your Email Address"  AutoCompleteType="Disabled" placeholder="E.g. username@domain.com"></asp:TextBox>

<asp:RequiredFieldValidator ID="rvemail" runat="server" Display="None" ErrorMessage="Please Enter Email" Text="*" ControlToValidate="TxtBxEmail" Font-Names="Calibri" Font-Size="small" ForeColor="Red" ></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="revemail" runat="server" Display="None" ControlToValidate="TxtBxEmail"  ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$" ErrorMessage="invalid email format" Text="*" Font-Names="Calibri" Font-Size="12px" ForeColor="Red"></asp:RegularExpressionValidator>


<asp:TextBox ID="TxtBxPassword" runat="server" CssClass="TextBoxClass" Width="60%" TextMode="Password" ToolTip="Enter Your Password"  AutoCompleteType="Disabled" ></asp:TextBox>

<asp:RequiredFieldValidator ID="rvPassword" runat="server" Display="None" ControlToValidate="TxtBxPassword" ErrorMessage="Please Enter Password" Text="*" Font-Names="Calibri" Font-Size="small" ForeColor="Red" ></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="revPassword" runat="server" Display="None"  ControlToValidate="TxtBxPassword" ValidationExpression="(?=^.{5,15}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{:;'?/>.<,])(?!.*\s).*$" ErrorMessage="Password must be 5-15 characters long with at least one numeric,</br>one upper case character and one special character." Text="*" Font-Names="Calibri" ForeColor="Red" Font-Size="12px"></asp:RegularExpressionValidator> 


<asp:Button ID="BtnLogin" runat="server" Text="Login" CssClass="BtnClass"  Height="40px" Width="100px" OnClick="BtnLogin_Click"   /> 

<asp:ValidationSummary ID="vs" runat="server" Font-Names="Calibri" ForeColor="Red" Font-Size="Small" HeaderText="Warnings........" />



and the code behind:

C#
public partial class Login_Login : System.Web.UI.Page
{
   

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try {
                // rest of my code....
            }
            catch (Exception ex) {
               
            }
            finally {
}
            }//end if

        
        else
        {

            //Page Validation Does not Occurs Successfully.
        }
    }

  

   
}
Posted

1 solution

You should set the ValidationGroup Property in all the validators and the Button on which you want to validate the fields.

So, what happens is the Validators will only work on that Button Click.

To know more - ValidationSummary.ValidationGroup Property[^].
 
Share this answer
 
v2
Comments
Nirav Prabtani 14-Jun-14 6:01am    
my 5+
Thanks Nirav. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900