js 正则表达式,动态拼接匹配规则
2021-05-08 02:26
标签:ali ring color style date tchar function element return 需要动态拼出脚本字符串传给eval()方法执行时,需要对字符‘\‘转义 js 正则表达式,动态拼接匹配规则 标签:ali ring color style date tchar function element return 原文地址:https://www.cnblogs.com/hu-kang/p/12082338.html/**
* 验证月份表单输入是否合法
* pattern : 月份格式字符串
* id : 表单id
*/
function validateMonth(pattern, id) {
var text = document.getElementById(id);
var monthStr = text.value;
var splitChar = "";
if(pattern.length > 6) splitChar = pattern.substring(4, pattern.length - 2);
eval("var re = /\\d{4}" + splitChar + "\\d{2}$/;");
//var re = /\d{4}-\d{2}$/;
if(monthStr.match(re) == null) {
alert("请参考格式[" + pattern + "]输入!\n" + "e.g \"2010" + splitChar + "11\" or \"2010" + splitChar + "03\"");
text.value = "";
text.focus();
return false;
}
return true;
}
上一篇:Apache网页优化之缓存
下一篇:Django之Web框架原理