正则表达式在线测试
常用正则
layui.use(['form'], function(){
var form = layui.form;
form.verify({
noSpecialCharacter:function(value,item){
if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
return '不能有特殊字符';
}
if(/(^\_)|(\__)|(\_+$)/.test(value)){
return '首尾不能出现下划线\'_\'';
}
},
noAllNumber:function(value,item){
if(/^\d+\d$/.test(value)){
return '不能全为数字';
}
},
code:function(value,item){
if(/[\u4E00-\u9FA5]/g.test(value)){
return "不能有汉字";
}
},
IpVerify:function(value,item){
var pattren = /(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})(\.(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})){3}/g
if(!pattren.test(value)){
return 'ip格式错误';
}
},
passWord:function(value,item){
if(value.length > 16){
return "密码长度过长";
}
},
maxLength:function(value,item){
if(value.length > 20){
return '长度不能超过20';
}
},
greaterThanZero:function(value,item){
var aa = Number(value);
if(value != null && value != ""){
if(isNaN(aa)){
return '请输入数字';
}
if(value <= 0)
return '数值必须大于0';
if(value.charAt(0) == "0" && value.charAt(1) != ".")
return '请输入正确的数值格式';
}
}
,isPhone:function(value,item){
var myreg=/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
if(value != null && value != ""){
if (!myreg.test(value)) {
return '请输入正确的手机号';
}
}
}
,noNumber:function(value,item){
for(var i = 0; i < value.length;i++){
var aa = value.charAt(i).charCodeAt();
if(aa <= 57 && aa >= 48 ){
return '不允许出现数字';
}
}
}
,Mnumber:function(value,item){
var myreg =/^\+?[1-9][0-9]*$/;
if(value != null && value != ""){
if(!myreg.test(value)){
return '请输入正整数';
}
}
}
,Email:function(value,item){
var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
if(value != null && value != ""){
if (!pattern.test(value)) {
return '请输入正确的邮箱';
}
}
}
,Money:function(value,item){
var money = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if(value != null && value != ""){
if (!money.test(value)) {
return '请输入正确的金额';
}
}
}
//“yyyy-mm-dd” 格式的日期校验,已考虑平闰年。但不包括9999-99-99的验证
,Date:function(value,item){
var date = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
if(value != null && value != ""){
if (!date.test(value)) {
return '请输入正确的日期';
}
}
}
});
});
验证
//邮箱验证
var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
//手机号码验证
var pattern = /^1[345678]\d{9}$/
//金额验证(两位小数)
var money = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
//“yyyy-mm-dd” 格式的日期校验,已考虑平闰年。
var reg =
/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
//正负 0 小数 小数点前后不限制位数
var reg = ^\d+(?:\.\d*)?(?:e[+\-]?\d+)?$
另一种验证方式
//自定义验证规则
form.verify({
password: [/(.+){6,12}$/, '密码必须6到12位'],
password: [/^[0-9A-Za-z]{6,12}$/,'密码必须6到12位,由数字、小写字符和大写字母组成'],
phone: [/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/, '请输入正确的手机号']
});
表单验证
//layui必填项
lay-verify = "required|Mnumber|noNumber"
//只能输入正数和小数点后两位
onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''"
//必填项
<a style="color: red; position: relative;top: 3px;">*</a>
//只读项
style="background-color:#F0F0F0" disabled="disabled" readonly="readonly"
//只能数字
onkeyup="this.value=this.value.replace(/\D/g,'')"