Solved

Using a CustomValidator to check two controls

Posted on 2014-04-02
1
204 Views
Last Modified: 2014-04-03
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="CheckIfIdInPswd"
                    Display="None" ValidationGroup="vsShow">*</asp:CustomValidator>


(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;
        }
0
Comment
Question by:CarmenMTorres
1 Comment
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39973195
don't return

instead:
 if (s2.IndexOf(s1) != -1)
       args.IsValid = false;
else
       args.IsValid = true;

Open in new window

in your save function:

cuvPassword.Validate();

if (cuvPassword.IsValid)
 save();

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question