Solved

Using a CustomValidator to check two controls

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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