Solved

Using a CustomValidator to check two controls

Posted on 2014-04-02
1
191 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 39

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now