required is not working - required

i am trying to set the required if there is no input, but it does not work.
Even if there is no input the button works and does not show the required alert.
How can i fix this issue, can someone help me to fix this !
Html --
<form id="searchform">
<div style="padding: 0px 10px 0px 0px;" class="col-md-3">
<input type="text" id= "nameSearch" name="nameSearch" class="form-control" required >
</div>
<button type="submit" class="btn btn-default" id="submitSearch">
<i class="fa fa-search"></i> {% trans %}Search{% endtrans %}
</button>
</form>
Script --
$("#submitSearch").on('click', function () {
var data = {};
data['nameSearch'] = $('#nameSearch').val();
$.ajax({
url: '/hello/search',
type: 'post',
data: data,
success: function (data) {
...........

Add a small anonymous jQuery function to check the HTML5 validity status of a form..
(function( $ ){
$.fn.isValid = function() {
return checkValidity();
};
})(jQuery);
and then use this in your submit
$('#submitSearch').on('click', function(e) {
if(this.isValid()){
} else {
}
});

Please note that adding the required class wont prevent your form from being submitted.
It looks like you're using Bootstrap, which offers input "validation state" classes, like has-error. But this just changes the look of the input, not functionality. See here: http://getbootstrap.com/css/#forms-control-validation
Have a look at HTML5 form validation: http://www.the-art-of-web.com/html/html5-form-validation/
And also look whether it will work on the browser you are working with: http://caniuse.com/#feat=form-validation

try adding " in class="form-control":
<form id="searchform">
<div style="padding: 0px 10px 0px 0px;" class="col-md-3">
<input type="text" id= "nameSearch" name="nameSearch" class="form-control" required >
</div>
<button type="submit" class="btn btn-default" id="submitSearch">
<i class="fa fa-search"></i> {% trans %}Search{% endtrans %}
</button>
</form>

Your quotations:
<input type="text" id= "nameSearch" name="nameSearch" class="form-control required >
</div>
See this:
<input type="text" id= "nameSearch" name="nameSearch" class="form-control" required >
</div>

Related

LESS/CSS: Hide all subsequent elements below an element that does not contain a certain attribute

I have the following structure:
<div id="1" class="MyMainSection">
<div id=2" class "MyRow">
<input id="5" type="button" disabled>
<input id="4" type="button" disabled>
<input id="7" type="button" disabled>
</div>
<div class "MyRow">
<input id="12" type="button" disabled>
<input id="8" type="button">
<input id="10" type="button" disabled>
</div>
<div class "MyRow">
<input id="11" type="button" disabled>
<input id="1" type="button" disabled>
<input id="9" type="button" disabled>
</div>
</div>
My goal is to hide all elements (via "display:none") below the only non-disabled button. The LESS selector should not purely look for "id=8" as the id is generated dynamically.
I had something in mind like
.MyMainSection {
.MyRow {
input:not([disabled]) ~ input {
display:none;
}
}
}
but this doesn't work...
Can someone help me ?
UPDATE:
I was not precise enough.
My selection statement already contained the input element and not the class "input", so this should not stop us.
But my issue is that I want to hide ALL subsequent elements in my HTML code (no matter if they are input, MyRow, MyMainSection or anything else that comes below that one non-disabled button).
The problem is that you are selecting a class of input and not the input itself. Simply remove the . from your selector to get
.MyMainSection {
.MyRow {
input:not([disabled]) ~ input {
display:none;
}
}
}

Width override of easyAutocomplete plugin inside input-group-addon

I have an input box in a form. I am using the easyautocomplete plugin in this input box. When I have input box outside the input-group-addon the input box has proper width. But, when I add it inside the input-group-addon, the width is reduced/collapsed. (See screenshots)
HTML With input-group-addon
<div class="form-group col-md-9">
<label for="taskname">Task Name</label>
<div class="input-group">
<span class="input-group-addon"><i class="fas fa-anchor"></i></span>
<input class="form-control" id="taskname" name="taskname" required autocomplete="off" maxlength="50" placeholder="Name" value="<?php echo h($dataset['taskname']); ?>">
</div>
<small class="form-text text-muted">Select Task name from auto suggestion or type your own </small>
</div>
HTML Without input-group-addon
<div class="form-group col-md-9">
<label for="taskname">Task Name</label>
<input class="form-control" id="taskname" name="taskname" required autocomplete="off" maxlength="50" placeholder="Name" value="<?php echo h($dataset['taskname']); ?>">
<small class="form-text text-muted">Select Task name from auto suggestion or type your own </small>
</div>
Javascript
$("#taskname").easyAutocomplete(tasknameoption);
var tasknameoption = {
url: "/task/tasklistapi.php",
getValue: "tasklistname",
adjustWidth: false,
requestDelay: 500,
list: {
maxNumberOfElements: 10,
match: {enabled: true},
onSelectItemEvent: function() {
var value = $("#taskname").getSelectedItemData().iscomp;
$("#iscomp").val(value).trigger("change");
}
}
};
When the above javascript is commented, the form looks as below
Things I have Tried (but did not work):
Adding adjustWidth: false in the options as per Github
Added inline CSS as per SO and SO
Actually it is easier than that.
Just add classes to the initializer.
Example:
var options = {
data: ["Cyclops", "Storm", "Mystique", "Wolverine", "Professor X", "Magneto"],
theme: "dark", **cssClasses: 'col'**
};
$("#theme").easyAutocomplete(options);
Apparently the EasyAutocomplete plugin wraps the <input class="form-control"> into an additional <div class="easy-autocomplete"> element. With that modification it breaks the Bootstrap markup necessary for proper input-group styling. Unfortunately, I did not find an option for EasyAutocomplete to control how it changes the DOM.
In order to overcome this, you have two workarounds that I can think of.
You could modify the Bootstrap css to address the new markup. The easiest way to do this would be to recompile the modified scss files into a new css. However this could also lead to an error prone result.
The second option however is to further modify the markup a bit with javascript.
I think the latter one is far more the easiest way to do it, so I demonstrate that in the example below.
// Init `easyAutocomplete`
var tasknameoption = {
adjustWidth: false,
data: ["blue", "green", "pink", "red", "yellow"]
};
$("#taskname").easyAutocomplete(tasknameoption);
// Fixing markup
$('.easy-autocomplete').closest('.input-group').each(function(i, inputGroup) {
$(inputGroup).removeClass('input-group');
$autocomplete = $(inputGroup).find('.easy-autocomplete');
$(inputGroup).find('.input-group-addon').prependTo($autocomplete);
$autocomplete.addClass('input-group');
});
.easy-autocomplete-container {
top: 100%;
}
<div class="form-group col-md-9">
<label for="taskname">Task Name</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-anchor"></i></span>
<input class="form-control" id="taskname" name="taskname" required autocomplete="off" maxlength="50" placeholder="Name">
</div>
<small class="form-text text-muted">Select Task name from auto suggestion or type your own </small>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/easy-autocomplete.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/jquery.easy-autocomplete.js"></script>
Based on your initial markup, I guess you are using Bootstrap 4 Beta 2. It is important to note that Beta 3 is available, and changes include the styling of the input group component too. So, with the most recent beta version the markup and the “reset” script would look like as below.
// Init `easyAutocomplete`
var tasknameoption = {
adjustWidth: false,
data: ["blue", "green", "pink", "red", "yellow"]
};
$("#taskname").easyAutocomplete(tasknameoption);
// Fixing markup
$('.easy-autocomplete').closest('.input-group').each(function(i, inputGroup) {
$(inputGroup).removeClass('input-group');
$autocomplete = $(inputGroup).find('.easy-autocomplete');
$(inputGroup).find('.input-group-prepend').prependTo($autocomplete);
$autocomplete.addClass('input-group');
});
.easy-autocomplete-container {
top: 100%;
}
<div class="form-group col-md-9">
<label for="taskname">Task Name</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">
<i class="fa fa-anchor"></i>
</span>
</div>
<input class="form-control" id="taskname" name="taskname" required autocomplete="off" maxlength="50" placeholder="Name" aria-describedby="basic-addon1">
</div>
<small class="form-text text-muted">Select Task name from auto suggestion or type your own </small>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/easy-autocomplete.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/jquery.easy-autocomplete.js"></script>
Note: As you can see in the examples, it is still required to set adjustWidth: false for EasyAutocomplete options. Also, there is a fix for the class .easy-autocomplete-container to display properly.

How to remove a tooltip when the following input disabled

How can I remove the Boostrap tooltip when the following input is disabled?
<label class="label" data-toggle="tooltip" title="Test">
<input type="checkbox" value="disabled" class="input" disabled>
<label>
First, I tried to detect whether it has a tooltip, but it seems not working. And then, set it to ''.
if($(this).is('[data-toggle="tooltip"]')) {
alert('tooltip');
$('[data-toggle="tooltip"]').tooltip('title', ''});
}
Any idea? Thanks.
you have to check your input is disabled or not first,
and then going back to label to remove the title.
$(document).ready(function(){
$(".label").hover(function() {
if($(this).children().attr('disabled')) {
$(this).attr('title','123'); /*just change 123 to empty '' then can remove your title */
}
if($(this).attr('title')) $(this).removeAttr('title');
/*<del>$('.showtitle').text($(this).attr('title'));*/
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<label class="label" data-toggle="tooltip" title="Test">
<input type="checkbox" value="disabled" class="input" disabled/>
</label>
<label class="showtitle"></label>
You have to check for all input that have disabled attribute or not. but for this solution following code will work:
$(document).ready(function(){
$(".label").hover(function() {
if($(this).attr('data-toggle')=="tooltip" ) {
$(this).attr('title','');
}
});
});
<label class="label" data-toggle="tooltip" title="Test">
<input type="checkbox" value="disabled" class="input" disabled/>
</label>

Meteor: Using an HTML Template to add to the DOM.

I'm building an app with Meteor.js, and I've got a form where I would like to be able to allow the user to add a new row to the form when they click a button (button.addExperience). I'm using an HTML Template to populate each row of the form.
How would I have the template (experienceRow) rendered each time the user clicks the button?
See example code below:
<body>
<form>
{{> experienceRow }}
</form>
<button class="addExperience">Add Experience</button>
</body>
<template name="experienceRow">
<div id={{experienceNumber}} class='experienceRow'>
<input type="text" placeholder="name" value="" class="name">
<input type="text" placeholder="address" value="" class="address">
<input type="text" placeholder="phone" value="" class="phone">
</div>
</template>
You will need to use an each block along with a reactive variable. That variable could be a ReactiveVar, a session varaible, a local collection, etc. Here's an example implementation using a ReactiveVar to hold an array of ids:
html
<body>
{{> experienceForm }}
</body>
<template name="experienceForm">
<form>
{{#each experienceIds}}
{{> experienceRow }}
{{/each}}
</form>
<button class="addExperience">Add Experience</button>
</template>
<template name="experienceRow">
<div id={{this}} class='experienceRow'>
<input type="text" placeholder="name" value="" class="name">
<input type="text" placeholder="address" value="" class="address">
<input type="text" placeholder="phone" value="" class="phone">
</div>
</template>
js
Template.experienceForm.onCreated(function() {
this.experienceIds = new ReactiveVar(Random.id());
});
Template.experienceForm.helpers({
experienceIds: function() {
return Template.instance().experienceIds.get();
}
});
Template.experienceForm.events({
'click .addExperience': function(e, template) {
e.preventDefault();
var ids = template.experienceIds.get();
ids.push(Random.id());
template.experienceIds.set(ids);
}
});
Note that you'll need to meteor add reactive-var for this to work.
Recommended reading: scoped reactivity.

bootstrap - avoid gap between controls when window gets maximized

I want to keep both the text input and the button input sticking together in following code. Right now, when the window gets maximized, there is space between the two controls, how to avoid this?
#{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<div class="input-group">
<input type="text" name="message" id="message" class="form-control" />
<span class="input-group-btn">
<button type="submit" name="sendmessage" id="sendmessage" class="btn btn-default">Send</button>
</span>
</div>
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
#section scripts {
<!--Script references. -->
...
ok, i am a newbie when it comes to "modern UI techniques" and now i feel a little shame because i figured out that adding:
input[id=message] {
max-width: 100%;
}
in site.css solved the problem! :-)

Resources