Solved

Programming Validation Controls

Posted on 2008-06-19
9
175 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
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.

 

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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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