Solved

Use custom validator in webform and server side code

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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