HTML5 - Select multi required-checkbox - html

I've writen some code here: http://jsfiddle.net/anhtran/kXsj9/8/
Users have to select at least 1 option on the group. But it makes me must click all of them to submit the form. How to do this issue without javascript?
Thanks for any help :)

I think this html5 attribute is only supposed to define which fields are required.
You cant put logic in to say "at least one is required".
You will need to add custom javascript for this to work (and/or have validation on the server side).
hope this helps...

The ideal answer would be to use HTML5 and the required attribute as part of a select element, like so:
<form method="post" action="processForm.php">
<label for="myLanguages">What languages can you program in?</label>
<br>
<select id="myLanguages" multiple required>
<option value="C#">C#
<option value="Java">Java
<option value="PHP">PHP
<option value="Perl">Perl
<option value="Haskell">Haskell
</select>
<br>
<input type="submit" value="Submit">
</form>
Yes, I know they are not checkboxes, but the end functionality is exactly what you want. Sadly, neither IE 9 nor Safari 5 currently have support for the required attribute. Chrome 13 and FF 5, however, do. (Tested on Win 7)

I thought it'd be possible, to do in part, what you were after using CSS. Not using the required attribute but to instead hide the submit button if nothing was selected.
You'd get rid of the required attributes and use CSS similar to this:
input[type=submit] {
display:none;
}
input[type=checkbox]:checked ~ input[type=submit] {
display:block;
}
However, that particular CSS is not working on my version of Google Chrome. I've made a question regarding it here. It seems to be working fine on my FF 3.6 though.

You can't do this without javascript.
What you can do is select a default option and set it as selected.
But it can't assure you that a checkbox is selected when the form is submitted.

Related

Disable autofill in Chrome 63 [duplicate]

