Default option for required select element - html

Is there a way to not count the default option as a submission in a required select element? For example if the default option is "Choose...", I don't want the user to be able to submit the form without changing this input. I want the user to have to choose one of the other options before they can submit.

The value of the default option is left empty (""), so the script can check and prevent form submission (event.preventDefault()) unless there is an option value.
Note the alert (popup) can be commented out or removed, and the form will still not submit until an option is selected. However, the user may not realize their mistake without some kind of notification.
function checkInput() {
const choice = document.getElementById('cars');
if (choice.value === '') {
event.preventDefault();
alert('Must select an option');
}
}
select {
width: 14rem;
margin-bottom: 1em;
}
<form action="#" id="form">
<select name="cars" id="cars">
<option value="">Pick a Ride</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br />
<button type="submit" onclick="checkInput()">Submit Form</button>
</form>

Related

Not able to keep the option selected in drop-down list

I have a drop-down list which POSTs to express and then redirects to same page.
Now, I want the one valid option to be selected before submitting, hence I added required property in <select> tag.
But, I also want a value to be Selected in the list when page renders
If first time render, then first option
If after submitting, the submitted option to be selected by default
Here is the skeleton code
<select required data-style="btn-info" name="selectpicker">
<optgroup label="Select Map">
<option name="table1" value="Station">Station</option>
<option name="table2" value="Station2">Station2</option>
</optgroup>
</select>
and the things I tried
<option value="" selected disabled>Select station</option>
<option name="table1" value="Station" selected>Station</option>
but none of them fulfills my requirements.
if you are using javascript for handling the form just add event.preventDefault() in your onsubmit function so when you submit it won't revert back to default
The problems were solved by
<form action="any" method="any" onSubmit="return fieldCheck();">
<select required id="collec" data-style="btn-info" name="selectpicker">
<option value="Select Station" selected disabled hidden>Select City</option>
<option name="table1" value="Vice City">Vice City</option>
<option name="table2" value="Delhi City">Delhi City</option>
and script
function fieldCheck() {
var collec = document.querySelector('#collec');
if(collec.value != "Select Station") {
return true;
}
else {
alert("Select a city first!!");
return false;
}
}

Setting default value to an HTML dropdown

