?
Solved

Use custom validator in webform and server side code

Posted on 2014-07-25
3
Medium Priority
?
208 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
[X]
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
  • 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 2000 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

765 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