This question already has answers here:
Disabling Chrome Autofill
(68 answers)
Closed 4 years ago.
I just updated my browser to Chrome Version 63.0.3239.84 (Official Build) (64-bit).
I then proceeded to go on my website, where I have a input box with autocomplete='off', yet I still get the following:
(You can see my inbuilt suggestion dropdown below it)
This never used to be the case. Nothing else has changed!
Why is this happening? Is this a bug in the new version of chrome? I have tried all other suggestions like autocomplete="false" or applying autocomplete=off to the form too. I have even tried to apply these with jquery after the page has loaded but also no luck.
I have tested this on multiple machines with the newest version of chrome on different operating systems. The issue persists.
Update Apr 2021:
Chrome and Firefox support autocomplete="off"
Safari continues to ignore autocomplete="off" and as far as I know there's no good solution fore Safari except to obfuscate the field name.
Update Feb 2018:
Thanks to #JamesNisbet for pointing this out in the comments.
According to the Chrome team, autocomplete="off" and autocomplete="false" will be ignored moving forward. This is not a temporary regression in Chrome.
Chrome will attempt to autofill any form fields that follow the WHATWG standard on autocomplete. With one exception, they ignore "off" and "false" values.
In summary, to disable autofill, use the autocomplete attribute with a value that is not on the WHATWG list.
Make your case why you think autocomplete="off" should not be ignored by Chrome in this Chromium thread.
Looks like a possible regression in Chrome 63. In Chrome's original autofill documentation:
In the past, many developers would add autocomplete="off" to their form fields to prevent the browser from performing any kind of autocomplete functionality. While Chrome will still respect this tag for autocomplete data, it will not respect it for autofill data. So when should you use autocomplete="off"? One example is when you've implemented your own version of autocomplete for search.
https://developers.google.com/web/updates/2015/06/checkout-faster-with-autofill
They do make a distinction between autocomplete and autofill, although it's not clear they are different.
Chrome, Safari, and Edge are all attempting to implement autofill but there is no clear standard. They look at the name attribute rather than an explicit, standardized attribute.
For now autocomplete="something-new" is a good workaround, although syntactically it makes no sense. This seems to work because the browser can't understand it.
We tried autocomplete="false" and autocomplete="off", neither work. But something Chrome doesn't understand, like autocomplete="disabled", does seem to work. Strange!
Update: this is working as of Chrome 72.
2019 It seems autocomplete="disabled" works again as of Chrome 72.
SINCE A LOT OF PEOPLE HAVE BEEN DOWNVOTING WITHOUT READING THE COMMENTS:
THIS NO LONGER WORKS IN CHROME AS OF 2018 / CHROME 63+
relevant: https://bugs.chromium.org/p/chromium/issues/detail?id=587466
Having autocomplete="false" instead of autocomplete="off" works, you can read more from the Chrome team as to why they did it
here:
https://www.chromium.org/developers/design-documents/form-styles-that-chromium-understands
https://bugs.chromium.org/p/chromium/issues/detail?id=468153
https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/zhhj7hCip5c
https://developers.google.com/web/updates/2015/06/checkout-faster-with-autofill
Looks like chrome looks for the closest "label" html tag to the input, and analyzes the label's value/html to affect the input's autofill.
The cleanest workaround I found to disable the input's autofill was this:
<label for="">Country</label>
<label for="" style="display: none;">hidden label to mislead chrome autocomplete</label>
<input ... />
I've managed to get a working "hack" in Chrome Version 65.0.3325.162 (Official Build) (64-bit).
I have to render an input field - hidden so it doesn't affect my page:
<input style="display:none;"/>
Then I render my password input field:
<input type="password" autocomplete="new-password" />
So my form ends up looking like:
<form>
<input style="display:none;" />
<input type="password" autocomplete="new-password" />
<input type="submit" />
</form>
Importantly, you cannot add a name or an id attribute to your password type input element, and you must have autocomplete="new-password"
After Chrome 63 it looks like they changed it to autocomplete="disabled"
I recommend you get a browser detecting library and for the rest of it use autocomplete="off"
As Chrome is never going to work properly and/or keeps changing its mind (I know its not human) the simplest solution to ensure autofill/autocomplete stops is to do the following on any inputs you dont want autofilled:
<input type='text' readonly onfocus="this.removeAttribute('readonly');" value=''/>
For Angular users, Since the autocomplete = 'off' ignore by new chrome versions, chrome developer suggests autocomplete= 'false | random-string', so the google chrome/modern browsers have 2 type of users helpers -
autocomplete='off' (which prevents last cached suggestions).
autocomplete = 'false | random-string' (which prevents autofill setting, since the 'random-string' is not known by the browser).
so what to do, in case of disabling both the annoying suggestions? Here is the trick:-
add autocomplete = 'off' in every input fields. (or simple Jquery).
$("input").attr('autocomplete', 'off');
Remove the <form name='form-name'> tag from HTML code and add ng-form = 'form-name' in your <div> container. adding ng-form="form-name" will also retain all your validations.
I feel terrible how different browsers use different options in a same functionality.
If it's chrome, use autocomplete="disabled" which handles both autocomplete and address based autofill (two separate things):
element.autocomplete = isGoogleChrome() ? 'disabled' : 'off';
You can get some insight on how to writ isGoogleChrome() from here
JavaScript: How to find out if the user browser is Chrome?
Current working solution using JQuery:
Removed name and id from the input I don't want autofill on and added an identifying class. I then created a hidden input with the field name and id I want. Then on form submit I copy the value from the field with no id and no name (finding it by my identifying class), into the hidden field with the name and id.
HTML
<form id="myform">
<input class="identifyingclass" value="">
<input class="hidden" id="city" name="city" value="">
<button type="submit">Submit</button>
</form>
Javascript
$('#myform').on('submit', function(e) {
$("#city").val($('.identifyingclass').val());
});
I reckon this should work as I don't see autofill latching on to anything other than an id or name.
Every answer I could find did not work for me. The most irritating part about my situation was how Android populated the notes field with a login name, resulting in erroneous notes being entered into the database.
I thought about how typing into the text input clears the Android autofill and the below trick worked. Note that simply clearing the value did not remove the autocomplete, I had to set the field's value. Immediately clearing the value after setting a value also did not work. The delay is needed for Android chrome to see a change and remove the filled in value.
Bonus: doing this action on the notes field caused Android to empty the other autocompleted elements in my form.
<script src="/js/jquery-1.12.1.min.js"></script>
<script>
$(function () {
$('#notes').val('--');
setTimeout(
function(){ $('#notes').val(''); }
, 2000
);
});
</script>
<input type='text' id='notes' name='notes' maxlength='250' size='17'>
The function setTimeout( callback, msec ) is javascript, thus a programmer could implement this without using jQuery.
I fixed this on my site by replacing the offending input element with
<p class="input" contenteditable="true"> </p>
and using jQuery to populate a hidden field prior to submission.
But this is a truly awful hack made necessary by a bad decision at Chromium.
I usually do this to hide the autofill icon:
<div style="width: 0; overflow:hidden;">
<input type="text" />
</div>
As Chrome will put the autofill icon on the first writable text field, the icon is placed on the hidden input field.
Note: Making the input field hidden-type or setting its display to 'none' doesn't seem to work.
autocomplete="off" works in the current Chrome 68 as well as in Chrome 63.
Demo.
Try to remove the "Id" of the input.
That's how i fixed it.

