jQuery form validation for dropdown menu

Hi Experts,
I have a form with 4 dropdown menus. I would like to have some validation which tells the user "Please make your selection before you submit".
If they don't select at least one dropdown, they should not be able to submit the form.
This is my form:
<form method="POST" action="../Search/SearchResults.asp">
<select name="Affiliation">
  <option value=""></option>
  <option value="VER">VER</option>
  <option value="TIR">TIR</option>
  <option value="SAV">SAV</option>
  <option value="ASE">ASE</option>
</select>
<select name="DeviceName">
  <option value=""></option>
  <option value="100968_FILE1">100968_FILE1</option>
  <option value="101324_FILE1">101324_FILE1</option>
  <option value="101325_FILE1">101325_FILE1</option>
  <option value="101333_FILE1">101333_FILE1</option>
</select>
<select name="AliasName">
  <option value=""></option>
  <option value="021850">021850</option>
  <option value="024803">024803</option>
  <option value="026294">026294</option>
  <option value="050403">050403</option>
</select>
<select name="DeviceAccountID">
  <option value=""></option>
  <option value="100968">100968</option>
  <option value="101324">101324</option>
  <option value="101333">101333</option>
  <option value="101335">101335</option>
</select>
<input type="submit" value="Submit" />
</td></tr>
</table>
</form>

Open in new window

I would appreciate your help.
romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Perhaps something like this.
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>

<form id="mydropdowns" method="POST" action="../Search/SearchResults.asp">
  <select name="Affiliation" id="Affiliation" class="required">
  <option value=""></option>
  <option value="VER">VER</option>
  <option value="TIR">TIR</option>
  <option value="SAV">SAV</option>
  <option value="ASE">ASE</option>
</select>
  <select name="DeviceName" id="DeviceName" class="required">
  <option value=""></option>
  <option value="100968_FILE1">100968_FILE1</option>
  <option value="101324_FILE1">101324_FILE1</option>
  <option value="101325_FILE1">101325_FILE1</option>
  <option value="101333_FILE1">101333_FILE1</option>
</select>
<select name="AliasName" id="AliasName" class="required">
  <option value=""></option>
  <option value="021850">021850</option>
  <option value="024803">024803</option>
  <option value="026294">026294</option>
  <option value="050403">050403</option>
</select>
<select name="DeviceAccountID" id="DeviceAccountID" class="required">
  <option value=""></option>
  <option value="100968">100968</option>
  <option value="101324">101324</option>
  <option value="101333">101333</option>
  <option value="101335">101335</option>
</select>
  <br/><br/>
  <input type="submit" value="submit" />
</form>

Open in new window


$("#mydropdowns").validate({
  messages: {
    Affiliation: {
      required: "Please select an affiliation.",
    },
    DeviceName: {
      required: "Please select a device."
    },
    AliasName: {
    	required: "Please select an alias."
    },
    DeviceAccountID: {
    	required: "Please select an Account ID"
    }
  }
});

Open in new window

0
romsomIT DeveloperAuthor Commented:
Thank you so much for your response.
I tried it on my web page, but it still lets me submit the form without selecting a dropdown. Maybe I missed something. This is the code of my web page:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
    <script type="text/javascript">
$("#mydropdowns").validate({
  messages: {
    Affiliation: {
      required: "Please select an affiliation.",
    },
    DeviceName: {
      required: "Please select a device."
    },
    AliasName: {
    	required: "Please select an alias."
    },
    DeviceAccountID: {
    	required: "Please select an Account ID"
    }
  }
})
</script>
  
</head>

<body>

<form id="mydropdowns" method="POST" action="../Search/SearchResults.asp">
  <select name="Affiliation" id="Affiliation" class="required">
  <option value=""></option>
  <option value="VER">VER</option>
  <option value="TIR">TIR</option>
  <option value="SAV">SAV</option>
  <option value="ASE">ASE</option>
</select>
  <select name="DeviceName" id="DeviceName" class="required">
  <option value=""></option>
  <option value="100968_FILE1">100968_FILE1</option>
  <option value="101324_FILE1">101324_FILE1</option>
  <option value="101325_FILE1">101325_FILE1</option>
  <option value="101333_FILE1">101333_FILE1</option>
</select>
<select name="AliasName" id="AliasName" class="required">
  <option value=""></option>
  <option value="021850">021850</option>
  <option value="024803">024803</option>
  <option value="026294">026294</option>
  <option value="050403">050403</option>
</select>
<select name="DeviceAccountID" id="DeviceAccountID" class="required">
  <option value=""></option>
  <option value="100968">100968</option>
  <option value="101324">101324</option>
  <option value="101333">101333</option>
  <option value="101335">101335</option>
</select>
  <br/><br/>
  <input type="submit" value="submit" />
</form>

  
</body>

</html>

Open in new window

0
NorieAnalyst Assistant Commented:
What I posted is based on something I saw quite a while ago, might be out of date.

