• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 51
  • Last Modified:

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.
0
romsom
Asked:
romsom
  • 4
  • 4
1 Solution
 
NorieVBA ExpertCommented:
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
 
romsomAuthor 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
 
NorieVBA ExpertCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
NorieVBA ExpertCommented:
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
 
romsomAuthor 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
 
romsomAuthor Commented:
Thank you very much for your help!
0
 
NorieVBA ExpertCommented:
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
 
romsomAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now