Add 'required' attribute to <select> element - html

I have a <select> dropdown in my HTML form. How do I add required attribute to my <select> element? I would like a pure HTML5 solution. I googled but I dont seem to find it.
Here is my fiddle.

The first <option> needs to be empty. Otherwise, required will see it as a value filled.
Example
<select required>
<option></option>
<option>1</option>
<option>2</option>
</select>

First of all you must insert empty field in movie list.and add required
HTML
<form id="book_tickets">
<fieldset>
<legend>Booking Details</legend>
<p>
<label for="movie_name">Choose Movie</label>
<select id="movie_name" name="movie_name" required>
<option></option>
<option value="Movie 1">Movie 1</option>
<option value="Movie 2">Movie 2</option>
<option value="Movie 3">Movie 3</option>
</select>
</p>
<p>
<label for="theaters">Theaters</label>
<input list="theaters" name="theaters" required>
<datalist id="theaters">
<option value="Inox" />
<option value="E Square" />
</datalist>
</input>
</p>
<p>
<label for="date">Select Date</label>
<input type="date" name="date" id="date" required />
</p>
<p>
<label for="email">Enter Email</label>
<input type="email" name="email" id="email" placeholder="abc#gmail.com" required />
</p>
<p>
<label for="tickets_quantity">Number of Tickets</label>
<input type="number" min="1" max="10" name="tickets_quantity" id="tickets_quantity" required />
</p>
<p>
<label>Total Price</label>
<span id="total_price"></span>
</p>
<p>
<input type="submit" id="book_tickets" value="Book Tickets" />
</p>
</fieldset>
</form>

The required attribute on select element: video demo
select element HTML5 here
referring to the first option, check out the placeholder label option demo

Related

My divs with class aren't working to style my doc. But input and labels are. What am i missing?

HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> FreeCodeCamp Survey</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1 id="title"> FreeCodeCamp Survey</h1>
<p id="description"> Thanks for taking the time to help us improve!</p>
<form id="survey-form">
<div class="details">
<label for="name" id="name-label"> Name <input id="name" type="text" required placeholder="Enter Your Name">
</label>
<label for="email" id="email-label"> Email <input id="email" type="email" required placeholder="Enter Your Email">
</label>
<label for="number" id="number-label"> Number <input id="number" type="number" required min="11" max="20" placeholder="Enter Your Number">
</label>
<label for="current-role" id="current-role-label">Which option best describes your current role?<br>
<select id="dropdown" name="dropdown">
<option value="current-role">(Select Current Role)</option>
<option value="student">Student</option>
<option value="full-time-job">Full Time Job</option>
<option value="full-time-learner">Full Time Learner</option>
<option value="prefer-not-to-say">Prefer Not to Say</option>
<option value="other">Other</option>
</select>
</label>
</div>
<div class="recommend">
<label>Would you Recommend FCC.com to a friend?</label>
<label for="definitely"><input id="definitely" name="definitely" value="definitely" type="radio"> Definitely</label>
<label for="maybe"><input id="maybe" type="radio" value="maybe" name="definitely"> Maybe</label>
<label><input id="not-sure" type="radio" value="not-sure" name="definitely"> Not Sure</label>
</div>
<div class="features">
<label for="favorite-feature" id="favorite-feature-label">What is your favorite feature of freeCodeCamp? (select an option/s)</label>
<select id="dropdown" name="dropdown">
<option value="favorite-feature">(Select Favorite Feature)
</option>
<option value="challenges">Challenges</options>
<option value="projects">Projects</options>
<option value="community">Community</options>
<option value="open-source">Open Source</option>
</select>
</div>
<div class="improvements">
<label>What would you like to see improved? (check all that apply)</label>
<label for="front-end"><input type="checkbox" value="front-end" name="front-end" id="front-end">Front-end Projects</label>
<label for="back-end"><input type="checkbox" value="back-end" name="back-end" id="back-end">Back-end Projects</label>
<label><input type="checkbox" value="challenges" name="challenges" id="challenges">Challenges</label>
</div>
<div class="comment">
<label for="comments">Any Comments or Suggestions?</label>
<textarea id="comments" name="comments" placeholder="Enter your comments here"></textarea>
</div>
<div>
<button type="submit" id="submit"> Submit</button>
</div>
</form>
</body>
</html>
I have tried styling using .features for example which is a class that I gave to my div but it doesn't seem to do anything. However if I use
label, input{ display:block;}
It seems to do the trick! I'm not sure if this makes sense. Anyhow, any help or guidance would be much appreciated. I'm having so much fun learning all this new stuff!
your class .features class is working. The option tags closing was wrong. Had an extra s in the end mentioned by #Nasser in the comment. Run the code below and you will get border which I have given through features class.
I think you should check stylesheets name 'styles.css' whether it's same with the css file name.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> FreeCodeCamp Survey</title>
<style>
label, input{ display:block;}
.features{
border: 2px solid #fcba03;
}
</style>
</head>
<body>
<h1 id="title"> FreeCodeCamp Survey</h1>
<p id="description"> Thanks for taking the time to help us improve!</p>
<form id="survey-form">
<div class="details">
<label for="name" id="name-label"> Name <input id="name" type="text" required placeholder="Enter Your Name">
</label>
<label for="email" id="email-label"> Email <input id="email" type="email" required placeholder="Enter Your Email">
</label>
<label for="number" id="number-label"> Number <input id="number" type="number" required min="11" max="20" placeholder="Enter Your Number">
</label>
<label for="current-role" id="current-role-label">Which option best describes your current role?<br>
<select id="dropdown" name="dropdown">
<option value="current-role">(Select Current Role)</option>
<option value="student">Student</option>
<option value="full-time-job">Full Time Job</option>
<option value="full-time-learner">Full Time Learner</option>
<option value="prefer-not-to-say">Prefer Not to Say</option>
<option value="other">Other</option>
</select>
</label>
</div>
<div class="recommend">
<label>Would you Recommend FCC.com to a friend?</label>
<label for="definitely"><input id="definitely" name="definitely" value="definitely" type="radio"> Definitely</label>
<label for="maybe"><input id="maybe" type="radio" value="maybe" name="definitely"> Maybe</label>
<label><input id="not-sure" type="radio" value="not-sure" name="definitely"> Not Sure</label>
</div>
<div class="features">
<label for="favorite-feature" id="favorite-feature-label">What is your favorite feature of freeCodeCamp? (select an option/s)</label>
<select id="dropdown" name="dropdown">
<option value="favorite-feature">(Select Favorite Feature)
</option>
<option value="challenges">Challenges</option>
<option value="projects">Projects</option>
<option value="community">Community</option>
<option value="open-source">Open Source</option>
</select>
</div>
<div class="improvements">
<label>What would you like to see improved? (check all that apply)</label>
<label for="front-end"><input type="checkbox" value="front-end" name="front-end" id="front-end">Front-end Projects</label>
<label for="back-end"><input type="checkbox" value="back-end" name="back-end" id="back-end">Back-end Projects</label>
<label><input type="checkbox" value="challenges" name="challenges" id="challenges">Challenges</label>
</div>
<div class="comment">
<label for="comments">Any Comments or Suggestions?</label>
<textarea id="comments" name="comments" placeholder="Enter your comments here"></textarea>
</div>
<div>
<button type="submit" id="submit"> Submit</button>
</div>
</form>
</body>
</html>

label for radio won't make a new line