I have created a dropdown menu in html. I have a reset button which resets all the value to null. But on clicking this I need to reset the dropdown menu to the first value which I gave in the field.
Please help...
The code for the dropdown is as below.
<select name="abc" id="abc">
<option value="one">One
<option value="two">Two
</select>
But on clicking Reset I am getting a blank in the dropdown list from where i have to again select either One or Two.
Instead I want it to automatically set to the value "One".
You can use add the attribute selected to the option you want to be default like this:
<select name="abc" id="abc">
<option value="one">One</option>
<option value="two" selected>Two</option>
</select>
Or add selected="selected":
<select name="abc" id="abc">
<option value="one" selected="selected">One</option>
<option value="two">Two</option>
</select>
You just need to use the selected attribute for the item that you want to be the default:
<select name="abc" id="abc">
<option value="one" selected="selected">One</option>
<option value="two">Two</option>
</select>
If you are manually performing the reset, you'll likely want to store a reference to the current selection prior to resetting them and using that to retain the previous selection, which you can access via the selectedIndex property. Depending on your use case and technology (e.g. jQuery, etc.), you can select a specific element by its index, value, or potentially other attributes.
You can add selected attributes to make default select.
<select name="abc" id="abc">
<option value="one">One
<option value="two" selected="selected">Two
</select>
With jquery
$("#abc").val("two")
use selected
here live example https://www.w3schools.com/tags/att_option_selected.asp
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
I think you need this code in your reset jquery function.
$("#abc option:selected").prop("selected", false);
$("#abc option:first").prop("selected", "selected");
HTML select drop down by default shows first option if none of the option is selected. So, effectively you just need to clear the selected attibute for your dropdown.
function clearForm(form) {
//var $f = $(form);
//var $f = $f.find(':input').not(':button, :submit, :reset, :hidden');
//$('#msg').empty();
$('#abc option:selected').removeAttr('selected');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="abc" id="abc">
<option value="one">One
<option value="two" selected>Two
</select>
<input type='button' class="btn btn-primary" value='Reset' name='reset' onclick="return clearForm(this.form);">

How to prevent user from picking default value from dropdown in css?

I have a form in which I have added "Please Select" option for the drop-down.
The form should work in a way that if users doesn't select values 1, 2-5, 6-15, 16-30 etc then the form should not submit.
In my form, if I select "Please Select" then the form gets submit as it being treated as 1, 2-5, 6-15, 16-30, etc.
The HTML codes for the form generated at run time are:
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" aria-required="true" aria-invalid="false">
<option value="Please Select" selected="selected">Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
Problem Statement:
I am wondering what changes I should make in the HTML code above so that "Please Select" option is not treated as other drop-down values (1, 2-5, 6-15, 16-30 etc ) in the HTML.
To force form validation on a select input, you need to use the required attribute on your select form and set the value of the initial option to "".
Note: aria-required="true" works fine as well, especially for browsers that don't yet support HTML5, I just prefer the shorter HTML5 alternative. This also applies to selected="selected" vs selected.
<form>
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" required aria-invalid="false">
<option value="" selected>Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
<button type="submit">Submit</button>
</form>
Edit: This can also be done via Javascript.
// Obtain our form via its ID
var form = document.querySelector('form');
// Add a listener to our form to wait for its submission
if (form.addEventListener) {
form.addEventListener("submit", validate, false); //Modern browsers
} else if (form.attachEvent) {
form.attachEvent('onsubmit', validate); //Old IE
}
function validate(e) {
var select = e.target.querySelector("select");
// Get the value of our selected option
var selectedOption = select.options[select.selectedIndex].value;
// Compare the value of the default option to the selected option
if (selectedOption === "Please Select") {
// Trigger Error and prevent the form submission
alert("Please select an option!")
e.preventDefault();
}
}
<form>
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" aria-required="true" aria-invalid="false">
<option value="Please Select" selected="selected">Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
<button type="submit">Button</button>
</form>
To simply prevent the users from selecting an option just add disabled in the html for the option (as pointed out in the question from the comment marking this as a possible duplicate, #csmckelvey).
However, you can also simply add a display:none to the option:
select option:first-of-type{
display:none;
}
At least works on android, and yet show the text of the first option (even though it Should be hidden - dont Know why).
Anyways it's important to Remember to validate the form as the other answer suggests.

Dropdown in form required

I have to make a form for a contact page.
I want that people put their names, email, phone number etc. etc. in different input's. The input's are all tagged with REQUIRED.
If I try to submit the form and I forget to fill in for example a name, it will give a notification that you didn't fill in your name.
That's good.
However, my dropdown menu is also required even if I don't select a value from the dropdown, I will not get a notification.
<div class="form-group">
<select class="selectpicker" name="dropdownmenu" id="selectpicker" required>
<option selected="selected" value="first_value" disabled>Kies je onderwerp</option>
<option value="dropdown_advertentie">Advertentie</option>
<option value="dropdown_suggestie">Suggestie</option>
<option value="dropdown_opmerking">Opmerking</option>
</select>
So the first value, 'Kies je onderwerp' is selected when you open the page. But when that is still selected and you click the submit button, I need a notification that you didn't select a value.
Your value for the first option has to be empty for a required check.
<select required>
<option value="">Kies je onderwerp</option>
<option value="dropdown_advertentie">Advertentie</option>
<option value="dropdown_suggestie">Suggestie</option>
<option value="dropdown_opmerking">Opmerking</option>
</select>
you can use a js validation as bellow
function validateForm() {
var x = document.forms["myForm"]["field_name"].value;
if (x == ""|| x=="first_value") {
alert("Name must be filled out");
return false;
}
}
If you are not ready to leave it empty, you can validate it by using below script. Check below snippet for reference.
$('#btnSubmit').click(function() {
if ($('#selectpicker :selected').text() == 'Kies je onderwerp') {
alert('please select a valid value');
}
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form><select class="selectpicker" name="dropdownmenu" id="selectpicker" required>
<option selected="selected" value="first_value" disabled>Kies je onderwerp</option>
<option value="dropdown_advertentie">Advertentie</option>
<option value="dropdown_suggestie">Suggestie</option>
<option value="dropdown_opmerking">Opmerking</option>
</select>
<input type="submit" id="btnSubmit" value="submit">
</form>

How to clear datalist input when options are opened?

I have a html5 input with associated datalist, I want to clear the input when the options are opened so that all of them could be visible (unfiltered). How could I do this in Javascript? (with jQuery or not)
For example (https://stackoverflow.com/a/29755076/2190425)
<input type="text" name="city" list="cityname">
<datalist id="cityname">
<option value="Boston">
<option value="Cambridge">
</datalist>
When I click the dropdown arrow, then select Boston and after that click the dropdown arrow again - after this second click I want the input to be empty (because it filters the options to the one option that's typed in - Boston), so I need some kind of event or something that would allow me to empty the input, but it can't be input event, because nothing is input when you click the dropdown yet.
You can go with editable dropdown which will work as datalist and your requirement will be accomplished. The code for editable drop down given below.
$(document).ready(function(){
$(".editableBox").change(function(){
$(".timeTextBox").val($(".editableBox option:selected").html());
});
});
.editableBox {
width: 75px;
height: 30px;
}
.timeTextBox {
width: 54px;
margin-left: -78px;
height: 25px;
border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<select class="editableBox">
<option value="1">01:00</option>
<option value="2">02:00</option>
<option value="3">03:00</option>
<option value="4">04:00</option>
<option value="5">05:00</option>
<option value="6">06:00</option>
<option value="7">07:00</option>
<option value="8">08:00</option>
<option value="9">09:00</option>
<option value="10">10:00</option>
<option value="11">11:00</option>
<option value="12">12:00</option>
<option value="13">13:00</option>
<option value="14">14:00</option>
<option value="15">15:00</option>
<option value="16">16:00</option>
<option value="17">17:00</option>
<option value="18">18:00</option>
<option value="19">19:00</option>
<option value="20">20:00</option>
<option value="21">21:00</option>
<option value="22">22:00</option>
<option value="23">23:00</option>
<option value="24">24:00</option>
</select>
<input class="timeTextBox" name="timebox" maxlength="5"/>
</div>
What I did was simply clean the input on double click. This way at least the user has the option and if informed - the usage is quite comfortable (for new users it could less of a solution).
Here is the code I used:
# datalist does not fire change event, therefore we need this exception, the if is for avoiding duplicate call with keyup
$(#dom.search_fields).find('input.datalist_filter').on 'input', (e) =>
#handleInput(e) if e.target.value.length and
($(e.target.list).find('option').filter -> this.value == e.target.value).length
# it does not fire event if you set the value of input manually, therefore we need to call handleInput directly here
$(#dom.search_fields).find('input.datalist_filter').on 'dblclick', (e) =>
e.target.value = ''
#handleInput(e)