Using CSS "content" property breaks form interaction

As a usability feature I'd like to add highlights to the currently focused form element, in a similar fashion to the moneysupermarket forms. I'm working with an HTML pattern for building forms that I cannot change:
<div class="field">
<label for="name">
Name
</label>
<input id="name" type="text" value="Fred">
</div>
I thought I'd come up with a simple solution: I'll add focus and blur events to all inputs, selects and textareas that toggles a .hasfocus class on the wrapping div, then hook styling on that class.
Here's a demo on JSBin but if you're using Firefox or Chrome 40 you may see the issue I have: Adding the CSS content property to the div.field breaks the form interaction, it seems the initial mouse click is swallowed by the browser & you're unable to select the content. I'm stumped, I think it may be a browser bug. Any ideas how I can make the highlighted elements behave as the others?
Thanks in advance!
Edit:
Here's a screencast that demonstrates the issue: http://screencast.com/t/LlmwcsZ2qYM
I was unable reproduce in Chrome 42, but I did reproduce this in Firefox 37. It appears as though adding the highlighting to the label element fixes the issue:
.field.hasfocus label:before {
content:"";
...
}
Updated JS Bin: http://jsbin.com/kajifoquci/edit?html,css,js,output
You missed your test-x references.
The whole idea with the focus on the field through the labels, is the mapping of the for attribute with the id attribute. The name attribute alone is not going to cut it. Since id has to have a unique value on the page, I ended up at 5.
<label for="test5">
Test 5
</label>
<!-- name="test4" -->
<select id="test5">
<option value="">one</option>
<option value="">two</option>
<option value="">three</option>
</select>
So you can simplify your logic with:
$('input, select, textarea').on('focus blur', function (){
var el = $(this),
field = el.closest('.field');
field.toggleClass('hasfocus');
});
You can keep the .control.hasfocus:before to disable this effect on .control classes.
Hope this helps.
DEMO

Firefox draws red border on required select box

