Stefan Motz
asked on
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:
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>
I would appreciate your help.
ASKER
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:
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
ASKER
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.
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.
ASKER
Thank you very much for your help!
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.
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.
ASKER
Hi Norie,
I've posted the question here:
https://www.experts-exchange.com/questions/29095463/JavaScript-validation-of-a-group-of-dropdown-menus.html
Thank you for your help
I've posted the question here:
https://www.experts-exchange.com/questions/29095463/JavaScript-validation-of-a-group-of-dropdown-menus.html
Thank you for your help
Open in new window
Open in new window