jquery validate errorPlacement in select2.js [duplicate] - html

This question already has answers here:
select2 and jquery validation not working properly
(3 answers)
How to make select2 work with jquery.validation plugin?
(15 answers)
Error placement, Select2
(2 answers)
Closed 4 years ago.
When error replacement in select2 is showing up above the box, I want the error replacement below the box. Here is my HTML:
<form id="form" class="form_required">
<select type="text" id="test1" class="form-control select2" required>
<option value="">&nbsp();</option>
<option value="1">1</option>
</select>
<button type="button" onclick="insert()">Submit</button>
</form>
Here is the JavaScript:
$('.select2').select2()
$(".form_required").validate({
highlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-error').addClass('has-success');
},
errorPlacement: function (error, element) {
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
}
else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
error.insertAfter(element.parent().parent());
}
else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.appendTo(element.parent().parent());
}
else {
error.insertAfter(element);
}
}
});
function insert(){
if ($('#form').valid()){
alert('OK');
}
}
1) 2)
When not using select2 the error replacement is showing up below box the like number 2 but when using select2 error replacement is showing up above the box..

If you check the source code of the select2 library, you'll see that the select2-container (container responsible for the select2 dropdown) is inserted right after the select element. Here's the part of the code which does that:
Select2.prototype._placeContainer = function ($container) {
$container.insertAfter(this.$element);
var width = this._resolveWidth(this.$element, this.options.get('width'));
if (width != null) {
$container.css('width', width);
}
};
which gets us to easily place the error message from the jquery-validate after this select2-container. Here's the change in the errorPlacement option:
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
}
....
And a snippet showing the results:
$('.select2').select2({container: 'body'})
$(".form_required").validate({
highlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-error').addClass('has-success');
},
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
} else if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
}
else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
error.insertAfter(element.parent().parent());
}
else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.appendTo(element.parent().parent());
}
else {
error.insertAfter(element);
}
}
});
form {
margin: 20px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<form id="form" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test1" class="form-control select2" required>
<option value=""> </option>
<option value="1">test1</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert()">Submit</button>
</div>
</form>
<form id="form2" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test2" class="form-control" required>
<option value=""> </option>
<option value="1">test2</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert2()">Submit</button>
</div>
</form>
<script>
function insert(){
if ($('#form').valid()){
alert('OK');
}
}
function insert2(){
if ($('#form2').valid()){
alert('OK');
}
}
</script>
Hope this helps.

Related

How to hide a label of a dropdown with hide() function

I have 2 dropdowns (EmployeeType, Type) and a textbox (Rate) and I just want to hide "Type" and "Rate" according to the selected value of "EmployeeType". I've done it with no issues to the "Rate", but for the "Type" only the dropdown hides not the label. Can anyone help me to hide the label of the dropdown as well?
<div class="control-group">
<label class="control-label" for="Designation">Employee Type</label>
<div class="controls">
<select name="Designation" onchange="ajaxRate(this)" id="Designation">
<option value="">--Select Employee Type--</option>
<option value="Center">Center</option>
<option value="Visiting">Visiting</option>
<option value="Other">Other</option>
</select>
<span id="ajax_img3"></span>
</div>
<!-- designation-->
<br/>
<!-- 2019-03-18 Visiting Type -->
<div class="control-group">
<label class="control-label" for="Type">Type</label>
<div class="controls">
<select name="Type" id="TypeValue">
<option value="">--Select Type--</option>
<option value="Yes">Regular</option>
<option value="No">Visiting</option>
</select>
</div>
</div>
<div class="control-group" id='rate'>
<label class="control-label" for="Rate">Hourly Rate</label>
<div class="controls">
<input type="number" name="Rate" id='rateValue'/>
</div>
</div>
#include('includes.footer')
<script>
$(document).ready(function () {
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x) {
if (x.value == 'Center') {
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
} else {
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
}
}
$( document ).ready(function()
{
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x)
{
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
$("#TypeValue").parents(".control-group").show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
$("#TypeValue").parents(".control-group").hide();
}
}
You have to hide full div using parents or you have to add any id to main div to hide.
function ajaxRate(x) {
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').hide();
}
}

Show DIV only when a specific radio item is selected

I have this code which works without a problem with select options:
HTML
<select onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option value="Apple">Apple</option>
<option value="Orange">Orange</option>
<option value="Bananna">Bananna</option>
</select>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
Script
<script>
function show(aval) {
if (aval == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
}
</script>
Any ideas how can I make this code to work if those were radio options?
Try it like this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" id="Apple"/> Apple <br>
<input type="radio" name="radioName" id="Orange" /> Orange <br>
<input type="radio" name="radioName" id="Bananna" /> Bananna <br>
</form>
<div id="hiddenDiv" style="display:none">Hidden Question about Oranges</div>
<script>
$('#myForm input').on('change', function() {
if ($(this).attr('id') == "Orange") {
hiddenDiv.style.display='inherit';
} else {
hiddenDiv.style.display='none';
}
});
</script>
My solution with JQuery:
$(function () {
$("select").click(function () {
var validOption = $("select > option[value='Orange']");
if (validOption[0].selected) {
$("#hiddenDiv").show();
} else {
$("#hiddenDiv").hidde();
}
});
});

Directive for confirm dialog when click submit button

I have a form to create an employee.
When I click the submit button I need to confirm with the confirm dialog box as 'do you want to submit' form?
In angularjs and bootstrap design, if it's possible.
<form ng-submit="create()">
<input type="text" ng-model="fstname">
<input type="text" ng-model="lstname">
<input type="submit" value="submit">
</form>
When I click the submit button, if the form is valid then I want to confirm with confirm box. If I click ok, that means I want to submit the form else it should not submit.
finally i found my answer for this question.my view page code look like below
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl" class="modal-demo">
<br>
<form name="form" novalidate>
<input type="text" style="width:200px" class="form-control" name="name" ng-model="text.name" required>
<span ng-show="form.$submitted && form.name.$error.required">name is required</span><br>
<input type="text" style="width:200px" class="form-control" name="name1" ng-model="text.name1" required>
<span ng-show="form.$submitted && form.name1.$error.required">name1 is required</span><br>
<input type="submit" ng-click="open(form)">
</form><br>
<p ng-hide="!msg" class="alert" ng-class="{'alert-success':suc, 'alert-danger':!suc}">{{msg}}</p>
</div>
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">Your Details</h3>
</div>
<div class="modal-body" id="modal-body">
<p>Are you sure, your name <b>{{name }}</b> is going to submit?
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ok()">Submit</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
</script>
<script>
and my controller code looks below
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope,$uibModal, $log, $document) {
var $ctrl = this;
$scope.animationsEnabled = true;
$scope.text = {};
$scope.open = function (form) {
if(form.$valid)
{
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
values: function () {
return $scope.text;
}
}
});
modalInstance.result.then(function () {
console.log($scope.text);
$scope.msg = "Submitted";
$scope.suc = true;
}, function(error) {
$scope.msg = 'Cancelled';
$scope.suc = false;
});
}else{
alert('');
}
};
});
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope,$uibModalInstance, values) {
var $ctrl = this;
$scope.name= values;
$scope.ok = function () {
$uibModalInstance.close('ok');
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
my updated plunkr here demo

Using Bootstrap Modals in google apps script

I am trying to have a form that allows users to check a status in our DB, and on submit I want write to our db, and then return a bootstrap css modal that has their info from querying another table in the db. All works great except the modal, it just loads the div without creating the pop-up panel. I am assuming I am not loading a dependency library that it needs but still learning so figured I'd see feedback on my code and any potential solutions.
My code is:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
form { margin:0 auto; }
input { display:block; }
</style>
<div class="container-fluid">
<form id="LiRoForm">
<div class="page-header">
I have a an image header here
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter your name..">
</div>
<div class="form-group">
<label>Email:</label>
<input type="email" class="form-control validate[required,custom[email]]" id="email" name="email" placeholder="Enter your email..">
</div>
<div class="form-group">
<label>Application ID:</label>
<input type="text" class="form-control" id="appID" name="appID" placeholder="Please enter the full application ID">
</div>
<input type="submit" class="btn-primary btn-lg" id="submit-button" value="Check Status"
onclick="submitForm(); return false;">
</form>
</div>
<div id="dialog" title="Modal Title" class="modal fade" role="dialog" aria-labelledby="dialog" aria-hidden="true"></div>
<script>
function submitForm() {
var form = $('#LiRoForm')[0];
checkForm(function(){
$("#submit-button")[0].value = 'Checking Status...';
google.script.run
.withSuccessHandler(function onSuccess(data) {
var data = JSON.parse(data);
if (data == '') {
$("#dialog").html("<div>Our records indicate you have no data in the system</div>");
}
else {
$("#dialog").html("<div>Our system indicates you have the following data:</div>");
for(var x in data) {
$("#dialog").append("<div>"+data[x].FileType+"</div>");
}
}
$("#submit-button")[0].value = 'Check Status';
$("#dialog").modal('show');
})
.withFailureHandler(function(error) {
alert(error);
})
.checkStatus(form);
});
}
function checkForm(callback) {
var name = $('#name').val();
var email = $('#email').val();
var appID = $('#appID').val();
var emailPattern = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (name == '' || email == '' || appID == '' || fileType == '') {
alert("Please fill in all fields");
return;
}
else if (file == '') {
alert("Please select a file");
return;
}
else if (emailPattern.test(email) == false) {
alert("Please enter a valid email address");
return;
}
// Validate AppID
google.script.run
.withSuccessHandler(callback)
.withFailureHandler(function(error) {
alert(error);
})
.checkID(appID);
}
</script>
Thanks #Mogsdad switching the html mode from NATIVE to IFRAME was the solution, as well as matching up that the style css and bootstrap scripts matched up:
Not Working:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
Working
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

How to add a class to a div on a certain option value

I was wondering how to add a class to a div if the option has the value selected. For example I'm making a website for my cousin's business, we want an easy way to shuffle through menus.
<code>
<div id="select"><form>
Menu:
<select name="select" class="form-control">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
<input type="submit" value="Submit">
</form>
</code>
I would like if the value is breakfast to hide the div drinks and show the div breakfast, and also have it reversible. Thank you here's my code on JSFiddle http://jsfiddle.net/t5R9s/
Here it is
$(document).ready(function() {
$(".form-control").change(function() {
if($(this).val() == "drinks") {
$("#drinks").show();
$("#breakfast").hide();
} else {
$("#drinks").hide();
$("#breakfast").show();
}
});
});
Here is the FIDDLE.
You can use javascript. Do you want to do it when you hit submit or just after you make the selection?
[
Fiddle
]
<script type="text/javascript">
function changeClass(){
var myVariable = document.getElementById('control').value;
if(myVariable == "drinks"){
document.getElementById('drinks').className = "";
document.getElementById('breakfast').className = "hide";
}
else if(myVariable == "breakfast"){
document.getElementById('drinks').className = "hide";
document.getElementById('breakfast').className = "";
}
}
</script>
<div id="select">
<form>Menu:
<select name="select" class="form-control" id="control" onChange="changeClass()">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
</form>
</div>
<div id="drinks">
<h2 align="center">Drinks</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>
<!--Breakfast table-->
<div id="breakfast" class="hide">
<h2 align="center">Breakfast</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>