其他表单元素及输入验证
1、使用其他表单元素
以下五个表单元素还没有介绍:select、optgroup、textarea、output 和 keygen。
1.1、生成选项列表
select 元素可以用来生成一个选项列表供用户选择。它比 radiobutton 型 input 元素更紧凑,更适合于选项较多的情况。
该元素的 name、disabled、form、autofocus 和 required 属性与 input 元素的同名属性类似。size 属性用来设定要显示给用户的选项数目。元素如果设置了 multiple 属性的话,那么用户就能一次选择多个选项。
元素 | select |
---|---|
元素类型 | 短语元素 |
允许具备的父元素 | 任何可以包含短语元素的元素 |
局部属性 | name、disabled、form、size、multiple、autofocus 和 required |
内容 | option 和 optgroup 元素 |
标签用法 | 开始标签和结束标签 |
习惯样式 | 无 |
提供给用户的选项由 option 定义。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name: <input value="Adam" id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password" placeholder="Min 6 characters"
id="password" name="password"/>
</label>
</p>
<p>
<label for="fave">
Favorite Fruit:
<select id="fave" name="fave">
<option value="apples" selected label="Apples">Apples</option>
<option value="oranges" label="Oranges">Oranges</option>
<option value="cherries" label="Cherries">Cherries</option>
<option value="pears" label="Pears">Pears</option>
</select>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
通过 size 属性可让 select 元素显示多个选项,设置 multiple 属性则可让用户一次选择多个选项。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name: <input value="Adam" id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password" placeholder="Min 6 characters"
id="password" name="password"/>
</label>
</p>
<p>
<label for="fave" style="vertical-align:top">
Favorite Fruit:
<select id="fave" name="fave" size="5" multiple>
<option value="apples" selected label="Apples">Apples</option>
<option value="oranges" label="Oranges">Oranges</option>
<option value="cherries" label="Cherries">Cherries</option>
<option value="pears" label="Pears">Pears</option>
</select>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
1.1.1、在 select 元素中建立结构
optgroup 元素可以用来在 select 元素的内容中建立一定的结构。
元素 | optgroup |
---|---|
元素类型 | 无 |
允许具备的父元素 | select 元素 |
局部属性 | label、disabled |
内容 | option 元素 |
标签用法 | 开始标签和结束标签 |
习惯样式 | 无 |
该元素的用途是对 option 元素进行编组。其 label 属性可用来为整组选项提供小标题,而 disabled 属性则可用来阻止选择组内的任何选项。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name: <input value="Adam" id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password" placeholder="Min 6 characters"
id="password" name="password"/>
</label>
</p>
<p>
<label for="fave" style="vertical-align:top">
Favorite Fruit:
<select id="fave" name="fave">
<optgroup label="Top Choices">
<option value="apples" label="Apples">Apples</option>
<option value="oranges" label="Oranges">Oranges</option>
</optgroup>
<optgroup label="Others">
<option value="cherries" label="Cherries">Cherries</option>
<option value="pears" label="Pears">Pears</option>
</optgroup>
</select>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
1.2、输入多行文字
textarea 元素生成的是多行文本框,用户可以在里边输入多行文字。
元素 | textarea |
---|---|
元素类型 | 短语元素 |
允许具备的父元素 | 任何可以包含短语元素的元素,但通常是 form 元素 |
局部属性 | name、disabled、form、readonly、maxlength、autofocus、required、placeholder、dirname、rows、wrap 和 cols |
内容 | 文本,也即该元素内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | 无 |
textarea 元素的 rows 和 cols 属性可用来设置其大小。wrap 属性有两个值:hard 和 soft,可用来控制在用户输入的文字中插入换行符的方式。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name: <input value="Adam" id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password" placeholder="Min 6 characters"
id="password" name="password"/>
</label>
</p>
<p>
<label for="fave" style="vertical-align:top">
Favorite Fruit:
<select id="fave" name="fave">
<optgroup label="Top Choices">
<option value="apples" label="Apples">Apples</option>
<option value="oranges" label="Oranges">Oranges</option>
</optgroup>
<optgroup label="Others">
<option value="cherries" label="Cherries">Cherries</option>
<option value="pears" label="Pears">Pears</option>
</optgroup>
</select>
</label>
</p>
<p>
<textarea cols="20" rows="5" wrap="hard" id="story"
name="story">Tell us why this is your favorite fruit</textarea>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
这段代码中的 textarea 的宽和高分别为 20 列和 5 行:
1.3、表示计算结果
output 元素表示计算结果。
元素 | output |
---|---|
元素类型 | 短语元素 |
允许具备的父元素 | 任何可以包含短语元素的元素,但通常是 form 元素 |
局部属性 | name、form、for |
内容 | 短语内容 |
标签用法 | 开始标签和结束标签 |
习惯样式 | output { display: inline; } |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form onsubmit="return false"
oninput="res.value = quant.valueAsNumber * price.valueAsNumber">
<fieldset>
<legend>Price Calculator</legend>
<input type="number" placeholder="Quantity" id="quant" name="quant" />x
<input type="number" placeholder="Price" id="price" name="price" />=
<output for="quant name" name="res" />
</fieldset>
</form>
</body>
</html>
1.4、生成公开/私有秘钥
keygen 元素的用途是生成公开/私有密钥对。公钥被发给服务器,而私钥则有浏览器保留并存入用户的秘钥仓库。
元素 | keygen |
---|---|
元素类型 | 短语元素 |
允许具备的父元素 | 任何可以包含短语元素的元素 |
局部属性 | challenge、keytype、autofocus、name、disabled 和 form |
内容 | 无 |
习惯样式 | 无 |
keygen 属性的用途是指用来生成密钥对的算法,不过它支持的值只有 RSA 一种。challenge 属性用来指定一条与公钥一起发送给服务器的密钥口令。
2、使用输入验证
输入验证是通过表单元素的一些属性控制的。
验证元素 | 元素 |
---|---|
required | textarea、select、input(text、password、checkbox、radio、file、datetime、datetime-local、date、month、time、week、number、email、url、search 及 tel 型) |
min、max | input(datetime、datetime-local、date、month、time、week、number 及 range 型) |
pattern | input(text、password、email、url、search 及 tel 型) |
2.1、确保用户提供了一个值
最简单的输入验证是检查用户是否提供了一个值,这正是 required 属性的用途。对某个元素设置这个属性后,除非用户已经提供了一个值,否则无法提交表单。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name: <input value="Adam" required id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password" placeholder="Min 6 characters"
id="password" name="password"/>
</label>
</p>
<p>
<label for="fave" style="vertical-align:top">
Favorite Fruit:
<select id="fave" name="fave">
<optgroup label="Top Choices">
<option value="apples" label="Apples">Apples</option>
<option value="oranges" label="Oranges">Oranges</option>
</optgroup>
<optgroup label="Others">
<option value="cherries" label="Cherries">Cherries</option>
<option value="pears" label="Pears">Pears</option>
</optgroup>
</select>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
2.2、确保输入值位于某个范围内
min 和 max 属性可以用来确保输入的数值和日期处于指定的范围内。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name:
<input type="text" id="name" name="name"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password"
placeholder="Min 6 characters" id="password" name="password"/>
</label>
</p>
<p>
<label for="price">
$ per unit in your area:
<input type="number" min="0" max="100"
value="1" id="price" name="price"/>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
2.3、确保输入值与指定模式匹配
pattern 属性可以用来确保输入值与一个正则表达式匹配。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name:
<input type="text" id="name" name="name" pattern="^.* .*$"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password"
placeholder="Min 6 characters" id="password" name="password"/>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
2.4、确保输入值是电子邮箱地址或 URL
email 和 url 型 input 元素分别可以用来确保用户输入的值是有效的电子邮箱地址和完全限定的 URL。把 pattern 属性与这类型的 input 元素结合使用可以进一步限制用户输入的值。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name:
<input type="text" id="name" name="name" pattern="^.* .*$"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password"
placeholder="Min 6 characters" id="password" name="password"/>
</label>
</p>
<p>
<label for="email">
Email: <input type="email" placeholder="user@mydomain.com" required
pattern=".*@mydomain.com$" id="email" name="email"/>
</label>
</p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
3、禁用输入验证
有时候,设计者也想让用户不经过输入内容验证就能提交表单,如用户需要在中途保存进度这种情况。
要想不经输入验证就能提交表单,可以设置 form 元素的 novalidate 属性,也可以设置用来提交表单的 button 或 input 元素的 formnovalidate 属性。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input type="hidden" name="recordID" value="1234"/>
<p>
<label for="name">
Name:
<input type="text" id="name" name="name" pattern="^.* .*$"/>
</label>
</p>
<p>
<label for="password">
Password: <input type="password"
placeholder="Min 6 characters" id="password" name="password"/>
</label>
</p>
<p>
<label for="email">
Email: <input type="email" placeholder="user@mydomain.com" required
pattern=".*@mydomain.com$" id="email" name="email"/>
</label>
</p>
<input type="submit" value="Submit"/>
<input type="submit" value="Save" formnovalidate/>
</form>
</body>
</html>