前台页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./layui/css/layui.css" media="all">
</head>
<body>
<div style="text-align: center;">
<form class="layui-form layui-form-pane" action="" style="text-align: center;border: 1px; margin-top: 50px;" >
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">确认密码</label>
<div class="layui-input-inline">
<input type="password" name="password_check" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item" >
<button class="layui-btn" lay-submit="" lay-filter="demo2">提交</button>
</div>
</form>
</div>
</body>
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script src="./layui/layui.js" charset="utf-8"></script>
<script>
layui.use(['form', 'layedit', 'laydate'], function(){
var form = layui.form
,layer = layui.layer
,layedit = layui.layedit
,laydate = layui.laydate;
form.on('submit(demo2)', function(data){
console.log(data.field);
/* layer.msg(JSON.stringify(data.field)) */
$.ajax({
url:"form.php",
type:"POST",
dataType:"json",
data:data.field,
success: function(res){
layer.msg(res.msg);
}
})
return false;
});
})
</script>
</html>
后台
<?php
$str = 'qwertyuiopasdfghjklzxcvbnm';
$data = $_POST;
if(strlen($data['username']) <= 4){
echo json_encode(['code'=>0,'msg'=>'用户名长度不得少于4位']);die;
}
// $one = substr($data['username'],0,1);
// if(!substr_count($str,strtolower($one))){
// echo json_encode(['code'=>0,'msg'=>'用户名必须以字母开头']);die;
// }
$o = ord(ucfirst($data['username']));
if($o<65 || $o>90){
echo json_encode(['code'=>0,'msg'=>'用户名必须以字母开头']);die;
}
if(strlen($data['password']) < 10){
echo json_encode(['code'=>0,'msg'=>'用密码长度不得少于10位']);die;
}
if(strcmp($data['password'],$data['password_check']) !== 0)
{
echo json_encode(['code'=>0,'msg'=>'两次密码输入需一致']);
die;
}
echo json_encode(['code'=>1,'msg'=>'注册成功']);