Firefox 21 draws red borders around required select-boxes when they are bound to an angularjs-model.
<select ng-model="color" ng-options="c.text for c in colors" required>
<option value="">-- choose color --</option>
</select>
Is there a way to let Firefox validate the input after selecting (or not selecting) an item?
→ A fiddle to demonstrate the problem
To get around this, you can disable the required style for when the form is pristine only:
.ng-pristine .ng-invalid-required {
box-shadow: none;
}
After a user has entered invalid data (and the ng-pristine class has changed to ng-dirty), the box-shadow will show again b/c this rule will no longer apply.
This has actually nothing to do with AngularJS but is a browser feature which you can control with CSS.
Take a look at this MDN-Doc: https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid and this question: Firefox 4 Required input form RED border/outline
With this CSS, Firefox seems to behave the same as Chrome:
select:invalid {
box-shadow: none;
}
http://jsfiddle.net/xLmC2/6/
#klamping: Your solution kinda works, but: As soon as you start typing in another field, your field gets the red shadow (since form.ng-pristine is not there anymore).
Better way:
.ng-untouched.ng-invalid-required { box-shadow:none; }
Drawback: The red shadow isn't shown when the user submits the form without ever clicking into this field (but the popup-tooltip is shown).
#DanEEStar: The problem only occurs if AngularJS is present. Your solution certainly works, but breaks the red highlighting completely (i.e. after form submission too).
I've created a bug report for AngularJS here:
https://github.com/angular/angular.js/issues/12102
Example:
<div ng-app>
<form>
<input type="text" ng-model="a" required>
</form>
</div>
Here's the fiddle: https://jsfiddle.net/5yh58orm/11/
Thomas Landauer's solution kinda works :), but as soon as you click/focus the field, the field shadow turn to be red again,
I suggest to use:
.ng-pristine.ng-invalid-required { box-shadow:none; }
For angular2, this is:
.ng-pristine.ng-invalid {
box-shadow: none;
}
To completly remove it type in css:
:-moz-ui-invalid:not(output) {
box-shadow: none;
}
I had the same problem when I used a select box outside of a form. The problem was fixed when I did this:
<form novalidate>
<select>
<option value=""></option>
</select>
</form>
I think then that this is a browser thing and nothing to do with angular?

Submit form when selected value of select element is changed without javascript

Hi the question is pretty simple.
<select name="justanumber">
<option value="1" selected="selected">1</option>
<option value="2"></option>
</select>
when selected value is changed i have to do a form post.
But the problem is to do this WITHOUT javascript.
I'm not sure is it possible to do that, the best result i have archived is submit form using label for.
No, that is not possible without using client script.
I suggest that you use script for how you want it to work normally, and supply a submit button as a backup for those who can't use script.
No, there is no auto-submit attribute for such things -- however, there is a way around it:
CSS:
#jsOn .Submit {
display: none;
}
HTML:
<form id="my_form" action="">
<select id="justanumber" name="justanumber">
<option value="1" selected="selected">1</option>
<option value="2"></option>
</select>
<input type="submit" value="Go!" class="Submit" />
</form>
JavaScript:
var visible_root = document.getElementsByTagName("body");
while (visible_root.length < 1) {
continue;
}
visible_root = visible_root[0];
visible_root.id = "jsOn";
document.getElementById("justanumber").onchange = function() {
document.getElementById("my_form").submit();
};
When people without JavaScript arrive at your site they will see a submit button. When people with JavaScript turned on arrive at your site the submit button will be hidden and an onchange event will be added to the select element. (Alternately you could add an event listener, if you have a JavaScript library that normalizes all of the events for you.)
I don't believe this is possible without js. An obvious approach would be to set a submit button as the option's value (eg, <option><input type="submit"></option>). Unfortunately browsers will bark and moan, appending the input element after the select element. If the select determines the app flow, consider using another another UI element (eg, buttons, links, etc.).
Here is the answer.
Unfortunately, It's not possible.
<form action="aaa.php" method="post" name="autosubmit_select">
<select name="justanumber" onChange="document.autosubmit_select.submit();">
<option value="1" selected="selected">1</option>
<option value="2"></option>
</select>
</form>
agreed you must use javascript for this
please see this post How to submit form on change of dropdown list?
if your worried about users not having javascript enabled then i would suggest along with the given example in the post in the link you do this
<noscript>This form requires that you have javascript enabled to work properly please enable javascript in your browser.</noscript>
or something along those lines
other than that it is not possible to do this (currently) without the use of javascript
use a submit_button to submit while JS not enabled.
If JS enabled hide the button using JavaScript. Amazon did so.!
http://www.amazon.in/s/ref=nb_sb_ss_i_1_5?url=search-alias%3Delectronics&field-keywords=sd+card&sprefix=sd+ca%2Celectronics%2C401&crid=1M4KMJEGDLTEL
disable JS and see the sortby section top right corner.

