如何调用html5表单验证提示

html-css07

如何调用html5表单验证提示,第1张

HTML5加强了表单验证功能,可验证是否可空及输入内容的类型及格式,并可通过为表单或控件设置 novalidate 属性指定在提交表单时不验证整个 form 或指定的input。

例:

<form action="demo_form.asp" method="get" novalidate="false">

<input type="text" name="user_name" required novalidate="true"/>

<input type="number" name="user_age" />

<input type="submit" />

</form>

END

INPUT验证

INPUT 标签中通过 type属性指定输入内容类型:

email,指定输入内容为电子邮件地址。

url,指定输入内容为URL。

number,指定输入内容为数字,并可通过 min、max、step 属性指定最大最小及间隔。

date、month、week、time、datetime、datetime-local 指定输入内容为相应日期相关类型。

color,指定控件为颜色选择器。

例:<input id="u_email" name="u_email" type="email"/>

END

其它验证

1

required 属性指定输入内容不可为空。

pattern 属性指定输入内容必须符合指定模式(正则表达式)。

例:

<input id="phone_num" name="phone_num" type="text" pattern="\d{3}-\d{4}-\d{4}" placeholder="xxx-xxxx-xxxx"/>

HTML5验证

1

HTML5加强了表单验证功能,可验证是否可空及输入内容的类型及格式,并可通过为表单或控件设置

novalidate

属性指定在提交表单时不验证整个

form

或指定的input。

例:

<form

action="demo_form.asp"

method="get"

novalidate="false">

<input

type="text"

name="user_name"

required

novalidate="true"/>

<input

type="number"...

HTML5验证

1

HTML5加强了表单验证功能,可验证是否可空及输入内容的类型及格式,并可通过为表单或控件设置

novalidate

属性指定在提交表单时不验证整个

form

或指定的input。

例:

<form

action="demo_form.asp"

method="get"

novalidate="false">

<input

type="text"

name="user_name"

required

novalidate="true"/>

<input

type="number"

name="user_age"

/>

<input

type="submit"

/>

</form>

END

INPUT验证

1

INPUT

标签中通过

type属性指定输入内容类型:

email,指定输入内容为电子邮件地址。

url,指定输入内容为URL。

number,指定输入内容为数字,并可通过

min、max、step

属性指定最大最小及间隔。

date、month、week、time、datetime、datetime-local

指定输入内容为相应日期相关类型。

color,指定控件为颜色选择器。

例:<input

id="u_email"

name="u_email"

type="email"/>

END

其它验证

required

属性指定输入内容不可为空。

pattern

属性指定输入内容必须符合指定模式(正则表达式)。

例:

<input

id="phone_num"

name="phone_num"

type="text"

pattern="\d{3}-\d{4}-\d{4}"

placeholder="xxx-xxxx-xxxx"/>

END

JS验证

1

主流浏览器都已实现或实现了大部分HTML的验证功能,可各浏览器验证行为并不完全一致,为统一其验证行为,可按旧办法自定义JS方法统一浏览器的验证行为。

END

全部

html5表单验证用placeholder显示错误提示:

1、html5包含校验的placeholder如下:

<form action="#" method="post">

<div>

<label for="name">Text Input:</label>

<input type="text" placeholder="Your name" name="name" id="name" value="" tabindex="1" />

</div>

<div>

<label for="name_2">Text Input 2:</label>

<input type="text" placeholder="Your name" name="name_2" id="name_2" value="" tabindex="1" />

</div>

<div>

<label for="textarea">Textarea:</label>

<textarea placeholder="Some text" cols="40" rows="8" name="textarea" id="textarea"></textarea>

</div>

<div>

<label for="textarea">Textarea 2:</label>

<textarea placeholder="Some text" cols="40" rows="8" name="textarea_2" id="textarea_2"></textarea>

</div>

<div>

<input type="submit" value="Submit" />

</div>

</form>

2、css样式:

::-webkit-input-placeholder {

color:    #999

}

:-moz-placeholder {

color:    #999

}

::-moz-placeholder {

color:    #999

}

:-ms-input-placeholder {

color:    #999

}

/* Different color for some fields */

#name_2::-webkit-input-placeholder,

#textarea_2::-webkit-input-placeholder

{

color:    #FF0000

}

#name_2:-moz-placeholder,

#textarea_2:-moz-placeholder

{

color:    #FF0000

}

#name_2::-moz-placeholder,

#textarea_2::-moz-placeholder

{

color:    #FF0000

}

#name_2:-ms-input-placeholder,

#textarea_2:-ms-input-placeholder

{

color:    #FF0000

}

3、运行效果:

提交出错就是红色的