Dropdown in form required - html

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>

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;
}
}

How can I set the default value for a html <select> when the default value comes from database (it`s a page to edit an object) in blazor?

Here I want to get a project from database to edit or change its properties for ex. destination country.
when the project is loaded the select element should be already selected same as the project.DestinationCountry. Then I may change this property if needed.
<select type="text" class="form-control" id="Destination" value="#project.DestinationCountry">
<option value="Canada">Canada</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
</select>
I tried to use value="#project.DestinationCountry" within select element but it did not work.
Here is c# code
private ProjectModel project;
protected override void OnInitialized()
{
project = _db.GetProject<ProjectModel>(id);
}
This won't work since you can't add a value to the select element.
<select type="text" class="form-control" id="Destination" value="#project.DestinationCountry">
<option value="Canada">Canada</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
</select>
However, to make an option selected by default you need to use the selected attribute on option. To make sure the select element shows the fetched value from database, the best bet would be to use conditionals. If project.DestinationCountry matches the value add the selected attribute.
<select type="text" class="form-control" id="Destination">
<option value="Canada" selected>Canada</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
</select>
You can achieve this using conditional attributes
<select type="text" class="form-control" id="Destination">
<option value="Canada" selected="#(project.DestinationCountry == "Canada")">Canada</option>
<option value="Sweden" selected="#(project.DestinationCountry == "Sweden")">Sweden</option>
<option value="Switzerland" selected="#(project.DestinationCountry == "Switzerland")">Switzerland</option>
</select>
This checks whether the condition matches. If true then the value assigned is same as the attribute name thus resulting in selected="selected". When the value is false the attribute won't render at all. Or better use a for loop to output the options and for each entry you can check whether it's the current value and then add selected attribute to it to it.
I did like this and it worked but I want to know if there is smarter solution for that.
<select type="text" class="form-control" id="Destination">
<option value="">Select a country</option>
#foreach (var country in Countries)
{
if (project.DestinationCountry != null)
{
<option value="#country" selected=#(project.DestinationCountry.ToLower() ==
country.ToString().ToLower())>#country</option>
}
else
{
<option value="#country">#country</option>
}
}
</select>

Required attribute for a dropdown does not work

I have a html form which has a dropdown consisting of a few values. If the user does not select an option and moves to the next field, I need to give an error message. I have used the required attribute but it does not fire in Chrome and Firefox.
This is my code :
<select name="gender" id="gender" style="max-width:100%" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
The required attribute does not work on Chrome and Firefox. A JavaScript solution would also be good. At the time of submitting the data I am checking for empty fields but I would like to display an error message if the user does not select a value from the dropdown and moves to the next field.
Use below code, not need any script, use form tag
<!DOCTYPE html>
<html>
<body>
<form>
<select required>
<option value="">None</option>
<option value="demo">demo</option>
<option value="demo1">demo1</option>
<option value="demo2">demo2</option>
<option value="demo3">demo3</option>
</select>
<input type="submit">
</form>
</body>
</html>
Try using the onfocusout option on your select paired with some Javascript.
HTML
<select name="gender" id="gender" onfocusout="check()" style="max-width:100%" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
JS
function check(){
var x = document.getElementById("gender").selectedOptions[0].label;
if(x == "Select Gender"){
alert("Please select an option.");
}
}
CodePen.io: https://codepen.io/anon/pen/XQxQgw
There's undoubtedly a more efficient way of doing this and you would need to tweak the JS to check if all fields have been completed etc. but that's my two cents in a pinch!

Using HTML5 form validation, can I make an input field required only if a specific <option> from a <select> menu is selected?

Here's a simple form I have:
<form name="form_1">
<fieldset>
<select required name="choice">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
<option value="Other">Other</option>
</select>
<label for="other_text">If other, please specify: </label>
<input id="other_text" name="other_text" size="30">
</fieldset>
</form>
How do I make the "other_text" input field required if, and only if, the "choice" selection is set to value "Other"? By default, the input field should not be required. Could this be done with only HTML5, or is Javascript required?
Since this use case is dependent upon user interaction and selection of an appropriate menu item, it requires Javascript, and can't be achieved independently with HTML5. You may use following snippet for the requested scenario
<form name="form_1">
<fieldset>
<select required name="choice">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
<option value="Other">Other</option>
</select>
<label for="other_text">If other, please specify: </label>
<input id="other_text" name="other_text" size="30">
</fieldset>
</form>
<script type="text/javascript">
var form = document.getElementsByTagName('form')[0];
form.onsubmit = function(e) {
if (document.getElementsByTagName('select')[0].value == "Other") {
e.preventDefault();
// Block submitting form if option = Other selected
document.getElementById('other_text').setAttribute('required', true);
return false;
}
// Allow form submit otherwise
document.getElementById('other_text').removeAttribute('required');
console.log("Submitting form", e);
return true;
}
</script>
I believe that you must use a scripting language like javascript for this. Give your select field an ID and call your javascript funciton
<form name="form_1">
<fieldset>
<select required name="choice" id="choice" onchange="changeAttribute()">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
<option value="Other">Other</option>
</select>
<label for="other_text">If other, please specify: </label>
<input id="other_text" name="other_text" size="30">
</fieldset>
</form>
Then use javascript to manipulate the other field
function changeAttribute(){
if(document.getElementById('choice').value == "Other") {
document.getElementById('other_text').setAttribute("required","");
}else{
document.getElementById('other_text').removeAttribute("required","");
}
}

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.