<form class="form">
<div class="my-form-group">
<label class="my-form-label">非空<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input"/>
</div>
<div class="my-form-group">
<label class="my-form-label">电话<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input telephone"/>
</div>
<div class="my-form-group">
<label class="my-form-label">邮箱<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input email"/>
</div>
<div class="my-form-group">
<label class="my-form-label">身份证号<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input idcard"/>
</div>
<div class="my-form-group">
<label class="my-form-label">整数<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input int"/>
</div>
<div class="my-form-group">
<label class="my-form-label">正整数<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input posInt"/>
</div>
<div class="my-form-group">
<label class="my-form-label">负整数<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input negInt"/>
</div>
<div class="my-form-group">
<label class="my-form-label">浮点数(2位)<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input floatNum"/>
</div>
<div class="my-form-group">
<label class="my-form-label">整数(min-max)<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input intRange"/>
</div>
<div class="my-form-group">
<label class="my-form-label">浮点数(min-max)<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input floatRange"/>
</div>
<div class="my-form-group">
<label class="my-form-label">英文+数字<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input letNum"/>
</div>
<div class="my-form-group">
<label class="my-form-label">英文+符号<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input letChar"/>
</div>
<div class="my-form-group">
<label class="my-form-label">英文+数字+符号<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input letCharNum"/>
</div>
<div class="my-form-group">
<label class="my-form-label">仅可输入汉字<span class="my-form-label-required">*</span></label>
<input type="text" class="required my-form-input chinese"/>
</div>
<div class="my-form-group" style="text-align:center;">
<button type="button" class="layui-btn">保存</button>
<button type="button" class="layui-btn layui-btn-primary">取消</button>
</div>
</form>
<style>
.form{
position: relative;
top:1rem;
left:50%;
width:60rem;
margin-left:-30rem;
}
.my-form-group{
clear: both;
margin-bottom:10px;
}
.my-form-label{
width: 10rem;
float: left;
text-align: right;
padding: 0 0.5rem;
height: 3rem;
font-size: 0.8rem;
}
.my-form-label-required{
color:red;
}
.my-form-input{
margin-bottom:5px !important;
width:50rem !important;
height:2.5rem !important;
float: left;
}
.error-input{
border:1px solid red;
}
.error{
color: red;
clear: both;
margin-left: 10rem;
}
.fa-warning{
color:#ffcc00;
}
@media screen and (max-width:640px){
.form{
width:24rem;
margin-left:-12rem;
}
.my-form-label{
width:6rem;
}
.my-form-input{
width:17rem !important;
}
}
</style>
<script>
// 验证公共方法
function checkReg($ele,v,reg,errorTip){
if(v != ''){
var _this = $ele;
if(!(reg.test(v))){
showError($(_this),errorTip);
return false;
}else{
removeError($(_this));
return true;
}
}
}
// 验证数值范围的公共方法,当min/max为null时,代表无下/上限,exp为数组格式,代表允许输入的特殊值,iseq代表是否包含边缘数值true/false(默认包含)
function RangeFunc($ele,type,v,reg,min,max,exp,errorTip1,errorTip2,iseq){
if(v != ''){
// 首先根据正则表达式判断输入内容是否为有效数字
var _this = $ele;
if(!(reg.test(v))){
showError($(_this),errorTip1);
return false;
}else{
if(type=='i'){
v = parseInt(v);
min = parseInt(min);
max = parseInt(max);
}else if(type=='f'){
v = parseFloat(v);
min = parseFloat(min);
max = parseFloat(max);
}
var flag = false;
// 当值为有效数字时,判断是否在范围内(整数/浮点数)
if(min == null && max != null){
if(iseq && max >= v){
flag=true;
}
if(!iseq && max > v){
flag=true;
}
}else if(min != null && max == null){
if(iseq && min <= v){
flag=true;
}
if(!iseq && min < v){
flag=true;
}
}else if(min != null && max != null){
if(iseq && min <= v && max >= v){
flag=true;
}
if(!iseq && min < v && max > v){
flag=true;
}
}else if(min == null && max == null){
alert("方法调用错误,请传入数值范围参数。")
}
if(flag){
removeError($(_this));
return true;
}else{
showError($(_this),errorTip2);
return false;
}
}
}
}
// 提取公共添加错误提示的方法
function showError($ele,errorTip){
$ele.addClass("error-input");
if($ele.nextAll('.error').length>0){
$ele.nextAll('.error').html('<span class="fa fa-warning"></span> '+errorTip);
}else{
$ele.after('<div class="error"><span class="fa fa-warning"></span> '+errorTip+'</div>');
}
}
// 移除错误提示的方法
function removeError($ele){
$ele.removeClass('error-input');
$ele.nextAll('.error').remove();
}
//非空
$(".required").on("blur",function(){
var v = $(this).val().trim();
var tip = '此处不可为空';
if(v == '') showError($(this),tip);
else {
if($(this).nextAll('.error').text() == tip) removeError($(this));
}
});
// 电话
$('.telephone').on('change',function(){
var telephone = $(this).val().trim();
checkReg($(this),telephone,/^1[3456789]\d{9}$/,'电话号码格式错误');
});
// 邮箱
$('.email').on('change',function(){
var email = $(this).val().trim();
checkReg($(this),email,/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,'邮箱格式错误');
})
// 身份证号码
$('.idcard').on('change',function(){
var idcard = $(this).val().trim();
if(idcard.length == 18){
// 身份证号=6位地址码+8位出生日期+3位顺序码+1位校验码
checkReg($(this),idcard,/^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|81|82)[0-9]{4}[1-9][0-9]{3}((0[1-9])|(1[0-2]))(([0-2][0-9])|(3[0-1]))[0-9]{3}[0-9Xx]$/,'身份证号码格式错误');
// 日期校验
var dateStr = idcard.substr(6,8);
var year = parseInt(dateStr.substr(0,4));
var month = parseInt(dateStr.substr(4,2));
var day = parseInt(dateStr.substr(6,2));
var flag = 0;
// 判断日期是否合理:闰年,及每月最后一天
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month == 12){
if(day > 31){
flag = 1;
}
}else if(month == 4 || month == 6 || month == 9 || month == 11){
if(day > 30){
flag = 1;
}
}else if(month == 2){
if(year%4 == 0){
if(day > 28){
flag = 1;
}
}else{
if(day > 29){
flag = 1;
}
}
}
// 检验码校验
var ai = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];//前17位加权因子,每位上的数字x对应的加权因子,所有乘积加和,模11即可得X的值
var x = [1,0,'X',9,8,7,6,5,4,3,2];//X的值0-10对应如下11个校验码
var arr = idcard.substr(0,17).split("");
var sum = 0;
for(var i=0;i<17;i++){
sum += arr[i]*ai[i];
}
var yzm = x[sum%11];
if(yzm != parseInt(idcard.substr(17))){
flag = 1;
}
if(flag){
showError($(this),'身份证号码格式错误');
}
}
})
// 整数
$('.int').on('change',function(){
var int = $(this).val().trim();
checkReg($(this),int,/^(0|((\-)?[1-9][0-9]*))$/,'整数格式错误');
})
// 正整数
$('.posInt').on('change',function(){
var posInt = $(this).val().trim();
checkReg($(this),posInt,/^[1-9][0-9]*$/,'正整数格式错误');
})
// 负整数
$('.negInt').on('change',function(){
var negInt = $(this).val().trim();
checkReg($(this),negInt,/^\-[1-9][0-9]*$/,'负整数格式错误');
})
// 浮点数(2位)
$('.floatNum').on('change',function(){
var floatNum = $(this).val().trim();
checkReg($(this),floatNum,/^(0.00|((\-)?([1-9][0-9]*)(.)?[0-9]{0,2})|((\-)?0.[0-9]{0,1}[1-9]))$/,'浮点数格式错误');
})
// 整数(min-max)
$('.intRange').on('change',function(){
var intRange = $(this).val().trim();
RangeFunc($(this),0,intRange,/^(0|((\-)?[1-9][0-9]*))$/,-100,100,[],'请输入整数','数值超出指定范围',true);
})
// 浮点数(min-max)
$('.floatRange').on('change',function(){
var floatRange = $(this).val().trim();
RangeFunc($(this),1,floatRange,/^(0.00|((\-)?([1-9][0-9]*)(.)?[0-9]{0,2})|((\-)?0.[0-9]{0,1}[1-9]))$/,-100.01,100.01,[],'请输入两位小数','数值超出指定范围',true);
})
// 英文+数字
$('.letNum').on('change',function(){
var letNum = $(this).val().trim();
checkReg($(this),letNum,/^([a-zA-Z0-9])*$/,'仅支持输入英文+数字组合');
})
// 英文+指定字符@_*&
$('.letChar').on('change',function(){
var letChar = $(this).val().trim();
checkReg($(this),letChar,/^([a-zA-Z@_*&])*$/,'仅支持输入英文+数字组合');
})
// 英文+数字+符号
$('.letCharNum').on('change',function(){
var letCharNum = $(this).val().trim();
checkReg($(this),letCharNum,/^([a-zA-Z0-9@_*&])*$/,'仅支持输入英文+数字+指定字符组合');
})
// 汉字
$('.chinese').on('change',function(){
var chinese = $(this).val().trim();
checkReg($(this),chinese,/^([\u4e00-\u9fa5])*$/,'仅支持输入汉字');
})
</script>