Solved

Use custom validator in webform and server side code

Posted on 2014-07-25
3
202 Views
Last Modified: 2014-07-30
I have a web form that has several asp.net validation controls on it. I have one textbox that is using a function to validate the textbox contents against a SQL server database.   I have the coding complete but I want to use a validator control to indicate if the textbox contents passed or failed so that it becomes part of the page validation along with the other validation controls on the form.

I tried setting the customvalidator.isvalid = false but it does not see to work.

How can I do this?
0
Comment
Question by:Mike_Stevens
  • 2
3 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40219342
Add a custom validator control to the page and point it at the textbox you want to validate
0
 

Author Comment

by:Mike_Stevens
ID: 40219350
How will it know if the text in the textbox is valid?
0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40219425
the validator runs a function on the code behind and you specify if it is valid as such in this instance it will call the function ValidatePasswordConfirmation:
aspx page
                    <label for="ConfirmPassword">
                        Password again:</label></dt><dd>
                    <asp:TextBox ID="ConfirmPassword" runat="server" AutoCompleteType="Disabled"
                            CssClass="PersonName" TextMode="Password" MaxLength="100" />
                            <asp:RequiredFieldValidator ID="reqConfirmPassword" runat="server" ControlToValidate="ConfirmPassword"
                                Display="None" ErrorMessage="Password confirmation can't be blank" />
                            <asp:CustomValidator ID="CustomValidatorPasswordConfirmation" runat="server"
                                Display="None" ErrorMessage="Password confirmation doesn't match." OnServerValidate="ValidatePasswordConfirmation"></asp:CustomValidator>

Open in new window

cs
     
  protected void ValidatePasswordConfirmation( object sender, ServerValidateEventArgs e )
        {
            bool result = true;
            if (Password.Text != ConfirmPassword.Text)
            {
                result = false;
                CustomValidatorPasswordConfirmation.ErrorMessage = "Password confirmation doesn't match original password";
            }
            if (!string.IsNullOrEmpty(Password.Text) && result)
            {
                string o = lic.validatepw(Password.Text);
                result = o.Length == 0;
                if (!result) CustomValidatorPasswordConfirmation.ErrorMessage = o;
            }
            e.IsValid = result;
        }

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

Suggested Solutions

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

770 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