使用javascript正则表达式匹配验证输入文本字段
我想在正则表达式的帮助下验证表单中的两个文本字段,但在遍历字段值时,我在regex match上遇到了一个错误-在表单提交时,我遇到了一个错误:“Uncaught TypeError:elem.match不是函数” 我想使用日期格式dd.mm.yyyy,我需要使用文本字段,不能使用日期字段 HTML: 例如:使用javascript正则表达式匹配验证输入文本字段,javascript,regex,validation,Javascript,Regex,Validation,我想在正则表达式的帮助下验证表单中的两个文本字段,但在遍历字段值时,我在regex match上遇到了一个错误-在表单提交时,我遇到了一个错误:“Uncaught TypeError:elem.match不是函数” 我想使用日期格式dd.mm.yyyy,我需要使用文本字段,不能使用日期字段 HTML: 例如: 你知道这为什么不起作用吗?使用元素值匹配(…)不元素匹配(…)当然,谢谢! <form method="post" onsubmit="checkFor
你知道这为什么不起作用吗?使用
元素值匹配(…)
不元素匹配(…)
当然,谢谢!
<form method="post" onsubmit="checkForm(); return false;" action="#">
<input type="text" id="jform0" name="jform0" value="12.12.2020" class="calendar_date">
<input type="text" id="jform1" name="jform1" value="12.12.2020" class="calendar_date">
<input type="submit" value="Posodobi">
</form>
function checkForm() {
// regular expression to match required date format
re = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
document.querySelectorAll(".calendar_date").forEach((elem) => {
if (elem.value != "" && !elem.match(re)) {
alert("Invalid date format: " + elem.value);
elem.focus();
return false;
}
});
alert("All input fields have been validated!");
return true;
}