正则表达式

本文详细介绍了一个学生信息注册表单的设计与实现过程,包括各字段的验证规则及JavaScript脚本的具体实现。通过正则表达式对姓名、密码、邮箱等进行格式校验,确保数据的有效性和规范性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<meta charset="utf-8">
<?php
    if(!empty($_COOKIE['name'])){
        header('location:zhengze3.php');
    }
?>
<center>
<table border="1">
    <form action="zhengze2.php" method="post" onsubmit="return check_all();">
    <h2>欢迎注册学生信息</h2>
    <tr>
        <td>姓名:</td>
        <td>
            <input type="text"  name="name" id="name" onblur="check_name(this);">
            <span id="u_name"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>密码:</td>
        <td>
            <input type="password"  name="psd" id="psd" onblur="check_psd(this);">
            <span id="u_psd"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>确认密码:</td>
        <td>
            <input type="password"  name="pwd" id="pwd" onblur="check_pwd();">
            <span id="u_pwd"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>性别:</td>
        <td>
            <input type="radio"  name="sex" value="男" id="sex1" onchange="check_sex();"><input type="radio"  name="sex" value="女" id="sex2" onchange="check_sex();"><span id="u_sex"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>邮箱:</td>
        <td>
            <input type="text"  name="email" id="email" onblur="check_email(this);">
            <span id="u_email"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>城市:</td>
        <td>
            <select name="city" id="city" onchange="check_city(this);">
                <option value="">请选择</option>
                <option value="北京">北京</option>
                <option value="上海">上海</option>
                <option value="广州">广州</option>
                <option value="武汉">武汉</option>
            </select>
            <span id="u_city"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>手机号:</td>
        <td>
            <input type="text" id="tel"  name="tel" onblur="check_tel(this);">
            <span id="u_tel"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>座机号:</td>
        <td>
            <input type="text"  name="phone" id="phone" onblur="check_phone(this);">
            <span id="u_phone"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>身份证号:</td>
        <td>
            <input type="text"  name="id_card" id="id_card" onblur="check_id_card(this);">
            <span id="u_id_card"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>QQ号:</td>
        <td>
            <input type="text"  name="qq" id="qq" onblur="check_qq(this);">
            <span id="u_qq"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td>自我介绍:</td>
        <td>
            <textarea name="content" id="content" onblur="check_content(this);"></textarea>
            <span id="u_content"><font color="red">*</font></span>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="checkbox" name="cookies" value="七天免登陆">七天免登陆
            <input type="submit" value="提交" id="sub" onclick="check_sub();">
            <input type="reset" value="重置">
        </td>
    </tr>
    </form>
