Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Use custom validator in webform and server side code

Posted on 2014-07-25
3
203 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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.

837 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