• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Programming Validation Controls

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
VBBRett
Asked:
VBBRett
  • 5
  • 4
1 Solution
 
ororioleCommented:
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
 
VBBRettAuthor Commented:
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
 
VBBRettAuthor Commented:
With the code that I posted, i get the error CS0019: Operator '!=' cannot be applied to operands of type 'int' and 'string'
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
VBBRettAuthor Commented:
Can you please expand on custom validators please?  Thanks!
0
 
ororioleCommented:
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
 
VBBRettAuthor Commented:
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
 
ororioleCommented:
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
 
VBBRettAuthor Commented:
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
 
ororioleCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now