html5表单提示的简单介绍

如何67实现HTML5表单提示弹窗功能

.tooltip, .arrow:after {

为常山等地区用户提供了全套网页设计制作服务,及常山网站建设行业解决方案。主营业务为成都做网站、成都网站制作、成都外贸网站建设、常山网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

background: black;

border: 2px solid white;

}

.tooltip {

pointer-events: none;

opacity: 0;

display: inline-block;

position: absolute;

padding: 10px 20px;

color: white;

border-radius: 20px;

margin-top: 20px;

text-align: center;

font: bold 14px"Helvetica Neue", Sans-Serif;

font-stretch: condensed;

text-decoration: none;

text-transform: uppercase;

box-shadow: 0 0 7px black;

}

.arrow {

width: 70px;

height: 16px;

overflow: hidden;

position: absolute;

left: 50%;

margin-left: -35px;

bottom: -16px;

}

.arrow:after {

content:"";

position: absolute;

left: 20px;

top: -20px;

width: 25px;

height: 25px;

-webkit-box-shadow: 6px 5px 9px -9px black,5px 6px 9px -9px black;

-moz-box-shadow: 6px 5px 9px -9px black,5px 6px 9px -9px black;

box-shadow: 6px 5px 9px -9px black,5px 6px 9px -9px black;

-webkit-transform: rotate(45deg);

-moz-transform: rotate(45deg);

-ms-transform: rotate(45deg);

-o-transform: rotate(45deg);

}

.tooltip.active {

opacity: 1;

margin-top: 5px;

-webkit-transition: all 0.2s ease;

-moz-transition: all 0.2s ease;

-ms-transition: all 0.2s ease;

-o-transition: all 0.2s ease;

}

.tooltip.out {

opacity: 0;

margin-top: -20px;

}

html5表单失去焦点进行非空验证,并用placeholder显示错误提示

简单改了下你的,效果实现了,规范的写法自己改改吧

!DOCTYPE html  

html  

head  

meta charset="UTF-8"/  

titlecheckValidity 示例/title 

style

input.dd::-webkit-input-placeholder{

color: red;

opacity:1;

/style

/head  

body 

form action="" method="get"

table width="200%" border="0" cellspacing="0" cellpadding="0" 

tr

tdinput class='' id='id' name="uname" type="text" placeholder="ID" onblur="aa(this)"/td

/tr

tr

tdinput name="pwd" type="password" placeholder="密码"/td

/tr

tr

tdinput name="" type="submit"/td

/tr

/table

/form

script 

function aa(a){

if(a.value==''){

a.placeholder='ID不能为空';

a.className="dd";

}

}

/script

/body  

/html

HTML5中如何实现表单的自定义验证消息?

实现表单自定义验证消息,实际上很简单。大体思路为:

1、首先我们要设置表单的验证规则

2、然后根据表单的验证规则定义要显示验证消息,

3、最后通过JavaScript 和CSS相结合先将所有预定义的验证消息隐藏掉,然后再根据表单中的输入是否符合验证规则来控制验证消息的显隐状态。

下面我们就来通过一个具体的示例进行讲解,你可以将这个示例直接放在浏览器中运行查看效果,但是要注意代码中引用了第三方类库jQuery,运行时你要自己设置一下:

!doctype html

html

head

meta charset="utf-8" /

titleForms: 验证提示/title

// 设置一些必要的样式,如验证消息的文字样式和控制验证消息显隐的样式

style

input:valid {

background: #fff;

}

input:invalid {

background: #fcc;

color: #333;

}

.validation-messages {

margin-bottom:15px;

}

.validation-messages span {

font-size:0.8em;

background-color:#eee;

padding:6px;

border:1px solid #ccc;

border-radius:10px;

color:#666;

}

//所有class属性为hide的元素都会被隐藏

.hide {

display:none;

}

/style

/head

body

article

h1Formsspan - 验证提示/span/h1

/article

div id="result-stub" class="well hidden"

form id="change-email-form" name="change-email-form"

fieldset

h4修改邮箱地址/h4

//设置表单应该遵循的验证规则。

//pattern 属性:规定用于验证输入字段的模式。

//maxlength 属性:规定输入字段的最大长度,以字符个数计。

//required属性:规定该input元素为必须的元素。

label用户名:/label

input type="text" name="username"

id="username" pattern="[a-zA-Z ]{5,}"

maxlength="30" required /

//设置表单的验证消息,并结合class属性样式将其隐藏起来

div

//这里的data-rule的属性值是HTML5表单中内置的验证规则中的一种

span data-rule="valueMissing"

class="hide"用户名不能为空./span

span data-rule="patternMismatch"

class="hide"必须是字母字符(长度应为5~30个字符)./span

/div

label邮箱地址:/label

//设置表单应该遵循的验证规则。

input type="email" name="email" id="email"

title="Email address is required" required /

//设置表单的验证消息,并结合class属性样式将其隐藏起来

div

span data-rule="valueMissing"

邮箱地址不能为空./span

span data-rule="typeMismatch"

邮箱格式不正确./span

/div

hr /

//设置两个按钮,作用分别是“清空表单内容和对表单中的内容进行验证

button type="submit" id="submit"

name="submit"修改/button

button type="button" id="checkValidation"

name="checkValidation"

class="btn"验证/button

/fieldset

/form

/div

script

window.onload= function() {

//---

// 创建一个验证规则容器。

var ruleNames = [];

// 填充验证规则容器.

// 寻找所有具有验证规则的元素,然后将该元素的data-rule属性值添加到验证规则数组中去。

$("[data-rule]").each(function(i, element) {

var ruleName = element.getAttribute('data-rule');

if ($.inArray(ruleName, ruleNames) 0) {

ruleNames.push(ruleName);

}

});

var

// 首先确定用户界面隐藏了所有验证消息。

// 然后在选定的表单上运行验证规则。

validate = function() {

$(".validation-messages span")

.addClass('hide');

document.getElementById('change-email-form')

.checkValidity();

},

// 查询每个输入的元素,以确定哪个元素无效。

// 一旦检测到无效元素,就遍历验证规则,找到无效的原因,然后通过消息对用户进行提示

validationFail = function(e) {

var

element = e.srcElement,

validity = element.validity;

if (!validity.valid) {

ruleNames.forEach(function(ruleName) {

checkRule(validity,

ruleName,

element);

});

e.preventDefault();

}

},

// 使用 input 元素 的ValidityState 对象的实例来运行验证规则。

//如果这个验证规则返回ture,就表示无法通过验证,在表单中就会有相应的提示信息。

checkRule = function(state, ruleName, ele) {

if (state[ruleName]) {

$(ele).next()

.find('[data-rule="'

+ ruleName + '"]')

.removeClass('hide');

}

};

// 所有验证事件处理程序是附加在input元素上的而不是button元素

$(':input:not(:button)').each(function() {

this.oninvalid = validationFail;

this.onblur = validate;

});

$('#checkValidation').click(validate);

}

/script

script src="../js/jquery.js"/script

/body

/html

推荐你去教程网站秒秒学上过一遍相关知识,希望对你有帮助。

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

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、运行效果:

提交出错就是红色的


本文题目:html5表单提示的简单介绍
URL分享:http://bzwzjz.com/article/phhpsj.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 网站建设方案 企业网站制作 手机网站制作 成都网站建设公司 成都商城网站建设 网站设计 成都网站制作 营销型网站建设 成都网站建设 成都网站建设公司 成都网站建设 网站制作 网站建设方案 成都网站设计 网站设计 成都响应式网站建设 外贸网站建设 手机网站建设套餐 成都网站建设 品牌网站建设 营销型网站建设 手机网站制作