I'm quite new to the HTML. I have to make a simple booking form. I'm getting stuck at the 'radio checking' label. After the "optgroup label" I need the radio option on a new line with a simple yes or no option after a text. Whatever I try it won't work and the yes or no option are right after the optgroup options.
<label for="Aantal personen">Aantal personen</label>
<input id="Aantal personen" name="Aantal personen" type="number" min="1" max="10"><br>
<label for="Aankomstdatum">Aankomstdatum</label>
<input id="Aankomstdatum" name="Aankomstdatum" type="date"><br>
<label for="Verblijfsduur">Verblijfsduur</label>
<input type="range" name="weight" id="range_weight" step="2" min="2" max="20" oninput="range_weight_disp.value = range_weight.value">
<output id="range_weight_disp"></output><br>
<label for="Verzorging">Verzorging</label>
<select id="Verzorging" name="Verzorging">
<optgroup label="opties">
<option value="logies">Logies</option>
<option value="Logies met ontbijt">Logies met ontbijt</option>
<option value="Halfpension">Halfpension</option>
<option value="Volpension">Volpensension</option>
</optgroup>
<form id="radio">"Annuleringsverzekering"</label>
<label for="annuleringsverzekering">
<label for="opt1"><input id="opt1" type="radio" name="mw" value="ja" required>Ja</label>
<label for="opt2"><input id="opt2" type="radio" name="mw" value="nee" required>Nee</label>
</form>
I hope someone can explain what I'm doing wrong, thank you:)
Welcome to Stack Overflow, there are some problems with your code:
You're not closing the <select> tag.
You are closing the annuleringsverzekering label before you even open it.
Here's how you can fix it. Make sure that you also assign the label for annuleringsverzekering to an id.
<label for="Aantal personen">Aantal personen</label>
<input id="Aantal personen" name="Aantal personen" type="number" min="1" max="10"><br>
<label for="Aankomstdatum">Aankomstdatum</label>
<input id="Aankomstdatum" name="Aankomstdatum" type="date"><br>
<label for="Verblijfsduur">Verblijfsduur</label>
<input type="range" name="weight" id="range_weight" step="2" min="2" max="20" oninput="range_weight_disp.value = range_weight.value">
<output id="range_weight_disp"></output><br>
<label for="Verzorging">Verzorging</label>
<select id="Verzorging" name="Verzorging">
<optgroup label="opties">
<option value="logies">Logies</option>
<option value="Logies met ontbijt">Logies met ontbijt</option>
<option value="Halfpension">Halfpension</option>
<option value="Volpension">Volpensension</option>
</optgroup>
</select>
<form id="radio"><label for="annuleringsverzekering">Annuleringsverzekering</label>
<label for="opt1"><input id="opt1" type="radio" name="mw" value="ja" required>Ja</label>
<label for="opt2"><input id="opt2" type="radio" name="mw" value="nee" required>Nee</label>
</form>
you can add this CSS code to your form tag with radio Id:
display: block;
you can use this code to every element to have full line for it

Not all of the input fields are aligned right

