Click here to Skip to main content
14,390,702 members
Rate this:
Please Sign up or sign in to vote.
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: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:

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^].
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100