Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

validate certain fields based upon dropdown selection

Posted on 2013-12-04
2
Medium Priority
?
509 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 2000 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

715 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