?
Solved

Programming Validation Controls

Posted on 2008-06-19
9
Medium Priority
?
177 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:VBBRett
ID: 21825779
Can you please expand on custom validators please?  Thanks!
0
 
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 2000 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

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!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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