Select box validation is having problem after adding a required attribute - html

I got a problem when I'm validating this. I can't understand how do I fix it?
<Label for="religion">Select Your Religion</Label>
<select required class="form-control" id="religion">
<option disabled>Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>
Here is the problem I'm getting on https://validator.w3.org/nu :
The first child option element of a select element with a required attribute, and without a multiple attribute, and without a size attribute whose value is greater than 1, must have either an empty value attribute, or must have no text content. Consider either adding a placeholder option label, or adding a size attribute with a value equal to the number of option elements.

The name attribute is missing for your select option
Also, the select option should have value=""
<Label for="religion">Select Your Religion</Label>
<select required name="religion" class="form-control" id="religion">
<option value="">Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>

Like the validator message says
...must have either an empty value attribute, or must have no text content...
Make sure your option has an empty value, like <option disabled value="">
<Label for="religion">Select Your Religion</Label>
<select required class="form-control" id="religion">
<option disabled value="">Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>

Related

How Disable an option on Datalist

how can I disable an option in Datalist?
<input type="hidden" name="Town_Group" id="frmTown_Group">
<input name="Town" id="frmTown" list="frmTown_List">
<datalist name="Town_List" id="frmTown_List">
<option value="London">London</option>
<option value="Zurich">Zurich</option>
<option value="|" disabled=true >Italian</option>
<option value="Turin">Turin</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="Naples">Naples</option>
<option value="|" disabled=true >French</option>
<option value="Bordeaux">Bordeaux</option>
<option value="Lion">Lion</option>
<option value="Paris">Paris</option>
</datalist>
I tried with disabled, the effect was that the option has disappeared.
Thanks Giovanni Rossati
In that case you can use readonly attribute like this, I wouldn't recommend this
<input type="hidden" name="Town_Group" id="frmTown_Group" />
<input name="Town" id="frmTown" list="frmTown_List" />
<datalist name="Town_List" id="frmTown_List">
<option value="London">London</option>
<option value="Zurich">Zurich</option>
<option value=" " readonly>Italian</option>
<option value="Turin">Turin</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="Naples">Naples</option>
<option value=" " readonly>French</option>
<option value="Bordeaux">Bordeaux</option>
<option value="Lion">Lion</option>
<option value="Paris">Paris</option>
</datalist>
This is the only way because
Datalist vs Select:
I guess this is what you want, but there is no comparison. Datalist
is different, Select is different.
datalist is used for autopopulating results from the list, based on
user input, while select doesn't do any magic, it just shows all
options it has.
so this makes clear that there shouldn't be any preselected value in
datalist (as it is used to auto populate, on user interaction). thus
it cant be readonly
select on other hand is different, it can have a default value, thus
it can be readonly.
And yes what you said: "If you desire readonly or disabled, then use a select tag", yes, according to me its true, coz you cant set anything as selected in datalist, but you can in select.

how to make required select statement even with disabled, selected option

<form action="/xyz.jsp">
<select required>
<option selected disabled hidden>Select one of your active items...</option>
<option value="">None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
The issue here is, when I specify require it does not act properly because the first field is selected.. it ends up producing NULL instead of actually requiring user to enter a choice...
Anything I can do to fix this?
It is because you have to make the first option value the empty string. You will have to come with another value for your "none" option.
See also Can I apply the required attribute to <select> fields in HTML5?
This is pure HTML.
<form action="/xyz.jsp">
<select required>
<option value="" selected hidden>Select one of your active items...</option>
<option value="none">None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>

Is there a way to fill select element with datalist

For input element we can use datalist element as autocomplete
<input name="account" placeholder="account" list="publisher">
<datalist id="publisher">
<option value="00000" >00000</option>
<option value="00001" >00001</option>
<option value="00002">00002</option>
</datalist>
there is a way to do so for select element ? not autocomplete just fill the select with options for example:
the two example should be equivalent
<select name=account>
<option value="00000" >00000</option>
<option value="00001" >00001</option>
<option value="00002">00002</option>
</select>
and that
<select name="account" list="publisher"></select>

Bootstrap select dropdown list placeholder

