本段代码举了一个最简单的表单验证实例,就是判断输入框是否为空而已,大家可以根据这个原理,加入正则表达式判断,实现各种功能强大的表单验证功能
Validate empty fields
body{font-family:"Trebuchet MS",verdana;width:450px;}
.error{ color:red; }
#info{color:#008000;font-weight:bold; }
ersonal
Name:*
Address:*
City:
Country:*
$(document).ready(function(){
var dataValid=false;
$('#info').html('');//然后将结果提示也设置为空;
$('.required').blur(function(){//对于required类的元素,如果失去焦点了,则....
var cur=$(this);//获取当前元素
cur.next('span').remove();//然后将错题提示的给移走,不管他之前是什么;
if($.trim(cur.val())=='')
{//判断如果输入为空;
cur.after('Mandatory field!');//则在输入框后添加错题提示信息;
dataValid=false;
}else{dataValid=true;}
});
$('#check').click(function(){//点击了Check按钮之后,执行....
if(dataValid)
{
$('#info').html('Validation OK');//提示通过验证
}});
});
导航网站源码
该片段来自于http://www.huiyi8.com/webyuanma/daohang/
Validate empty fields
body{font-family:"Trebuchet MS",verdana;width:450px;}
.error{ color:red; }
#info{color:#008000;font-weight:bold; }
![;P](https://i-blog.csdnimg.cn/blog_migrate/59e1bcd3e2208202727cfeabdb565eb0.png)
Name:*
Address:*
City:
Country:*
$(document).ready(function(){
var dataValid=false;
$('#info').html('');//然后将结果提示也设置为空;
$('.required').blur(function(){//对于required类的元素,如果失去焦点了,则....
var cur=$(this);//获取当前元素
cur.next('span').remove();//然后将错题提示的给移走,不管他之前是什么;
if($.trim(cur.val())=='')
{//判断如果输入为空;
cur.after('Mandatory field!');//则在输入框后添加错题提示信息;
dataValid=false;
}else{dataValid=true;}
});
$('#check').click(function(){//点击了Check按钮之后,执行....
if(dataValid)
{
$('#info').html('Validation OK');//提示通过验证
}});
});
导航网站源码
该片段来自于http://www.huiyi8.com/webyuanma/daohang/
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29691626/viewspace-1184100/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29691626/viewspace-1184100/