restrictionInput.js
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){3,15}$/;
if (!patrn.exec(s))
{
return false;
} else
{return true;}
}
function isRegisterPassword(s)
{
var patrn=/^[a-zA-Z0-9]{5,15}$/;
if (!patrn.exec(s))
{
return false;
} else
{return true}
}
function checkEmail(email){
var emailRegExp = new RegExp( "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
if (!emailRegExp.test(email)||email.indexOf('.')==-1){
return false;
}else{ return true;}
}
$(function(){
$("#p_w_picpathField").click(function(){
var username= $.trim($("input[name='username']").val());
var password= $.trim($("input[name='password']").val());
var password2= $.trim($("input[name='password2']").val());
var fname= $.trim($("input[name='fname']").val());
var sex = $("input[name='sex']:checked").val();
var email= $.trim($("input[name='email']").val());
var telephone= $.trim($("input[name='telephone']").val());
var address= $.trim($("input[name='address']").val());
if(username.length<1){alert('用户名不能为空');return false;}
if(username.length<4){alert('用户名不能小于四位');return false;}
if(username.length>16){alert('用户名不能大于十六位');return false;}
if(!isRegisterUserName(username)){alert('用户名必须为数字、字母、下划线,并以字母开始!');return false;}
if(password.length<1){alert('密码不能为空');return false;}
if(password.length<6){alert('密码不能小于六位'); return false;}
if(password.length>16){alert('密码不能大于十六位'); return false;}
if(!isRegisterPassword(password)){alert('密码必须为数字、字母!');return false;}
if(password2.length<1){alert('确认密码不能为空');return false;}
if(password!=password2){alert('密码和确认密码不相同');return false;}
if(fname.length<1){alert('真实姓名不能为空');return false;}
if(sex==undefined){alert('请选择性别');return false;}
if(email.length<1){alert('email不能为空');return false;}
if(!checkEmail(email)){alert('邮箱格式不正确,请确认');return false}
if(telephone.length<1){alert('电话不能为空');return false;}
if(address.length<1){ alert('地址不能为空');return false;}
});
});
只要在jsp页面引入即可!
转载于:https://blog.51cto.com/nothingisgod/394735