jquery实现表单验证,所以的验证通过后方可提交
<html>
<head>
<meta http-equiv=
"content-type"
content=
"text/html; charset=utf-8"
>
<title>Reg</title>
<style>
.state1{
color:#aaa;
}
.state2{
color:#000;
}
.state3{
color:red;
}
.state4{
color:green;
}
</style>
<script src=
"jquery.js"
></script>
<script>
$(
function
(){
var
ok1=false;
var
ok2=false;
var
ok3=false;
var
ok4=false;
// 验证用户名
$(
'input[name="username"]'
).focus(
function
(){
$(this).next().text(
'用户名应该为3-20位之间'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=
''
){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok1=true;
}
else
{
$(this).next().text(
'用户名应该为3-20位之间'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证密码
$(
'input[name="password"]'
).focus(
function
(){
$(this).next().text(
'密码应该为6-20位之间'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=
''
){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok2=true;
}
else
{
$(this).next().text(
'密码应该为6-20位之间'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证确认密码
$(
'input[name="repass"]'
).focus(
function
(){
$(this).next().text(
'输入的确认密码要和上面的密码一致,规则也要相同'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=
''
&& $(this).val() == $(
'input[name="password"]'
).val()){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok3=true;
}
else
{
$(this).next().text(
'输入的确认密码要和上面的密码一致,规则也要相同'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证邮箱
$(
'input[name="email"]'
).focus(
function
(){
$(this).next().text(
'请输入正确的EMAIL格式'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){
$(this).next().text(
'请输入正确的EMAIL格式'
).removeClass(
'state1'
).addClass(
'state3'
);
}
else
{
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok4=true;
}
});
//提交按钮,所有验证通过方可提交
$(
'.submit'
).click(
function
(){
if
(ok1 && ok2 && ok3 && ok4){
$(
'form'
).submit();
}
else
{
return
false;
}
});
});
</script>
</head>
<body>
<form action=
'do.php'
method=
'post'
>
用 户 名:<input type=
"text"
name=
"username"
>
<span
class
=
'state1'
>请输入用户名</span><br/><br/>
密 码:<input type=
"password"
name=
"password"
>
<span
class
=
'state1'
>请输入密码</span><br/><br/>
确认密码:<input type=
"password"
name=
"repass"
>
<span
class
=
'state1'
>请输入确认密码</span><br/><br/>
邮 箱:<input type=
"text"
name=
"email"
>
<span
class
=
'state1'
>请输入邮箱</span><br/><br/>
<a href=
"javascript:;"
><img
class
=
'submit'
type=
'image'
src=
'./images/reg.gif'
/></a>
</form>
</body>
<html>
<head>
<meta http-equiv=
"content-type"
content=
"text/html; charset=utf-8"
>
<title>Reg</title>
<style>
.state1{
color:#aaa;
}
.state2{
color:#000;
}
.state3{
color:red;
}
.state4{
color:green;
}
</style>
<script src=
"jquery.js"
></script>
<script>
$(
function
(){
var
ok1=false;
var
ok2=false;
var
ok3=false;
var
ok4=false;
// 验证用户名
$(
'input[name="username"]'
).focus(
function
(){
$(this).next().text(
'用户名应该为3-20位之间'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=
''
){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok1=true;
}
else
{
$(this).next().text(
'用户名应该为3-20位之间'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证密码
$(
'input[name="password"]'
).focus(
function
(){
$(this).next().text(
'密码应该为6-20位之间'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=
''
){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok2=true;
}
else
{
$(this).next().text(
'密码应该为6-20位之间'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证确认密码
$(
'input[name="repass"]'
).focus(
function
(){
$(this).next().text(
'输入的确认密码要和上面的密码一致,规则也要相同'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=
''
&& $(this).val() == $(
'input[name="password"]'
).val()){
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok3=true;
}
else
{
$(this).next().text(
'输入的确认密码要和上面的密码一致,规则也要相同'
).removeClass(
'state1'
).addClass(
'state3'
);
}
});
//验证邮箱
$(
'input[name="email"]'
).focus(
function
(){
$(this).next().text(
'请输入正确的EMAIL格式'
).removeClass(
'state1'
).addClass(
'state2'
);
}).blur(
function
(){
if
($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){
$(this).next().text(
'请输入正确的EMAIL格式'
).removeClass(
'state1'
).addClass(
'state3'
);
}
else
{
$(this).next().text(
'输入成功'
).removeClass(
'state1'
).addClass(
'state4'
);
ok4=true;
}
});
//提交按钮,所有验证通过方可提交
$(
'.submit'
).click(
function
(){
if
(ok1 && ok2 && ok3 && ok4){
$(
'form'
).submit();
}
else
{
return
false;
}
});
});
</script>
</head>
<body>
<form action=
'do.php'
method=
'post'
>
用 户 名:<input type=
"text"
name=
"username"
>
<span
class
=
'state1'
>请输入用户名</span><br/><br/>
密 码:<input type=
"password"
name=
"password"
>
<span
class
=
'state1'
>请输入密码</span><br/><br/>
确认密码:<input type=
"password"
name=
"repass"
>
<span
class
=
'state1'
>请输入确认密码</span><br/><br/>
邮 箱:<input type=
"text"
name=
"email"
>
<span
class
=
'state1'
>请输入邮箱</span><br/><br/>
<a href=
"javascript:;"
><img
class
=
'submit'
type=
'image'
src=
'./images/reg.gif'
/></a>
</form>
</body>
jquery实现表单验证,所以的验证通过后方可提交的更多相关文章
- ASP.NET MVC Jquery Validate 表单验证的多种方式
在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...
- jquery插件-表单验证插件-rules
ruels方法 1说明:查看.新增.移除一个表单控件的验证规则 2使用: 表单控件.rules(); 参数: rules() 返回元素的验证规则 rules('add',rules) 增加验证规则 r ...
- jQuery实现表单验证
表单是网页的一个重要组成部分.本节做一个简单的表单提交网页然后利用jQuery实现表单的验证.后续的表单完善以及功能的完善会在以后的博客中给出. 效果图: 代码: <!DOCTYPE html ...
- jQuery formValidator表单验证插件
什么是jQuery formValidator? jQuery formValidator表单验证插件是客户端表单验证插件. 在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人 ...
- [转]ASP.NET MVC Jquery Validate 表单验证的多种方式介绍
在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...
- Jquery Validate 表单验证的多种方式
ASP.NET MVC Jquery Validate 表单验证的多种方式 在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体 ...
- 【锋利的jQuery】表单验证插件踩坑
和前几篇博文提到的一样,由于版本原因,[锋利的jQuery]表单验证插件部分又出现照着敲不出效果的情况. 书中的使用方法: 1. 引入jquery源文件, 2. 引入表单验证插件js文件, 3. 在f ...
- 雷林鹏分享:jQuery EasyUI 表单 - 表单验证
jQuery EasyUI 表单 - 表单验证 本教程将向您展示如何验证一个表单.easyui 框架提供一个 validatebox 插件来验证一个表单.在本教程中,我们将创建一个联系表单,并应用 v ...
- jquery实现表单验证简单实例
/* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...
- jQuery的表单验证
jQuery的表单验证 直接上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
随机推荐
- chrom浏览器-F12使用方法二
文摘摘自:https://blog.csdn.net/run65536/article/details/80568543 提示:右键点击图片选择在新窗口或新标签页中打开可查看大图. 一.Element ...
- 【转】Intellij Idea识别Java Web项目
使用maven生成一个Java项目,手动添加相应的web目录WEB_INF,web.xml等,此时idea没有自动识别为web项目,此时编辑web.xml文件会出现一些不该出现的错误,需要做的就是让i ...
- MySQL 实时监控日志
简单的梳理一下为什么要写这边文章,主要是学了ORM之后,发现通过ORM插入数据真的很方便,但是通过ORM生成的SQL语句又是怎么写的呢,百思不得姐.于是就找到了这个办法 首先查看一下查看MySQL 日 ...
- jQuery绑定动态元素的点击事件无效
之前就一直受这个问题的困扰,在写ajax加载数据的时候发现,后面追加进来的demo节点元素,失去了之前的点击事件.为什么点击事件失效,我们该怎么去解决呢?那么,我们通过下面的示例简单说明. 示例如下: ...
- JS中鼠标左右键以及中键的事件
在三维场景中有时候需要判断鼠标的事件,除了使用的click事件,只有鼠标左键有效,而右键无效.而对于onmousedown.onmouseup的时候鼠标的事件左键/右键有效.详细请看w3c上的资料. ...
- shell脚本,如何监控目录下的文件内容是否被修改。
第一种方法是通过cmp来进行比对[root@localhost bo]# ls .html .html .html .html .html .html .html .html .html cat.sh ...
- MFC学习小结
2019/1/13 视频来源 一. MFC框架中一些重要的函数 1. InitInstance函数 应用程序类的一个虚函数,MFC应用程序的入口.初始化的作用. 2. PreCreateWindo ...
- ccf_201712-2
题目 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1 ...
- (27)zabbix自定义图表Graph
zabbix提供了一个自定义图表的功能,这不是废话么?呵呵~前面文章 讲到的<zabbix简易图表>只能显示单个item的数据图表.如果我们想显示多个信息到一个图表上,那必须使用zabbi ...
- NGINX宏观手记
一.这里的优化主要是指对nginx的配置优化,一般来说nginx配置文件中对优化比较有作用的主要有以下几项: nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数. ``` w ...