js验证手机号邮箱号用户名
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
</head>
<div role="alert" id="warningerr" style="display: none;">
<div id="warningerrmsg"><strong>错误:</strong> 角色名重复</div>
</div>
<script type="text/javascript">
function checkButton()
{
//用户名验证
var res = '';
var username = $('#username').val();
$.ajax
({ //一个Ajax过程
type: "post", //以post方式与后台沟通
url : "1.php", //与此php页面沟通
dataType:'json',//从php返回的值以 JSON方式 解释
async:false,
data: 'username='+username,
success: function(data)
{//如果调用php成功
res = data;
}
});
if (username == '')
{
waraing('<strong>错误:</strong> 用户名不能为空');
return false;
}
if (res.err == 1)
{
waraing('<strong>错误:</strong> 用户名已存在!');
return false;
}
if(username.length>10)
{
waraing('<strong>错误:</strong> 用户名不能超过10个字');return false;
}
////验证邮箱格式
var email = $('#email').val();
var reyx= /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
if(email == '')
{
waraing('<strong>错误:</strong> 邮箱不能为空');return false;
}
if(reyx.test(email) == false)
{
waraing('<strong>错误:</strong> 邮箱格式不对');return false;
}
//验证手机号格式
var tel = $('#phoneNumber').val();
var reg = /^0?1[3|4|5|8][0-9]\d{8}$/;
if(tel == '')
{
waraing('<strong>错误:</strong> 手机号码不能为空');return false;
}
if(reg.test(tel) == false)
{
waraing('<strong>错误:</strong> 手机号码格式不对');return false;
}
$('#form1').submit(function (){
$(this).serialize();
});
}
function waraing(a)
{
$('#warningerrmsg').html(a);
$('#warningerr').show();
window.scrollTo(0,0);
}
</script>
<body>
<form action="1.php" method="post" name="form1" id="form1">
<table width="600" border="1" cellspacing="0" cellpadding="9">
<tr>
<td width="133">用户名:</td>
<td width="425"><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td>手机号码:</td>
<td><input type="text" name="phoneNumber" id="phoneNumber" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="button" id="button" onclick="javascript:return checkButton()">提交</button></td>
</tr>
</table>
</form>
</body>
</html>