表单验证 JS版

代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>实验</title>
	<style type="text/css">
		*:focus{
			outline:none;/*该元素获得焦点时,不出现虚线框(或高亮框)*/
		}
		form{
			width: 750px;
			height: 500px;
			background-color: #FFC0CB;
			position: absolute;
			left: 30px;
			top: 30px;
			border: 3px solid #9DB5F3 ;
		}
		form ul {
		    width:750px;
		    list-style: none;/*清除默认样式*/
		    padding: 0;
		    margin: 0;
		}
		form li{
		    padding:3px;
		}
		form h1{
			
			text-align: center;
		}
		form span.left{
		    width:100px;
		    margin-top: 3px;
		    margin-left: 30px;
		    display:inline-block;/*把块元素强制转换为行内块元素*/   
		}
		/*给类名为usually的input标签设置背景颜色、背景图片、边框、外阴
		影、内阴影、边框圆角以及内边距的过渡效果*/
		input#usually{
			width: 200px;
			height: 25px;
		    background: #fff url(img/attention.png) no-repeat 98% center;
		    border:1px solid #9DB5F3;
		    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
		    border-radius:2px;
		    transition: padding .25s;
		}
		/*给类名为usually的input标签设置背景颜色、背景图片、边框、外阴
		影、内阴影、边框圆角以及内边距的过渡效果*/
		input#special{
			width: 200px;
			height: 25px;
		    background: #EBEBE3;
		    border:1px solid #9DB5F3;
		    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
		    border-radius:2px;
		    transition: padding .25s;
		}
		input#color{
			width: 100px;
			background-color: #EBEBE3;
		}
		li:nth-child(2) input{
			background-color: #EBEBE3;
		}
		li:last-child button:first-child{
			margin-left: 134.5px;
		}
		/*设置按钮样式*/
		button[type^=submit],button[type^=reset] {
			width: 110px;
			height: 30px;
			margin-right: 90px;
		    background-color: #EBEBE3;
		    border: 1px solid #9DB5F3;
		    border-radius: 3px;
		    padding: 6px 20px;/*内边距:上下6px、左右20px*/
		    text-align: center;/*文本对齐方式:水平居中*/		
		}
		
		/*当鼠标悬停在提交按钮时,该按钮背景颜色透明度为85%,光标变成“小手”*/
		button[type^=submit]:hover,button[type^=reset]:hover {
		    opacity:.85;
		    cursor: pointer;
		}
		/*当鼠标点击按钮时,出现1px的#20911e色的实现边框同时出现内阴影*/
		button[type^=submit]:active,button[type^=reset]:active{
			border: 1px solid #9DB5F3;
			box-shadow: 0 0 10px 5px #9DB5F3 inset;
		}
	</style>
