Skip to content

Instantly share code, notes, and snippets.

@acbrent25
Created October 26, 2018 15:06
Show Gist options
  • Save acbrent25/510845ff9a4d260ba08a0e19fc599ec1 to your computer and use it in GitHub Desktop.
Save acbrent25/510845ff9a4d260ba08a0e19fc599ec1 to your computer and use it in GitHub Desktop.
Custom Validation Messaging for Form Selects with Bootstrap and jQuery
<form action="/" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="select_validation">Select Validation: *</label>
<select class="form-control" name="select_validation" id="select_validation">
<option value="">Make Selection</option>
<option value="selectOne">selectOne</option>
<option value="selectTwo">selectTwo</option>
<option value="selectThree">selectThree</option>
</select>
</div>
</form>
<button class="btn btn-success sub-btn" type="submit">Save</button>
jQuery(document).ready(function($){
$('.sub-btn').click(function(){
var selectOne = $('#selectOne');
var selectTwo = $('#selectTwo');
var selectThree = $('#selectThree')
var alertMessage = [];
if (selectOne.val() === ''){
alertMessage.push("Please Select a selectOne");
$('.alert').text(alertMessage).show();
return false;
}
if (selectTwo.val() === ''){
alertMessage.push("Please Select a selectTwo");
$('.alert').text(alertMessage).show();
return false;
}
if (selectThree.val() === ''){
alertMessage.push("Please Select a selectThree");
$('.alert').text(alertMessage).show();
return false;
}
else return;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment