表单验证实现代码,php用户注册页面利用js进行表

2019-07-23 03:31栏目:威尼斯网站

复制代码 代码如下:

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=";
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
        <title>用户注册 - 脚本之家</title>
        <link href="../css/login.css" rel="stylesheet" type="text/css" />
        <link href="../css/page_bottom.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../js/jquery-1.7.1.js"></script>
        <script type="text/javascript">
            var flag = {
                "email":false,
                "nickname":false,
                "password":false,
                "verify":false
            };

<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript" src="form.js" src="form.js"></script>
</head>

            $(function(){
                $("#txtEmail").blur(function () {
                                        var email=$(this).val();
                                        //alert(email);
                                        if(email==""){
                                            $("#email\.info").html("Email地址不能为空");
                                            return;
                                        }
                                        var pattern=/b(^['_A-Za-z0-9-] (.['_A-Za-z0-9-] )*@([A-Za-z0-9-]) (.[A-Za-z0-9-] )*((.[A-Za-z0-9]{2,})|(.[A-Za-z0-9]{2,}.[A-Za-z0-9]{2,}))$)b/;
                                        if(!pattern.test(email)){
                                            $("#email\.info").html("Email格式不正确");
                                            return;
                                        }
                                        $.get("check_email.php?email=" email,null,
                                            function(data){
                                                $("#email\.info").html(data);
                                                if (data=="可以注册") {
                                                    flag.email=true;
                                                }
                                            }
                                        );
                                    });
                $("#txtNickName").blur(function () {
                                            var nickname=$(this).val();
                                            if(nickname==""){
                                                $("#name\.info").html("昵称不能为空");
                                                return;
                                            }
                                            var pattern = /b(^['A-Za-z0-9]{4,20}$)b/;
                                            if (!pattern.test(nickname)) {
                                                $("#name\.info").html("昵称格式不正确");
                                                return;
                                            }else{
                                                $("#name\.info").html("昵称格式正确");
                                                flag.nickname=true;
                                                return;
                                            }
                                       });
                $("#txtPassword").blur(function () {
                                            var password=$(this).val();
                                            if (password=="") {
                                                $("#password\.info").html("密码不能为空");
                                                return;
                                            }
                                            var pattern = /b(^['A-Za-z0-9]{4,20}$)b/;
                                            if (!pattern.test(password)) {
                                                $("#password\.info").html("密码格式不正确");
                                                return;   
                                            }else{
                                                $("#password\.info").html("密码格式正确");
                                                //flag.password=true;
                                                return;
                                            }
                                       });
                $("#txtRepeatPass").blur(function () {
                                            var password1=$(this).val();
                                            if (password1=="") {
                                                $("#password1\.info").html("密码不能为空");
                                                return;
                                            }
                                            var pattern = /b(^['A-Za-z0-9]{4,20}$)b/;
                                            if (!pattern.test(password1)) {
                                                $("#password1\.info").html("密码格式不正确");
                                                return;   
                                            }else if(password1!=$("#txtPassword").val()){
                                                $("#password1\.info").html("两次输入的密码不一致");
                                                return;
                                            }else{
                                                $("#password1\.info").html("重复密码正确");
                                                flag.password=true;
                                                return;
                                            }                                            
                                        });
                $("#txtVerifyCode").blur(function () {
                                            var verify=$(this).val();
                                            if(verify==""){
                                                $("#number\.info").html("验证码不能为空");
                                                return;
                                            }
                                            $.post("./verify/check.php",{verify:verify},
                                                function(data){
                                                    $("#number\.info").html(data);
                                                    if (data=="验证成功") {
                                                        flag.verify=true;
                                                    }
                                                }
                                            );
                                         })
                $("#f").submit(function(){
                                    var ok = flag.email&&flag.password&&flag.verify&&flag.nickname;
                                    if(ok==false){
                                        alert("表单项正在检测或存在错误");
                                        history.back();
                                        return false;
                                    }
                                    return true;
                                }); 
            })
        </script>
    </head>
    <body>
        <?php include("../common/head.php"); ?>
        <div class="login_step">
            注册步骤:<span class="red_bold">1.填写信息</span> > 2.验证邮箱 > 3.注册成功
        </div>
        <div class="fill_message">
            <form name="ctl00" method="post" action="save_reg.php" id="f">
                <h2>以下均为必填项</h2>
                <table class="tab_login" >
                    <tr>
                        <td valign="top" class="w1">请填写您的Email地址:</td>
                        <td>
                            <input name="email" type="text" id="txtEmail" class="text_input"/>
                            <div class="text_left" id="emailValidMsg">
                                <p>请填写有效的Email地址。</p>
                                <span id="email.info" style="color:red"></span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" class="w1">设置您在脚本之家的昵称:</td>
                        <td>
                            <input name="nickname" type="text" id="txtNickName" class="text_input" />
                            <div class="text_left" id="nickNameValidMsg">
                                <p>由小写英文字母、中文、数字组成,长度4-20个字符,一个汉字为两个字符。</p>
                                <span id="name.info" style="color:red"></span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" class="w1">设置密码:</td>
                        <td>
                            <input name="password" type="password" id="txtPassword" class="text_input" />
                            <div class="text_left" id="passwordValidMsg">
                                <p>您的密码可以由大小写英文字母、数字组成,长度6-20位。</p>
                                <span id="password.info" style="color:red"></span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" class="w1">再次输入您设置的密码:</td>
                        <td>
                            <input name="password1" type="password" id="txtRepeatPass" class="text_input"/>
                            <div class="text_left" id="repeatPassValidMsg">
                            <span id="password1.info" style="color:red"></span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" class="w1">验证码:</td>
                        <td>
                            <img class="yzm_img" id='imgVcode' src='./verify/verify.php' style="cursor:pointer" border='0' onclick="document.getElementById('imgVcode').src='./verify/verify.php?t=' Math.random()"/>
                            <input name="number" type="text" id="txtVerifyCode" class="yzm_input"/>
                            <div class="text_left t1">
                                <p class="t1">
                                    <span id="vcodeValidMsg">请输入图片中的四个字母。</span>                                    
                                    <a href="#" style="cursor:pointer" onclick="document.getElementById('imgVcode').src='./verify/verify.php?t=' Math.random()">看不清楚?换个图片</a>
                                    <br />
                                    <span id="number.info" style="color:red"></span>
                                </p>
                            </div>
                        </td>
                    </tr>
                </table>

<body>
<form action="post.php" method="get" name="form1" onsubmit="return form_sub()">
<table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="85"><div align="right">姓名:</div></td>
<td width="186"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td><div align="right">密码:</div></td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><div align="right">密码确认:</div></td>
<td><input name="password2" type="password" id="password2"></td>
</tr>
<tr>
<td><div align="right">性别:</div></td>
<td><select name="sex" id="sex">
<option value="0" selected>男</option>
<option value="1">女</option>
</select></td>
</tr>
<tr>
<td><div align="right">生日:</div></td>
<td><input name="birthday" type="text" id="birthday"></td>
</tr>
<tr>
<td><div align="right">E-mail:</div></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td><div align="right">职业:</div></td>
<td><input name="job" type="text" id="job"></td>
</tr>
</table>
<p align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</p>
</form>
</body>
</html>

                <div class="login_in">
                    <input id="btnClientRegister" class="button_1" name="submit" type="submit" value="注 册"/>
                </div>
            </form>
        </div>
        <?php include("../common/foot.php"); ?>
    </body>
</html>

复制代码 代码如下:

您可能感兴趣的文章:

  • AngularJS实现注册表单验证功能
  • js实现登录注册框手机号和验证码校验(前端部分)
  • JS jQuery实现注册信息的验证功能
  • Ionic AngularJS实现登录和注册带验证功能
  • 原生js验证简洁注册登录页面
  • JS实现用户注册时获取短信验证码和倒计时功能
  • js正则表达式注册页面表单验证
  • mvc C# JavaScript LigerUI oracle实现用户的注册、登陆验证、登陆
  • javascript制作的简单注册模块表单验证
  • js注册时输入合法性验证方法

function form_sub()
{
if(!test_username(document.form1.username.value))
{
alert("姓名格式不正确");
return false;
}

if(!test_date(document.form1.birthday.value))
{
alert("日期格式不正确");
return false;
}

if(!test_email(document.form1.email.value))
{
alert("E-mail地址格式不正确");
return false;
}

if(!test_password(document.form1.password.value, document.form1.password2.value))
{
alert("两次密码输入不相同");
return false;
}
}

function test_username(str_username)
{
var pattern = /[a-zA-Z_]/;
if(pattern.test(str_username))
return true;
else
return false;
}

function test_date(str_birthday)
{
var pattern = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
if(pattern.test(str_birthday))
return true;
else
return false;
}

function test_email(str_email)
{
var pattern = /^[a-zA-Z0-9_.] @([a-zA-Z0-9_] .) [a-zA-Z]{2,3}$/;
if(pattern.test(str_email))
return true;
else
return false;
}

function test_password(str_p1, str_p2)
{
if(str_p1==str_p2)
return true;
else
return false;
}

复制代码 代码如下:

<?php
//本程序用于接收来自HTML页面的表单数据并进行相应的验证
$founderr = false; //初始化founderr变量,表示没有错误
if(!ereg("[a-zA-Z_]", $_GET['username']))
{
echo "姓名格式不正确<BR>";
$founderr = true;
}

if(!ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}", $_GET['birthday']))
{
echo "日期格式不正确<BR>";
$founderr = true;
}

if(!ereg("^[a-zA-Z0-9_.] @([a-zA-Z0-9_] .) [a-zA-Z]{2,3}$", $_GET['email']))
{
echo "E-mail地址格式不正确<BR>";
$founderr = true;
}

if($_GET['password'] != $_GET['password2'])
{
echo "两次密码输入不相同";
$founderr = true;
}

if(!$founderr)
{
?>
<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<table width="271" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="85"><div align="right">姓名:</div></td>
<td width="186"><?php echo $_GET['username'] ?></td>
</tr>
<tr>
<td><div align="right">密码:</div></td>
<td><?php echo $_GET['password'] ?></td>
</tr>
<tr>
<td><div align="right">性别:</div></td>
<td><?php if($_GET['sex']==0) echo "男"; else echo "女" ?></td>
</tr>
<tr>
<td><div align="right">生日:</div></td>
<td><?php echo $_GET['birthday'] ?></td>
</tr>
<tr>
<td><div align="right">E-mail:</div></td>
<td><?php echo $_GET['email'] ?></td>
</tr>
<tr>
<td><div align="right">职业:</div></td>
<td><?php echo $_GET['job'] ?></td>
</tr>
</table>
</body>
</html>
<?php
}
?>

您可能感兴趣的文章:

  • php自动提交表单的方法(基于fsockopen与curl)
  • JQuery打造PHP的AJAX表单提交实例
  • PHP 表单提交给自己
  • php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
  • php 表单数据的获取代码
  • 用PHP的超级变量$_POST获取HTML表单(HTML Form) 数据
  • php中一个完整表单处理实现代码
  • php表单转换textarea换行符的方法
  • PHP form 表单传参明细研究
  • php使用curl实现简单模拟提交表单功能

版权声明:本文由威尼斯网站发布于威尼斯网站,转载请注明出处:表单验证实现代码,php用户注册页面利用js进行表