Maria Torres
asked on
Using a CustomValidator to check two controls
I defined a customvalidator that checks that the first control (USERID) is not part of the second control (PASSWORD). If it is then an error message is displayed indicating that password cannot be made up of the UserID. This logic is run at the server-side.
When I test my logic, the system displays the error message and proceeds to save the record to the database. Eventhough, I specified the args.IsValid to be false, the record is still written to the database. Can anyone help me at getting the system not to save the record if the function failed? Thank you.
Below is snippet of my code:
(located in the ASPX file)
<asp:CustomValidator ID="cuvPassword" runat="server"
ErrorMessage="Password cannot contain User ID"
OnServerValidate="CheckIfI dInPswd"
Display="None" ValidationGroup="vsShow">* </asp:Cust omValidato r>
(located in the ASPX.CS file)
protected void CheckIfIdInPswd(object source, ServerValidateEventArgs args)
{
string s1 = this.txtUserId.Text;
string s2 = this.txtPassword.Text;
if (s2.IndexOf(s1) != -1)
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
When I test my logic, the system displays the error message and proceeds to save the record to the database. Eventhough, I specified the args.IsValid to be false, the record is still written to the database. Can anyone help me at getting the system not to save the record if the function failed? Thank you.
Below is snippet of my code:
(located in the ASPX file)
<asp:CustomValidator ID="cuvPassword" runat="server"
ErrorMessage="Password cannot contain User ID"
OnServerValidate="CheckIfI
Display="None" ValidationGroup="vsShow">*
(located in the ASPX.CS file)
protected void CheckIfIdInPswd(object source, ServerValidateEventArgs args)
{
string s1 = this.txtUserId.Text;
string s2 = this.txtPassword.Text;
if (s2.IndexOf(s1) != -1)
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.