Refresh problem in Firefox

In my asp page there is one textbox name "ProductName"
if i write any thing in that textbox and refresh that page , textbox is not clear in firefox. But i open this same page in Internet explore and write any thing in textbox and refresh the page, my textbox comes clear
why textbox not comes clear in FireFox?
This is the html code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<input type="text" id="ProductName" name="ProductName" style="width:235; height:20" value="">
</body>
</html>
This is a feature of Firefox (one I'm quite fond of). There's nothing you can do about it on the server-side.
EDIT:
The longer version: There is something you can do about, but it's very messy. Basically, the way Firefox implements this is it refills in form elements with the same name when the user hits the refresh button.
The workaround is to change the name attribute on your HTML form elements every time the page loads. How you keep track of what you change them too is left to your discretion, but let me just say that as a Firefox user myself, having a website do this would annoy me no end.
"Actually firefox is stupid that way." - yes it is.
Another way would be to set autocomplete="off" on the input
Firefox isn't stupid that way. That's appropriate behavior because presumably the div your fading in occurs on an event e.g. onclick, onchange, etc. And since that event isn't triggered on refresh firefox has no reason to show your div. It's your responsibility to write the js necessary to trigger events onload or when document is ready.
Back to topic: a simple solution is to set the autocomplete attribute of your form to 'off'
That's it.
You can add the autocomplete="off" attribute to the input as shown below and it should fix the issue.
<input type="text" id="ProductName" name="ProductName" style="width:235; height:20" value="" autocomplete="off">
Actually firefox is stupid that way. Let's say you have a checkbox and when the user clicks to activate that checkbox there is a div that should fade in. The problem here is that if you refresh the page your checkbox will remain active but your div will be hidden. They(firefox) did not think it all the way through and that is .. well stupid. Because of that you have to resort to js workarounds which (I'm not quite fond of).
I do agree that you need to have, at some poin, the same value inside some input, like when you submit a page, there is an error and you need to redirect the user back to the page containing the form. Then yeah!, ok!, you need the values... but to keep the values on refresh , well that s just stupid.
What user enters his details and then just thinks "hm I'll just press f5 now see what happens". If that's the kind of user 'roaming round' your site, please shut it down it's infested with stupidity
Ok so now that that is out of the way.
"change the name attribute on your HTML form elements every time the page loads"
this is a bad idea don`t do this...
instead you can use a js function that simply gets the input and sets the value to ""
onpageload:
document.getElementById('inp1').value = '';
document.getElementById('inp2').value = '';
document.getElementById('inp3').value = '';
and so on and so forth.
P.S. this is just an example, it's your job to see, what fits for you (be it jquery, prot or whatever); to see if you use a for loop or not; etc
HTMLFormElement instances inherit a reset method.
It can be used to clear all forms to its default values:
for(var i=0; i<document.forms.length; ++i)
document.forms[i].reset();
for this problem add
autocomplete="off"
attr to input tag for example:
<input type="text" autocomplete="off" id="ProductName" name="ProductName" style="width:235; height:20" value="">
You can use Ctrl+Shift+R command to force reload (not from cache) (see Page Navigation Shortcuts on http://www-archive.mozilla.org/docs/end-user/moz_shortcuts.html)
Server-side solution:
PHP session_start() clears input data. Tested on firefox 103.0.2.
<?php session_start(); ?>
<input type="text" />
<br/><br/>
<select>
<option>Choice 1</option>
<option>Choice 2</option>
<option>Choice 3</option>
<option>Choice 4</option>
</select>