Solved

validate certain fields based upon dropdown selection

Posted on 2013-12-04
2
502 Views
Last Modified: 2013-12-05
I'm currently using jQuery to validate a simple user profile form and was wondering how I could validate certain fields based upon a dropdown selection.

basically i have a country dropdown with 2 options: US and Canada. If US is selected, the states dropdown appears and the provinces is hidden. if canada is chosen, then the provinces dropdown is shown and the states dropdown is hidden. I want to be able to validate whichever field is shown.

I tried the following code and applied a class called "info" to each dropdown (selStates and selProvinces) but it doesn't work. any ideas greatly appreciated

            $("#frmEditProfile").validate({
                submitHandler: function (form) {
                    form.submit();
                },
                ignore: "text :hidden, .select2-input, #s2id_autogen1, #s2id_autogen2",
                rules: {
                    txtFirstName: { required:true, maxlength:50 },
                    txtLastName: { required:true, maxlength:50 },
                    txtEmail: {
                        required: true,
                        email: true,
                        maxlength:50,
                        remote: {
                            url: "editProfile.asp",
                            data: { emailExists: "y" }
                        }
                    },
                    txtCity: { required:true, maxlength:50 },
                 //   selState: "required",
                    txtDOB: "required",
                    txtMotto: {
                        maxlength: 50
                    },
                    txtAboutMe: {
                        maxlength: 200
                    }
                },
                messages: {
                    txtFirstName: { required: "Please enter your first name", maxlength: "Please limit your first name to 50 charactors or less." },
                    txtLastName: { required: "Please enter your last name", maxlength: "Please limit your last name to 50 charactors or less." },
                    txtCity: "Please enter your city",
                    txtEmail: {
                        required: "Please enter a valid email address",
                        remote: "This email already exists, please try another email"
                    },
                    txtDOB: "Please enter in your Date Of Birth",
                    txtMotto: "Please keep your motto to 50 charactors or less",
                    txtAboutMe: "Please keep your About Me description to 200 charactors or less"
                }
            });

            $('.info').each(function() {
                $(this).rules('add', {
                    required: true,
                    messages: {  required:  "You must select a state or province"  }
                });
            });

Open in new window

0
Comment
Question by:Big Monty
[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 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39698130
Test page : http://jsfiddle.net/8zHCv/

    $(document).ready(function() {
            $("#frmEditProfile").validate({
                submitHandler: function (form) {
                    alert("ok");
                }
            });

            $('#country').change(function() {
                if($(this).val()=="US") {
                    $("#selStates").show().rules('add', { required: true, messages: { required: "You must select a state" } });
                    $("#selProvinces").hide().rules('remove');
                }
                else {
                    $("#selStates").hide().rules('remove');
                    $("#selProvinces").show().rules('add', { required: true, messages: { required: "You must select a province" } });
                }
                $("#frmEditProfile").valid();
            }).change();
    });

Open in new window

0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 39699277
beautiful! thanks :)
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

632 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