</head>
<body>
	<form onsubmit="return check()">
		<ul>
			<li><h1>员工信息登记表</h1></li>
			<li>
				<span class="left">用户登录名:</span>
				<input type="text" name="idname" placeholder="wcz@163.com" id="special">
			</li>
			<li>
				<span class="left">真实姓名:</span>
				<input type="text" name="name" placeholder="王传智" id="usually" class="usually">
				<span id="checktext1">( 必填,只能输入汉字 )</span>
			</li>
			<li>
				<span class="left">真实年龄:</span>
				<input type="number" name="age" placeholder="24" id="usually" class="usually">
				<span id="checktext2">( 必填 )</span>
			</li>
			<li>
				<span class="left">出生日期:</span>
				<input type="date" name="birthday" id="usually" class="usually">
				<span id="checktext3">( 必填 )</span>
			</li>
			<li>
				<span class="left">电子邮箱:</span>
				<input type="text" name="mail" placeholder="123456@126.com" autocomplete id="usually" class="usually">
				<span id="checktext4">( 必填 )</span>
			</li>
			<li>
				<span class="left">身份证号:</span>
				<input type="text" name="idnumber" id="usually" class="usually">
				<span id="checktext5">( 必填,能够以数字、字母x结尾的短身份证号 )</span>
			</li>
			<li>
				<span class="left">手机号码:</span>
				<input type="text" name="phone" id="usually" class="usually">
				<span id="checktext6">( 必填 )</span>
			</li>
			<li>
				<span class="left">幸运颜色:</span>
				<input type="color" name="color" id="color">
				<span>( 请选择你喜欢的颜色 )</span>
			</li>
			<br/>
			<li>
				<button type="submit">提交</button>
				<button type="reset">重置</button>
			</li>
		</ul>
	</form>
	<script>
		var str="请填写此字段。";
		var arr=['必填,只能输入汉字','必填','必填','必填','必填,能够以数字、字母x结尾的短身份证号','必填'];
		function $(id){
			return document.getElementById(id);
		}
		var input=document.getElementsByClassName('usually');
		function checkempty(){
			var flag;
			for(var i=0;i<input.length;++i){
				if (input[i].value=='') {
					$('checktext'+(i+1)).innerHTML=str;
					input[i].style.background = '#fff url(img/warn.png) no-repeat 98% center';
					input[i].style.boxShadow = '0 0 5px #d45252';
					input[i].style.borderColor='#b03535';
					flag=false;
				}
			}
			return flag;
		}
		var f=false,f1=false,f2=false,f3=false,f4=false,f5=false,f6=false;
		for(let i=0;i<input.length;++i){
			input[i].addEventListener('focus', function(e){
				let obj=e.target;
				obj.style.background = '#fff url(img/warn.png) no-repeat 98% center';
				obj.style.boxShadow = '0 0 5px #d45252';
				obj.style.borderColor ='#b03535';
				obj.style.border='1px solid #555';
				obj.style.boxShadow='0 0 3px #aaa';
				obj.style.paddingRight='70px';
			});
			input[i].addEventListener('blur', function(e){
				let obj=e.target;
				obj.style.paddingRight='0px';
			});
			input[i].addEventListener('input', function(e){
				if ($('checktext'+(i+1)).innerHTML=='请填写此字段。') {
					$('checktext'+(i+1)).innerHTML=arr[i];
				}
				let obj=e.target;
				
				if (obj.name=='name') {
					var str=/^([\u4E00-\u9FA5]+(\u2022)?)+$/;
					f1=ismatch(str);
				}
				if(obj.name=='age'){
					if(obj.value>=18&&obj.value<=120){
						obj.style.background = '#fff url(img/right.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #5cd053';
						obj.style.borderColor='#28921f';
						f2=true;
					}else {
						obj.style.background = '#fff url(img/warn.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #d45252';
						obj.style.borderColor='#b03535';
						f2=false;
					}
				}
				if(obj.name=='birthday'){
					if(isNaN(obj.value)&&!isNaN(Date.parse(obj.value))){
						obj.style.background = '#fff url(img/right.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #5cd053';
						obj.style.borderColor='#28921f';
						f3=true;
					}else {
						obj.style.background = '#fff url(img/warn.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #d45252';
						obj.style.borderColor='#b03535';
						f3=false;
					}
				}
				if (obj.name=='mail') {
					var str=/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
					f4=ismatch(str);
				}
				if (obj.name=='idnumber') {
					var str=/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
					f5=ismatch(str);
				}
				if (obj.name=='phone') {
					var str=/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
					f6=ismatch(str);
				}
				function ismatch (str) {
					if(obj.value.match(str)){
						obj.style.background = '#fff url(img/right.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #5cd053';
						obj.style.borderColor='#28921f';
						return true;
					}else {
						obj.style.background = '#fff url(img/warn.png) no-repeat 98% center';
						obj.style.boxShadow = '0 0 5px #d45252';
						obj.style.borderColor='#b03535';
						return false;
					}
				}
				f=f1&&f2&&f3&&f4&&f5&&f6;
			});	
		}
		function check(){
            checkempty();
            return f;
        }
	</script>
</body>
</html>

效果演示图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值