Try changing class="required" to just required, that's what I did below and it works, to some extent - the main thing missing is the custom message for each dropdown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/screen.css">
<script src="../lib/jquery.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script>
	$().ready(function() {
	
		$("#mydropdowns").validate({
		  messages: {
			Affiliation: {
			  required: "Please select an affiliation.",
			},
			DeviceName: {
			  required: "Please select a device."
			},
			AliasName: {
				required: "Please select an alias."
			},
			DeviceAccountID: {
				required: "Please select an Account ID"
			}
		  }
		});

	});
	</script>

</head>
<body>

<form id="mydropdowns" method="POST" action="../Search/SearchResults.asp">
  <select name="Affiliation" id="Affiliation" required>
  <option value=""></option>
  <option value="VER">VER</option>
  <option value="TIR">TIR</option>
  <option value="SAV">SAV</option>
  <option value="ASE">ASE</option>
</select>
  <select name="DeviceName" id="DeviceName" required>
  <option value=""></option>
  <option value="100968_FILE1">100968_FILE1</option>
  <option value="101324_FILE1">101324_FILE1</option>
  <option value="101325_FILE1">101325_FILE1</option>
  <option value="101333_FILE1">101333_FILE1</option>
</select>
<select name="AliasName" id="AliasName" required>
  <option value=""></option>
  <option value="021850">021850</option>
  <option value="024803">024803</option>
  <option value="026294">026294</option>
  <option value="050403">050403</option>
</select>
<select name="DeviceAccountID" id="DeviceAccountID" required>
  <option value=""></option>
  <option value="100968">100968</option>
  <option value="101324">101324</option>
  <option value="101333">101333</option>
  <option value="101335">101335</option>
</select>
  <br/><br/>
  <input type="submit" value="submit" />
</form>
</div>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

NorieAnalyst Assistant Commented:
Here's something that doesn't rely on a plugin.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/screen.css">
<script src="../js/jquery-3.3.1.js"></script>
<script>
	$(document).ready(function(){
	
		$('#mydropdowns').submit(function(e){ 
		
		var cancel = false;
		
        var department = $("#msg_affiliation");
        var msg = "Please select affiliation.";
            if ($('#Affiliation').val() == "") {
                department.html(msg);
                cancel = true
            }
			else
			{
				department.html("");
			}
			
		var department = $("#msg_device");
        var msg = "Please select device name.";
            if ($('#DeviceName').val() == "") {
                department.html(msg);
                cancel = true
            }
			else
			{
				department.html("");
			}
			
		var department = $("#msg_alias");
        var msg = "Please select alias.";
            if ($('#AliasName').val() == "") {
                department.html(msg);
                cancel = true
            }
			else
			{
				department.html("");
			}
			
		var department = $("#msg_id");
        var msg = "Please select Account ID.";
            if ($('#DeviceAccountID').val() == "") {
                department.html(msg);
				cancel = true
            }
			else
			{
				department.html("");
			}
			
		if (cancel)
		{
			e.preventDefault();
			cancel = false
            return false;
		}
        });

	});
	</script>

</head>
<body>

<form id="mydropdowns" method="POST" action="../Search/SearchResults.asp">
  <select name="Affiliation" id="Affiliation">
  <option value=""></option>
  <option value="VER">VER</option>
  <option value="TIR">TIR</option>
  <option value="SAV">SAV</option>
  <option value="ASE">ASE</option>
</select>
<div style="color:red;" id="msg_affiliation"></div>

  <select name="DeviceName" id="DeviceName">
  <option value=""></option>
  <option value="100968_FILE1">100968_FILE1</option>
  <option value="101324_FILE1">101324_FILE1</option>
  <option value="101325_FILE1">101325_FILE1</option>
  <option value="101333_FILE1">101333_FILE1</option>
</select>
<div style="color:red;" id="msg_device"></div>

<select name="AliasName" id="AliasName">
  <option value=""></option>
  <option value="021850">021850</option>
  <option value="024803">024803</option>
  <option value="026294">026294</option>
  <option value="050403">050403</option>
</select>
<div style="color:red;" id="msg_alias"></div>

<select name="DeviceAccountID" id="DeviceAccountID">
  <option value=""></option>
  <option value="100968">100968</option>
  <option value="101324">101324</option>
  <option value="101333">101333</option>
  <option value="101335">101335</option>
</select>
<div style="color:red;" id="msg_id"></div>

  <br/><br/>
  <input type="Submit" value="submit" />
  
</form>
</div>
</body>
</html>

Open in new window

0
romsomIT DeveloperAuthor Commented:
Thank you so much for your solutions. Unfortunately this makes all 4 fields required.
My goal is to make it impossible for the user to submit the form if they don't select at least one dropdown. They can pick any of the 4 dropdowns, and be able to submit the form.
Sorry for the misunderstanding, I should have emphasized the "at least one" part. I will accept the first solution, and post the question again after I rephrase the requirement.
Also, the last example is working in Chrome, but in Internet Explorer I was able to submit the form without selecting any of the dropdowns.
0
romsomIT DeveloperAuthor Commented:
Thank you very much for your help!
0
NorieAnalyst Assistant Commented:
Sorry about that, I must have misread the question.

It should be straightforward to rejig the logic, for the last piece of code anyway.

I'll keep my eye out to see if you post another question.
0
romsomIT DeveloperAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.