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
Solved

validate certain fields based upon dropdown selection

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MVC5 - Two different JQuery libraries are loading???? 2 25
how to avoid redundand ajax calls 5 41
How to reload page and grab querystring 2 22
2 separate CSS animations 2 16
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…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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)

839 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