Solved

validate certain fields based upon dropdown selection

Posted on 2013-12-04
2
491 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
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 32

Author Closing Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

746 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

8 Experts available now in Live!

Get 1:1 Help Now