Solved

validate certain fields based upon dropdown selection

Posted on 2013-12-04
2
495 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 33

Author Closing Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 51
Why is this panel heading creating extra space 2 52
Error in JQuery 5 54
trying to resize an Image, using CFimage tag. 8 32
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

832 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