</table>
</center>
<script>
    function check_name(obj){
        var a=obj.value;
        var b=/^[a-zA-Z]\w{4,9}$/i;
        if(a==""){
            document.getElementById('u_name').innerHTML="<font color='red'>姓名不能为空</font>";
            return false;
        }
        if(b.test(a)){
            document.getElementById('u_name').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_name').innerHTML="<font color=red>必须由5到10位数字或者字母组成,不允许数字开头</font>";
            return false;
        }
    }
    function check_psd(obj){
        var a=obj.value;
        var b=/^\w{7,}$/i;
        if(psd==""){
            document.getElementById('u_psd').innerHTML="<font color=red>密码不能为空</font>";
            return false;
        }
        if(b.test(a)){
            document.getElementById('u_psd').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_psd').innerHTML="<font color=red>密码必须在6位以上</font>";
            return false;
        }
    }
    function check_pwd(){
        var psd=document.getElementById('psd').value;
        var pwd=document.getElementById('pwd').value;
        if(pwd==""){
            document.getElementById('u_pwd').innerHTML="<font color=red>确认密码不能为空</font>";
            return false;
        }
        if(pwd==psd)
        {
            document.getElementById('u_pwd').innerHTML="<font color=greem>ok</font>";
            return true;

        }else{
            document.getElementById('u_pwd').innerHTML="<font color=red>确认密码必须与密码一致</font>";
            return false;
        }
    }
    function check_sex(){
        var sex1=document.getElementById('sex1').checked;
        var sex2=document.getElementById('sex2').checked;
        if(!sex1 && !sex2){
            document.getElementById('u_sex').innerHTML="<font color=red>性别必选一项</font>";
            return false;
        }else{
            document.getElementById('u_sex').innerHTML="<font color=greem>ok</font>";
            return true;
        }
    }
    function check_email(obj){
        var email=obj.value;
        var new_email=/^\w+@+\w+\.(com|cn|net)$/;
        var a=email.lastIndexOf('@');
        var b=email.lastIndexOf('.');
        if(email==""){
            document.getElementById('u_email').innerHTML="<font color=red>邮箱不能为空</font>";
            return false;
        }
        if(new_email.test(email)){
            if(a!=-1 & b!=-1 & b>a){
                document.getElementById('u_email').innerHTML="<font color=greem>这是"+(email.substring(a+1,b))+"邮箱</font>";
                return true;
            }
        }else{
            document.getElementById('u_email').innerHTML="<font color=red>邮箱格式不对</font>";
            return false;
        }
    }
    function check_city(obj){
        var city=obj.value;
        if(city==""){
            document.getElementById('u_city').innerHTML="<font color=red>必选一项</font>";
            return false;
        }else{
            document.getElementById('u_city').innerHTML="<font color=green>ok</font>";
            return true;
        }
    }
    function check_tel(obj){
        var tel=obj.value;
        var new_tel=/^(15|18|13)\d{9}$/;
        if(tel==""){
            document.getElementById('u_tel').innerHTML="<font color=red>手机号不能为空</font>";
            return false;
        }
        if(new_tel.test(tel)){
            document.getElementById('u_tel').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_tel').innerHTML="<font color=red>必须以11位数字组成,以15,18,13开头</font>";
            return false;
        }
    }
    function check_phone(obj){
        var phone=obj.value;
        var new_phone=/^0[1-9]0-\d{8}$/;
        if(phone==""){
            document.getElementById('u_phone').innerHTML="<font color=red>座机号不能为空</font>";
            return false;
        }
        if(new_phone.test(phone)){
            document.getElementById('u_phone').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_phone').innerHTML="<font color=red>必须以010-68801717这种格式,前面是三位的区号,后面是8位的电话号,中间是-</font>";
            return false;
        }
    }
    function check_id_card(obj){
        var id_card=obj.value;
        var new_id_card=/^(\d{15}|\d{18}|\d{17}X)$/;
        if(id_card==""){
            document.getElementById('u_id_card').innerHTML="<font color=red>身份证不能为空</font>";
            return false;
        }
        if(new_id_card.test(id_card)){
            document.getElementById('u_id_card').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_id_card').innerHTML="<font color=red>必须是15或者18位数字组成,18位的最后一位可以是X</font>";
            return false;
        }
    }
    function check_qq(obj){
        var qq=obj.value;
        var new_qq=/^\d{8,11}$/;
        if(qq==""){
            document.getElementById('u_qq').innerHTML="<font color=red>QQ不能为空</font>";
            return false;
        }
        if(new_qq.test(qq)){
            document.getElementById('u_qq').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_qq').innerHTML="<font color=red>必须是8到11位数字组成</font>";
            return false;
        }
    }
    function check_content(obj){
        var content=obj.value;
        var new_content=/^[\u4e00-\u9fa5]{1,20}$/; //php表达式为[\x4e00-\x9fa5]
        if(content==""){
            document.getElementById('u_content').innerHTML="<font color=red>简介不能为空</font>";
            return false;
        }
        if(new_content.test(content)){
            document.getElementById('u_content').innerHTML="<font color=greem>ok</font>";
            return true;
        }else{
            document.getElementById('u_content').innerHTML="<font color=red>必须在20字以内汉字</font>";
            return false;
        }
    }
    function check_all(){
        if(check_name(document.getElementById('name')) & check_psd(document.getElementById('psd')) & check_pwd() & check_sex() & check_email(document.getElementById('email')) & check_city(document.getElementById('city')) & check_tel(document.getElementById('tel')) & check_phone(document.getElementById('phone')) & check_id_card(document.getElementById('id_card')) & check_qq(document.getElementById('qq')) & check_content(document.getElementById('content'))){
            return true;
        }else{
            return false;
        }
    }
</script>
<tr>
    <td>自我介绍:</td>
    <td>
        <textarea name="content" id="content" placeholder="请输入少于50个字"  onkeyup="check_content();">
        </textarea>
        <span id="u_content"></span>
    </td>
</tr>
//自我介绍
function check_content(){
    var content=document.getElementById('content').value;
    var b=/^(\w|[\u4e00-\u9fa5]){1,50}$/;
    if(content.length<=50){
        document.getElementById('u_content').innerHTML="<font color='green'>"+"还可以输入"+(50-content.length)+"字"+"</font>";
        return true;
    }else{
        document.getElementById('content').value=content.substr(0,50);
        document.getElementById('u_content').innerHTML="<font color='red'>输入已经上限</font>";
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值