I have a form and I want to align all input fields and the select field to right line the 1st 5-6 elements. I wrote the following .css for the labels:
label {
display: inline-block;
width: 200px;
text-align: left;
}
but why only the first elemeents are right aligned ?
I wantto divide the labels and options to two columns
The below is the screenshot of my form.
HTML code:
<span class="screen-reader-text">Gallery Posts Widget</span>
</a>
</div>
<div class="widget-title"><h3>Gallery Posts Widget<span class="in-widget-title"></span></h3></div>
</div>
<div class="widget-inside">
<form method="post"> <div class="widget-content"> <p>
<label for="widget-gallery-posts-widget-__i__-title">Title:</label>
<input class="widefat" id="widget-gallery-posts-widget-__i__-title" name="widget-gallery-posts-widget[__i__][title]" type="text" value="" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-post_count">Posts to display:</label>
<input class="small-text" id="widget-gallery-posts-widget-__i__-post_count" name="widget-gallery-posts-widget[__i__][post_count]" type="number" value="4" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-post_offset">Posts offset:</label>
<input class="small-text" id="widget-gallery-posts-widget-__i__-post_offset" name="widget-gallery-posts-widget[__i__][post_offset]" type="number" value="0" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-column_qty">Posts per row:</label>
<input class="small-text" id="widget-gallery-posts-widget-__i__-column_qty" name="widget-gallery-posts-widget[__i__][column_qty]" type="number" value="1" />
</p>
<p>
Cell size (px)<br /></small>
<small><label for="widget-gallery-posts-widget-__i__-cell_width">Width:</label></small>
<input class="small-text" id="widget-gallery-posts-widget-__i__-cell_width" name="widget-gallery-posts-widget[__i__][cell_width]" type="number" value="200" />
<small><label for="widget-gallery-posts-widget-__i__-cell_height">Height:</label></small>
<input class="small-text" id="widget-gallery-posts-widget-__i__-cell_height" name="widget-gallery-posts-widget[__i__][cell_height]" type="number" value="200" />
</p>
<p>
Gap between<br /></small>
<small><label for="widget-gallery-posts-widget-__i__-cell_gap_horizontal">Columns:</label></small>
<input class="small-text" id="widget-gallery-posts-widget-__i__-cell_gap_horizontal" name="widget-gallery-posts-widget[__i__][cell_gap_horizontal]" type="text" value="0px" />
<small><label for="widget-gallery-posts-widget-__i__-cell_gap_vertical">Rows:</label></small>
<input class="small-text" id="widget-gallery-posts-widget-__i__-cell_gap_vertical" name="widget-gallery-posts-widget[__i__][cell_gap_vertical]" type="text" value="0px" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-widget_taxonomy">Taxonomy:</label>
<select id="widget-gallery-posts-widget-__i__-widget_taxonomy" name="widget-gallery-posts-widget[__i__][widget_taxonomy]">
<option value="">All</option>
<option value="category" >Categories (category)</option>
<option value="post_tag" >Tags (post_tag)</option>
<option value="post_format" >Format (post_format)</option>
</select>
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-taxonomy_term_id">Taxonomy Term IDs:</label>
<input id="widget-gallery-posts-widget-__i__-taxonomy_term_id" name="widget-gallery-posts-widget[__i__][taxonomy_term_id]" type="number" value="" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-custom_post_type">Custom Post Type:</label>
<select id="widget-gallery-posts-widget-__i__-custom_post_type" name="widget-gallery-posts-widget[__i__][custom_post_type]">
<option value="">All</option>
<option value="post" >Posts (post)</option>
<option value="page" >Pages (page)</option>
<option value="attachment" >Media (attachment)</option>
</select>
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-order_field">Order:</label>
<select id="widget-gallery-posts-widget-__i__-order_field" name="widget-gallery-posts-widget[__i__][order_field]">
<option value="date" >Date</option>
<option value="title" >Title</option>
<option value="modified" >Modified Date</option>
<option value="ID" >ID</option>
<option value="author" >Author</option>
<option value="comment_count" >Comment Count</option>
<option value="rand" >Random</option>
</select>
<select id="widget-gallery-posts-widget-__i__-order_type" name="widget-gallery-posts-widget[__i__][order_type]">
<option value="DESC" >Desc</option>
<option value="ASC" >Asc</option>
</select>
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-post_layout">Layout:</label>
<br />
<textarea class="widefat" style="resize:none;height:85px" id="widget-gallery-posts-widget-__i__-post_layout" name="widget-gallery-posts-widget[__i__][post_layout]"></textarea>
<br />
<small>
Available tags: <br />
[TITLE] [DATE] [AUTHOR] [EXCERPT] [COMMENT-COUNT] [TAGS] [WOO-PRODUCT-PRICE]
</small>
</p>
<label for="widget-gallery-posts-widget-__i__-widget_css_class">CSS classes:</label>
<input id="widget-gallery-posts-widget-__i__-widget_css_class" name="widget-gallery-posts-widget[__i__][widget_css_class]" type="text" value="" />
<p>
<label for="widget-gallery-posts-widget-__i__-post_date_format">Date Format:</label>
<input size="12" id="widget-gallery-posts-widget-__i__-post_date_format" name="widget-gallery-posts-widget[__i__][post_date_format]" type="text" value="F j, Y g:i a" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-post_excerpt_size">Excerpt size (words):</label>
<input size="3" id="widget-gallery-posts-widget-__i__-post_excerpt_size" name="widget-gallery-posts-widget[__i__][post_excerpt_size]" type="number" value="10" />
</p>
<p>
<label for="widget-gallery-posts-widget-__i__-post_excerpt_link">'Read More' Text:</label>
<input id="widget-gallery-posts-widget-__i__-post_excerpt_link" name="widget-gallery-posts-widget[__i__][post_excerpt_link]" type="text" value="more>>" />
</p>
<p>
You didn't add style to the inputs. You only styled the labels. Even those that seem to be right-aligned are not. They just fit there because of the label width and their width.

html required attribute is not working with readonly