I am trying to make a dropdown list that contains a placeholder. It doesn't seem to support placeholder="stuff" as other forms do. Is there a different way to obtain a placeholder in my dropdown?
Yes just "selected disabled" in the option.
<select>
<option value="" selected disabled>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Link to fiddle
You can also view the answer at
https://stackoverflow.com/a/5859221/1225125
Use hidden:
<select>
<option hidden >Display but don't show in list</option>
<option> text 1 </option>
<option> text 2 </option>
<option> text 3 </option>
</select>
dropdown or select doesn't have a placeholder because HTML doesn't support it but it's possible to create same effect so it looks the same as other inputs placeholder
$('select').change(function() {
if ($(this).children('option:first-child').is(':selected')) {
$(this).addClass('placeholder');
} else {
$(this).removeClass('placeholder');
}
});
.placeholder{color: grey;}
select option:first-child{color: grey; display: none;}
select option{color: #555;} // bootstrap default color
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control placeholder">
<option value="">Your Placeholder Text</option>
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
</select>
if you want to see first option in list remove display property from css
Most of the options are problematic for multi-select.
Place Title attribute, and make first option as data-hidden="true"
<select class="selectpicker" title="Some placeholder text...">
<option data-hidden="true"></option>
<option>First</option>
<option>Second</option>
</select>
If you are initializing the select field through javascript, the following can be added to replace the default placeholder text
noneSelectedText: 'Insert Placeholder text'
example: if you have:
<select class='picker'></select>
in your javascript, you initialize the selectpicker like this
$('.picker').selectpicker({noneSelectedText: 'Insert Placeholder text'});
Add hidden attribute:
<select>
<option value="" selected disabled hidden>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Try this:
<select class="form-control" required>
<option value="" selected hidden>Select...</option>
when using required + value="" then user can not select it
using hidden will make it hidden from the options list, when the user open the options box
Try #title = "stuff". It worked for me.
All this options are.... improvisations.
Bootstrap already offers a solution for this.
If you want to change the placeholder for all your dropdown elements you can change the value of
noneSelectedText
in the bootstrap file.
To change individualy, you can use TITLE parameter.
example:
<div class="form-group">
<label class="control-label col-xs-2" id="country">Continent:</label>
<div class="col-xs-9">
<select name="zones[]" class="selectpicker" title="All continents" id="zones" multiple >
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="North America">America North</option>
<option value="South America">America South</option>
<option value="Antarctica">Antarctica</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
</select>
</div>
</div>
Using Bootstrap, This is what you can do. Using class "text-hide", the disabled option will be shown at first but not on the list.
<select class="form-control" >
<option selected disabled class="text-hide">Title</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
in bootstrap-select.js Find title: null, and remove it.
add title="YOUR TEXT" in <select> element.
Fine :)
Example:
<select title="Please Choose one item">
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
I think, the Dropdown box with a class and JQuery code to disable the first option for user to select, will work perfectly as Select Box placeholder.
<select class="selectboxclass">
<option value="">- Please Select -</option>
<option value="IN">India</option>
<option value="US">America</option>
</select>
Make the first option disabled by JQuery.
<script>
$('select.selectboxclass option:first').attr('disabled', true);
</script>
This will make the first option of Dropdown as Placeholder and user will no longer able to select the first option.
Hope It helps!!
The right way to achieve what you are looking for is to use the title attribute.
The title global attribute contains text representing advisory information related to the element it belongs to.
title="Choose..."
for example:
<select class="form-control selectpicker" name="example" title="Choose...">
<option value="all">ABCDEFG</option>
<option value="all">EFG</option>
</select>
check the documentation for more info about bootstrap-select
custom-button-text
Here's another way to do it
<select name="GROUPINGS[xxxxxx]" style="width: 60%;" required>
<option value="">Choose Platform</option>
<option value="iOS">iOS</option>
<option value="Android">Android</option>
<option value="Windows">Windows</option>
</select>
"Choose Platform" becomes the placeholder and the 'required' property ensures that the user has to select one of the options.
Very useful, when you don't want to user field names or Labels.
Bootstrap select has an noneSelectedText option. You can set it via data-none-selected-text attribute.
Documentation.
For .html page
<select>
<option value="" selected disabled>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
for .jsp or any other servlet page.
<select>
<option value="" selected="true" disabled="true">Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Using bootstrap, if you need to also add some values to the option to use for filters or other stuff you can simply add the class "bs-title-option" to the option that you want as a placeholder:
<select class="form-group">
<option class="bs-title-option" value="myVal">My PlaceHolder</option>
<option>A</option>
<option>B</option>
<option>c</option>
</select>
Bootstrap adds this class to the title attribute.
Solution for Angular 2
Create a label on top of the select
<label class="hidden-label" for="IsActive"
*ngIf="filterIsActive == undefined">Placeholder text</label>
<select class="form-control form-control-sm" type="text" name="filterIsActive"
[(ngModel)]="filterIsActive" id="IsActive">
<option value="true">true</option>
<option value="false">false</option>
</select>
and apply CSS to place it on top
.hidden-label {
position: absolute;
margin-top: .34rem;
margin-left: .56rem;
font-style: italic;
pointer-events: none;
}
pointer-events: none allows you to display the select when you click on the label, which is hidden when you select an option.
angular html css
<option value="" defaultValue disabled> Something </option>
you can replace defaultValue with selected but that would give warning.
This is for Bootstrap 4.0, you only need to enter selected on the first line, it acts as a placeholder. The values are not necessary, but if you want to add value 0-... that is up to you.
Much simpler than you may think:
<select class="custom-select">
<option selected>Open This</option>
<option value="">1st Choice</option>
<option value="">2nd Choice</option>
</select>
This is link will guide you for further information.

HTML5 validation in an auto generated Symfony2 form <select> field

I have the following code on a required select :
<select required="required">
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select>
In my (HTML5 / symfony2) app its a required date (day) so the value must be between 1 and 31 and I do not want blank/null values.
Even though the code works, it breaks w3c validation with the following error :
The first child option element of a select element with a required
attribute and without a multiple attribute, and whose size is 1, must
have either an empty value attribute, or must have no text content.
What is the best approach to the problem
With require attribute:
<select required="required">
<option value=""></option><!-- added empty value -->
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select>
Without – just remove require attribute:
<select>
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select>