前端学习笔记之——其他表单元素及输入验证

其他表单元素及输入验证

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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nW34qh8Z-1624174807380)(素材/用select元素向用户提供一系列选项.png)]

通过 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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eU5U7VuB-1624174807382)(素材/在select元素中显示和选择多个选项.png)]

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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rcG2XLii-1624174807383)(素材/使用optgroup元素.png)]

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 行:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aQzrInkc-1624174807384)(素材/使用textarea元素.png)]

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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ss5kaTJx-1624174807385)(素材/使用outpue元素.png)]

1.4、生成公开/私有秘钥

keygen 元素的用途是生成公开/私有密钥对。公钥被发给服务器,而私钥则有浏览器保留并存入用户的秘钥仓库

元素keygen
元素类型短语元素
允许具备的父元素任何可以包含短语元素的元素
局部属性challenge、keytype、autofocus、name、disabled 和 form
内容
习惯样式

keygen 属性的用途是指用来生成密钥对的算法,不过它支持的值只有 RSA 一种。challenge 属性用来指定一条与公钥一起发送给服务器的密钥口令。


2、使用输入验证

输入验证是通过表单元素的一些属性控制的。

验证元素元素
requiredtextarea、select、input(text、password、checkbox、radio、file、datetime、datetime-local、date、month、time、week、number、email、url、search 及 tel 型)
min、maxinput(datetime、datetime-local、date、month、time、week、number 及 range 型)
patterninput(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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eieJrWRx-1624174807385)(素材/提醒必须填写此字段.png)]

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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WBpeb5KX-1624174807386)(素材/输入值范围验证错误提示.png)]

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>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jetSO8Ls-1624174807386)(素材/输入值模式验证错误提示.png)]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值