Note...
I have set up - in the <head> section on the form the following style code:-
input:focus {
background-color: yellow;
}
This makes all the input boxes go yellow when they receive focus.
input:invalid {
background-color: #CCFFCC;
}
This makes all the input boxes go light green when they have an invalid entry.
For more information on <forms action="" method="GET or POST">
Click Here
A Form should be used to allow the user to provide feedback or send and email
with some comments on.
This is an example of a form...it uses the label/input tags and text area tag (see code)
It also sets the 'Email Address' to be a REQUIRED field
Or it would have if I had a different page that made sense, so for now
it will just send me back to this page again (index.html)
It knows where to send me because of the
<form action="index.html" method="post">
NOTE... I also saw code...
<form id="form" >
<input type="text" id="input" value="" >
<button id="submit">SUBMIT</button>
</form>
which was a very simple form but because it had <form></form> around
the text box & button then when the cursor was in the text box & you pressed ENTER it
automatically ran the onclick for the button.
It's not really recommended, you should think about
doing this
instead (using 'keyup').
It's rather basic as you cannot really use just HTML to send a properly formatted email.
A Form's information can be checked before it is actually 'submitted' by
using the following example:-
In the <script> area, create a function, eg...
function checkForm(form) {
if (form.userName.value == "") {
alert("Error username must be entered");
form.userName.focus();
return false;
}
reg = /^\w+$/;
if (!re.test(form.userName.value)) {
alert ("Error, Invalid chr's in username");
form.userName.focus();
return false;
}
return true;
}
And in the body of the page, where you have your <form> you should add the 'onsubmit()' method
eg
<form onsubmit="return checkForm(this);">
This means my element with the name="userName" in it will get checked for an error before the form
gets submitted.
NOTE...
I have used an example of this above (second section), just leave the UserName blank before submitting
the form.
Attributes : For and id re input/label elements
Labels on forms must have the for="id" set so when the form is submitted it
knows which label goes with which element.
eg...
<input type="radio" id="age1" name="age" value="30">
<label for="age1">0 - 30</label>
<input type="radio" id="age2" name="age" value="60">
<label for="age2">31 - 60</label>
<input type="radio" id="age3" name="age" value="100">
<label for="age3">61 - 100</label>
(Also...using the for attribute on the label means you can click on the
label and the relevant input box will get focus)
Grouping of Radio buttons (must have the same name="")
eg...<input type="radio" name="ages" value="30">
This is an example of a type="range"
050
25
There are numerous types of 'input boxes' that can be used :-
button
checkbox
color
date
datetime-local
email
file
hidden
image
month
number
password
radio
range
reset
search
submit
tel
text
time
url
week
It's best to look them up in w3shools web site
HERE for an explanation of the
<input> tag - and a list of applicable attributes (eg
pattern,
list, max, maxlength, minlength, multiple, required, checked, size
etc)
or for the list of different types of input boxes
So far putting the TEXT in one div
and the INPUT BOXES in another seem to work well
(Another option & may be better in the long run is using a TABLE instead but, for now,
I have used div boxes)...
I created a DIV with two other DIV's inside (one for labels and one
for the INPUT Boxes).
Then I could RIGHT-ALIGN the labels (also adjust the line-spacing to make it align
with a text box) & LEFT-ALIGN the boxes.
combined with the :focus I can set
the background colour whenever I am about to enter text in it.
input:focus {background-color: yellow;}
Eg
Click on each input box below to see it in action
The dropdown box is NOT an <input> box but a <select> box with <option>'s.
Eg...
<label for="lunch">What's for lunch?</label>
<select id="lunch" name="lunch">
<option value="pizza">Pizza</option>
<option value="pasta">Pasta</option>
<option value="curry" selected>Curry</option>
<option value="salad">Salad</option>
<option value="ramen">Ramen</option>
<option value="tacos">Tacos</option>
</select>
The <datalist> is used with an <input type="text"> box.
The <input> creates a text field that users can type into and
filter options from the <datalist>.
It is similar to the Dropdown box on the left.
Eg...
<label for="lunch">What's for lunch?</label>
<input list="lunchList" type="text" id="lunch" name="lunch">
<datalist list="lunchList">
<option value="Pizza"></option>
<option value="Pasta"></option>
<option value="Curry" ></option>
<option value="Salad"></option>
<option value="Ramen"></option>
<option value="Tacos"></option>
</select>
Try typing in what you want, eg p, and the list will change - leaving just Pizza & Pasta
then you can type more letters or use the Up/Down arrows to make your selection.
NOTE
You can also type in a letter that is ANYWHERE in the name and it will change the list and display
just the selections that contain that combination of letters, eg a.
ALSO NOTE
You can NOT change the way the list of options is displayed in a datalist.
The line spacing and grey color is what you get.