运用正则表达式对表单提交的验证(包含Java验证方法)

代码块:

<!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>
<style>   
*:focus {outline: none;}
.a{
    width: 40%;
    position: absolute;/
    top: 25%;
    left: 25%;
    background-color:pink;
    font-family:Georgia;
   }
.a h2 {
    margin:0;
   }
.a span {
    width:150px;
    display:inline-block;
    padding:3px;
   }
.a li{
    padding:8px;
    border-bottom:1px solid #eee;
    position:relative;
   }
.a li:first-child,
.a li:last-child {
    border-bottom:1px solid #777;
   }
.a button{margin-left:180px;}
ul{
    width:750px;
    list-style: none;
    margin:0px;
    padding:0px;
   }
.usually input {
    height:15px;
    width:220px;
    padding:5px 8px;
   }
.usually input,
.usually textarea {
    background: #fff url(images/attention.png) no-repeat 98% center;
    border:1px solid #aaa;
    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
    border-radius:2px;
    transition: padding 0.25s;
   }
.usually input:focus,
.usually textarea:focus {
    background: #fff;
    border:1px solid #555;
    box-shadow: 0 0 3px #aaa;
    padding-right:70px;
   }
.usually input:focus:invalid,
.usually textarea:focus:invalid {
    background: #fff url(images/warn.png)no-repeat 98% center;
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535
   }
.usually input:required:valid,
.usually textarea:required:valid{
    background: #fff url(images/right.png) no-repeat 98% center;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
   }
</style>
</head>
<body>
<div>
<form class="a" action="#" method="post" name="a">
    <ul>
         <li class="usually">
            <h2><center>员工信息表</center></h2>
        </li>
     <li class="usually">
            <span>用户登录名:</span>
            <input type="email"  name="email" placeholder="javin@example.com" required />
        </li>
        <li class="usually">
            <span>真实姓名:</span>
            <input type="text" id="name" name="name" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')" required/ >
        </li>
        <li class="usually">
            <span>真实年龄:</span>
            <input type="number" name="age"  min="0" max="200" required/>
        </li>
        <li class="usually">
            <span>出生年月:</span>
            <input type="var" name="born" required/>
        </li>
        <li class="usually">
            <span>电子邮箱:</span>
            <input type="email"  name="password" placeholder= "javin@example.com" required />
        </li>
        <li class="usually">
            <span >身份证号:</span>
            <input type="number" nemr="idname"  required />
        </li>
             <li class="special">
            <span >幸运颜色:</span>
            <input type="color" >
        </li>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit  value=提交>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=reset value=重置>
    </ul>
</form>
    </div> 
 </body>
</html>

实验结果:
在这里插入图片描述
**区别:**Java类型更加注重内部包的建立 习惯用集合类型统一制定正则表达式的准则
Java验证类型:

<!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>
<style>   
*:focus {outline: none;}
.a{
    width: 40%;
    position: absolute;/
    top: 25%;
    left: 25%;
    background-color:pink;
    font-family:Georgia;
   }
.a h2 {
    margin:0;
   }
.a span {
    width:150px;
    display:inline-block;
    padding:3px;
   }
.a li{
    padding:8px;
    border-bottom:1px solid #eee;
    position:relative;
   }
.a li:first-child,
.a li:last-child {
    border-bottom:1px solid #777;
   }
.a button{margin-left:180px;}
ul{
    width:750px;
    list-style: none;
    margin:0px;
    padding:0px;
   }
.usually input {
    height:15px;
    width:220px;
    padding:5px 8px;
   }
.usually input,
.usually textarea {
    background: #fff url(images/attention.png) no-repeat 98% center;
    border:1px solid #aaa;
    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
    border-radius:2px;
    transition: padding 0.25s;
   }
.usually input:focus,
.usually textarea:focus {
    background: #fff;
    border:1px solid #555;
    box-shadow: 0 0 3px #aaa;
    padding-right:70px;
   }
.usually input:focus:invalid,
.usually textarea:focus:invalid {
    background: #fff url(images/warn.png)no-repeat 98% center;
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535
   }
.usually input:required:valid,
.usually textarea:required:valid{
    background: #fff url(images/right.png) no-repeat 98% center;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
   }
</style>
</head>
<body>
<script>
    //姓名验证-"name=“name”"的调用
    function NameCheck() {
        let reg = /^[\u4e00-\u9fa5]{2,6}$/;
        let name = $("#name").val();
        if (!reg.test(name) || name=='') {
            $(".name").html("<span class='red'>请输入2~6位中文</span>");
            return false;
        }else{
            $(".name").text('√');
            return true;
        }
    }
    //邮箱验证-"name='email'"
    function emailCheck(str) {
        let reg = /^\w{3,12}@\w{1,5}\.[a-z]{2,3}$/;
        let email = $("#email").val();
        if(!reg.test(email) || email==''){
            $(".email").html("<span class='red'>请输入正确的邮箱</span>")
            return false;
        }else{
            $(".email").text('√');
            return true;
        }
    }
</script>
<div>
<form class="a" action="#" method="post" name="a">
    <ul>
        <li class="usually">
            <h2><center>员工信息表</center></h2>
        </li>
     <li class="usually">
            <span>用户登录名:</span>
            <input type="email"  name="email" placeholder="javin@example.com" required />
        </li>
        <li class="usually">
            <span>真实姓名:</span>
            <input type="text" id="name" name="name" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')" required/ >
        </li>
        <li class="usually">
            <span>真实年龄:</span>
            <input type="number" name="age"  min="0" max="200" required/>
        </li>
        <li class="usually">
            <span>出生年月:</span>
            <input type="var" name="born" required/>
        </li>
        <li class="usually">
            <span>电子邮箱:</span>
            <input type="email"  name="password" placeholder= "javin@example.com" required />
        </li>
        <li class="usually">
            <span >身份证号:</span>
            <input type="number" nemr="idname"  required />
        </li>
             <li class="special">
            <span >幸运颜色:</span>
            <input type="color" >
        </li>
        <input type=submit  value=提交>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=reset value=重置>
    </ul>
</form>
    </div> 
 </body>
</html>

实验结果:
插入Script的验证方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值