<form action="check_rooms1.php" method="post" name="search_room" class="form" >
<p>
<label for="checkin">Check in date</label>
<input name="checkin" type="text" id="checkin" placeholder="click to enter" readonly required>
</p>
<p>
<label for="checkout">Check out date</label>
<input name="checkout" type="text" id="checkout" placeholder="click to enter" readonly required>
</p>
<p>
<label for="beds">Hotel type</label>
<label for="hotel_type"></label>
<select name="hotel_type" id="hotel_type">
<option value="1" selected>1 star</option>
<option value="2">2 star</option>
<option value="3">3 star</option>
<option value="4">4 star</option>
<option value="5">5 star</option>
</select>
<input type="submit" name="search_button" id="search_button" value="Search">
</p>
</form>
required validation is not working. I am entering the dates using a jquery calendar. that is why I disabled text edit by giving read only. Is there any problem with readonly?
Thanks for the concern.The problem is solved by using the input type date . It gives a calendar to pick date and cannot misformat the date also

htm form to disappear after submission

I am a bit new to coding. I have made a HTML form which is posting to one website after submission and is also landing on the other page for acknowledgement. but my form page is not disappearing. i am using google sites for this form so cant use any scripting language. Have to rely on html only
<form action="https://www.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8" method="POST">
<!-- ---------------------------------------------------------------------- -->
<!-- NOTE: These fields are optional debugging elements. Please uncomment -->
<!-- these lines if you wish to test in debug mode. -->
<!-- <input type="hidden" name="debug" value=1> -->
<!-- <input type="hidden" name="debugEmail" value="smoiz88#gmail.com"> -->
<!-- ---------------------------------------------------------------------- -->
<label for="first_name">First Name</label>
<input id="first_name" maxlength="40" name="first_name" size="40" type="text" required />
<br>
<label for="last_name">Last Name</label>
<input id="last_name" maxlength="40" name="last_name" size="40" type="text" required />
<br>
<label for="email">Email</label>
<input id="email" maxlength="80" name="email" size="40" type="text" required />
<br>
<label for="phone">Phone</label>
<input id="phone" maxlength="40" name="phone" size="40" type="number" />
<br>
<label class="label">Site</label>
<select class="select" id="Site" name="site" title="Site">
<option value="None">None</option>
<option value="Armadale">Armadale</option>
<option value="Elsternwick">Elsternwick</option>
<option value="Hawthorn">Hawthorn</option>
<option value="Manila">Manila</option>
<option value="UK">UK</option>
<option value="Other">Other</option>
</select>
<br>
<br>
<label class="label">Salesforce Profile</label>
<select class="select" id="Profile">
<option value="None">None</option>
<option value="Acquire Career Champion">Acquire Career Champion</option>
<option value="Acquire Career Advisor">Acquire Career Advisor</option>
<option value="Acquire Career Hunter">Acquire Career Hunter</option>
<option value="Acquire Data Analyst">Acquire Data Analyst</option>
<option value="Acquire Retention Specialist">Acquire Retention Specialist</option>
<option value="Acquire Training Admin">Acquire Training Admin</option>
<option value="Acquire Qualifier">Acquire Qualifier</option>
</select>
<br>
<br>
<label for="fault">Fault Title</label>
<input id="fault" maxlength="20" name="fault" size="30" type="text" required />
<br>
<br>
<label>Fault Type</label>
<select class="select" id="Type">
<option value="None">None</option>
<option value="Password Reset">Password Reset</option>
<option value="Report Required">Report Required</option>
<option value="Login Details">Login Details</option>
<option value="End Of Employment">End Of Employment</option>
<option value="File Merge">File Merge</option>
<option value="Insufficient Privilege">Insufficient Privilege</option>
<option value="Other">Other</option>
</select>
<br>
<br>
<label for="description">Description</label>
<textarea name="description"></textarea>
<br>
<div id="submit">
<input type="submit" name="submit">
</div>
</form>
any suggestions how can i make it disapper once it is submitted.
It seems the server brings you back because it doesn't know where to redirect after form submission.
Try create another page with confirmation text and configure server to redirect there after submission.
There must be a parameter or something on the server side to control submission behavior.