Solved

Use custom validator in webform and server side code

Posted on 2014-07-25
3
200 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
Comment Utility
Add a custom validator control to the page and point it at the textbox you want to validate
0
 

Author Comment

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

Accepted Solution

by:
Randy Poole earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error viewing ASP page 12 65
Regex code,how to do this? 3 20
regex code how to filter this sql email combo 3 15
XML & .net 5 0
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

763 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

6 Experts available now in Live!

Get 1:1 Help Now