<meta charset='utf-8'>
<center>
<form method="post" action="login_pro.php">
用户名:<input type="text" name="name" id="name" οnblur="ming()"><span id="spname"></span><br>
密码:<input type="password" name="pwd" id="pwd" οnblur="mima()"><span id="sppwd"></span><br>
确认密码:<input type="password" name="pwd1" id="pwd1" οnblur="queren()"><span id="sppwd1"></span><br>
邮箱:<input type="text" name="email" id="email" οnblur="youxiang()"><span id="spemail"></span><br>
手机:<input type="text" name="phone" id="phone" οnblur="shouji()"><span id="spphone"></span><br>
固定电话:<input type="text" name="tel" id="tel" οnblur="gutel()"><span id="sptel"></span><br>
邮编:<input type="text" name="you" id="bian" οnblur="youb()"><span id="spyou"></span><br>
地址:<input type="text" name="address" id="address" οnblur="addressa()"><span id="spaddress"></span><br>
<input type="submit" value="注册">
</form>
</center>
<script type="text/javascript">
<!--
//用户名
//姓名——失去焦点事件
function ming(){
var name=document.getElementById("name").value;
if(name=="")
{
document.getElementById("spname").innerHTML="<font color='#ff0000'>用户名不可以为空</font>"
return false;
}
//创建ajax对象
var ajax = new XMLHttpRequest();
//alert(ajax.readyState)
//ajax事件
ajax.onreadystatechange=function()
{
//alert(ajax.readyState)
if (ajax.readyState==4)
{
//接收数据
//alert(ajax.responseText);
document.getElementById("spname").innerHTML = ajax.responseText;
}
}
//与服务器建立连接
ajax.open("get","checkname.php?name="+name);
//处理请求
ajax.send(null);
}
//验证密码
function mima()
{
var pwd=document.getElementById("pwd").value;
if(pwd=="")
{
document.getElementById("sppwd").innerHTML="<font color='#ff0000'>密码不可以为空</font>";
}
else{
document.getElementById("sppwd").innerHTML="<font color='#00ff00'>√密码可用</font>";
}
}
//确认密码
function queren()
{
var pwd1=document.getElementById("pwd1").value;
var pwd=document.getElementById("pwd").value;
if(pwd1=="")
{
document.getElementById("sppwd1").innerHTML="<font color='#ff0000'>确认密码不可以为空</font>";
}
if (pwd1==pwd)
{
document.getElementById("sppwd1").innerHTML="<font color='#00ff00'>确认密码可用</font>";
}else{
document.getElementById("sppwd1").innerHTML="<font color='#ff0000'>两次密码不同,请重新输入</font>";
}
}
//验证邮箱
function youxiang()
{
var email=document.getElementById("email").value;
//alert(email);
if(email=="")
{
document.getElementById("spemail").innerHTML="<font color='#ff0000'>邮箱不可以为空</font>";
}
var r_eamil=/^\w+@\w+(\.)\w+$/
if(r_eamil.test(email)==false){
document.getElementById("spemail").innerHTML="<font color='#ff0000'>×邮箱格式不正确</font>";
return false;
}else{
document.getElementById("spemail").innerHTML="<font color='#00ff00'>√邮箱可用</font>";
}
}
//验证手机
function shouji()
{
var phone=document.getElementById("phone").value;
if (phone=="")
{
document.getElementById("spphone").innerHTML="<font color='#ff0000'>手机号不能为空</font>";
}
var r_tel=/^1[3,5,8]\d{9}$/
if (r_tel.test(phone)==false)
{
document.getElementById("spphone").innerHTML="<font color='#ff0000'>手机号格式不正确</font>";
return false;
}else{
document.getElementById("spphone").innerHTML="<font color='#00ff00'>手机号可用</font>";
}
}
//验证电话
function gutel()
{
var tel=document.getElementById("tel").value;
if (tel=="")
{
document.getElementById("sptel").innerHTML="<font color='#ff0000'>固定电话不能为空</font>";
}
if (r_tel.test(tel)==false)
{
document.getElementById("sptel").innerHTML="<font color='#ff0000'>固定电话格式不正确</font>";
return false;
}else{
document.getElementById("sptel").innerHTML="<font color='#00ff00'>固定电话可用</font>";
}
}
//邮编
function youb()
{
var bian=document.getElementById("bian").value;
if (bian=="")
{
document.getElementById("spyou").innerHTML="<font color='#ff0000'>邮编不能为空</font>";
}
var r_you=/^\d{6}$/
if (r_you.test(bian)==false)
{
document.getElementById("spyou").innerHTML="<font color='#ff0000'>邮编格式不正确</font>";
return false;
}else{
document.getElementById("spyou").innerHTML="<font color='#00ff00'>邮编可用</font>";
}
}
//验证地址
function addressa()
{
var address=document.getElementById("address").value;
if (address=="")
{
document.getElementById("spaddress").innerHTML="<font color='#ff0000'>地址不能为空</font>";
}
var r_you=/^\w{5,}$/
if (r_you.test(address)==false)
{
document.getElementById("spaddress").innerHTML="<font color='#ff0000'>地址格式不正确</font>";
return false;
}else{
document.getElementById("spaddress").innerHTML="<font color='#00ff00'>地址可用</font>";
}
}
//-->
</script>
php验证
<?php
header("content-type:text/html;charset=utf-8");
//print_r($_POST);die;
$name=$_POST['name'];
if($name==""){
alert("用户名不可以为空");
return false;
}
//echo $name;
$link = @mysql_connect('127.0.0.1','root','root') or die('数据库连接失败');
mysql_select_db('demo',$link) or die('选择数据库失败');
mysql_query('set names utf8');
$pwd=$_POST['pwd'];
if($pwd==""){
alert("密码不可以为空");
die;
}
$pwd1=$_POST['pwd1'];
if($pwd1==""){
alert("确认密码不可以为空");
die;
}
if($pwd!=$pwd1){
echo "<script>alert('两次密码不一致,请重新输入');</script>";
die;
}
$email=$_POST['email'];
if($email==""){
alert("邮箱不能为空");
die;
}
if(!preg_match("/^\w+@\w+(\.)\w+$/i",$email)){
echo "邮箱格式错误!!";die;
}
$phone=$_POST['phone'];
if($phone==""){
echo "手机不能为空";
die;
}
if(!preg_match("/^1[3,5,8]\d{9}$/i",$phone)){
echo "手机号格式不正确";
die;
}
$tel=$_POST['tel'];
/*if($tel==""){
echo "电话不能为空";
die;
}
if(!preg_match("/^0\d+-\d+$/i",$tel)){
echo "电话格式不正确";
die;
}
*/
$you=$_POST['you'];
if($you==""){
echo "邮编不能为空";
die;
}
if(!preg_match("/^\d{6}$/i",$you)){
echo "邮编长度不正确";
die;
}
$address=$_POST['address'];
if($address==""){
echo "地址不能为空";
die;
}
if(!preg_match("/^\w{5,}$/i",$address)){
echo "地址不符合要求";
die;
}
$sql="insert into users(name,pwd,pwd1,phone,tel,you,address,email) values('$name','$pwd','$pwd1','$phone','$tel','$you','$address','$email')";
//echo $sql;
$res = mysql_query($sql);
if($res){
echo "<script>alert('添加成功');</script>";
}else{
echo "<script>alert('添加失败');</script>";
}
echo mysql_error();
?>
验证用户名唯一
<?php
header("content-type:text/html;charset=utf-8");
$name=$_GET['name'];
//操作数据库
$link=mysql_connect("127.0.0.1","root","root") or die("连接数据库失败");
//选择数据库
mysql_select_db("demo",$link) or die("选择数据库失败");
//设置字符集
mysql_query("set names utf8");
$sql="select * from users where name='$name'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if($row)
{
echo "名称重复";die;
}
else
{
echo "名称可用";die;
}
?>