Solved

Programming Validation Controls

Posted on 2008-06-19
9
172 Views
Last Modified: 2010-04-15
I am trying to program validation controls on my web page.  How would I program it so it only validates when the drop down list box is the United States?  See below:

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
                    ControlToValidate="newZipcode" ErrorMessage="*Incorrect Zip Code"
                    ValidationExpression="\d{5}(-\d{4})?" ValidationGroup="NewUserGroup"></asp:RegularExpressionValidator>
0
Comment
Question by:VBBRett
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21825498
Use a custom validator and implement its event handler to do the test. Set RegularExpressionValidator3 Enabled=false in page_Load.

CustomValidator_ServerValidate( blah, blah)
{
if (ddl1.Selelcted.Value == "United States")
{
  RegularExpressionValidator3.Enabled = true;
  RegularExpressionValidator3.Validate();
}
}

Or you could just eliminate RegularExpressionValidator3 and do the regex test yourself in your custom validator. That would be cleaner.


 
0
 

Author Comment

by:VBBRett
ID: 21825640
Would this work?  countries is the drop down list box.  RegularExpressionValidator is the validation control for zip codes.  Basically if the user is from out of the country, I don't want the US zip code validation control to activate.  So, is what I am doing correct?See below:

 protected void countries_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (countries!= "United States")
        {
            this.RegularExpressionValidator3.Enabled = false;
        }
    }
0
 

Author Comment

by:VBBRett
ID: 21825709
With the code that I posted, i get the error CS0019: Operator '!=' cannot be applied to operands of type 'int' and 'string'
0
 

Author Comment

by:VBBRett
ID: 21825779
Can you please expand on custom validators please?  Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:ororiole
ID: 21827655
you could use the selected index changed event, its a good idea, if you dont mind it posting back. This will fix your error:
protected void countries_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (countries.SelectedValue != "United States")
        {
            this.RegularExpressionValidator3.Enabled = false;
        }
        else
           this.RegularExpressionValidator3.Enabled = true;
    }
and that takes care of the problem. However, if you wish to use a custom validator here is an example.


        <asp:CustomValidator ID="CustomValidator1" runat="server" 

            ControlToValidate="TextBox1" ErrorMessage="Invalid Zip Code" 

            onservervalidate="CustomValidator1_ServerValidate" 

            ValidationGroup="NewUserGroup"></asp:CustomValidator>
 

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)

        {

            args.IsValid = true; //default, but set true anyway to illustrate the example
 

            if (DropDownList1.SelectedValue != "UnitedStates")

                return;
 

            System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"^\d{5}(-\d{4})?$"); //here is your zip code regex

            if (!regx.IsMatch(args.Value))  //args.Value contains the contents of the control you are validating. you could also just write TextBox1.Text, especially if you are validating several controls here

                args.IsValid = false;    //setting this false causes validation to fail so you will get the error message       

        }

Open in new window

0
 

Author Comment

by:VBBRett
ID: 21833811
Hi, I tried to get this to work and nothing happened when I selected either different countries and nothing happened when I selected the united states.  When typing anything in the box whether it is correct format or incorrect format, nothing came to fruition.
0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21835584
This is a server side validator, it validates when the page postsback. click your submit button or whatever you use to submit and then it will validate and set the message for the error summary. If you want it to do client side  validation then you would write it in javascript, and enter the name of the javascript function in a property on the custom validator.

So now you have 3 options,
1. You could just disable the zip validator in the dropdown sel index changed event.
2. You can have it custom validate on the server and do all the checking in one place.
3. You can write a custom javascript routine and do all the checking there.
0
 

Author Comment

by:VBBRett
ID: 21847224
How would I write the custom Javascript routine?  Can you help me out with how I can do this or do you need more information from me in order to get this handled?
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21849376
I can show you the skeleton. Then enter the name of the function in your custom validation control where the property is something like ClientValidationFunction. You will see it.

function ClientValidator(src, args)
{
  args.isvalid = false;
   //do your tests
  args.isvalid = true;
}

I am not a javascript guru so I dont know how to do the javascript for a regex. But if you post this:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true; //default, but set true anyway to illustrate the example
 
            if (DropDownList1.SelectedValue != "UnitedStates")
                return;
 
            System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"^\d{5}(-\d{4})?$"); //here is your zip code regex
            if (!regx.IsMatch(args.Value))  //args.Value contains the contents of the control you are validating. you could also just write TextBox1.Text, especially if you are validating several controls here
                args.IsValid = false;    //setting this false causes validation to fail so you will get the error message      
        }
in the javascript zone and ASP.net zone again then someone can set you up.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# replace string in void with dR["myData"].ToString() 3 41
C# Json POSt to Rest API 4 37
C# Single Form 8 28
C# Comparing String Arrays. 5 26
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

895 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

14 Experts available now in Live!

Get 1:1 Help Now