[英]JavaScript Regex is not validating
I have a problem with my code. 我的代码有问题。 It should display red inputbox if I don't match regex else it should appear as green inputbox.
它应显示红色输入框,如果我不匹配正则表达式,否则它应显示为绿色输入框。
function IsValid(pole, regex) { if (regex.test(pole.value)) { pole.className = "ok"; return true; } else { pole.className = "chyba"; return false; } } function OnSubmit(form) { if (IsValid(form.filter_date, dateReg)) { return true; } else { return false; } } window.onload = init; function init() { var filter_date = document.getElementById("filter_date"); dateReg = /^\\d{2}-\\d{2}-\\d{4}$/; filter_date.onblur = function() { IsValid(this, dateReg); } document.forms("filter_form").onsubmit = function() { return OnSubmit(this); } }
.ok { background-color: red; } .chyba { background-color: green; }
<form id="filter_form" name="filter_form" action="" method="post"> <table class="filter_training" cellspacing="0" width="100%"> .... <input type="text" id="filter_date" name="filter_date" placeholder="01-01-1970" />.... </table> </form>
1). 1)。 The main problem is that
onsubmit
handler should be bound this way: 主要问题是
onsubmit
处理程序应该以这种方式绑定:
document.forms.filter_form.onsubmit = function(){
return OnSubmit(this);
}
because document.forms("filter_form")
will throw an error since document.forms
is a collection, not a function. 因为
document.forms("filter_form")
会抛出一个错误,因为document.forms
是一个集合,而不是一个函数。
2). 2)。 Another minor issue is that you confused colors, it should be:
另一个小问题是你混淆了颜色,它应该是:
.ok { background-color: green; }
.chyba { background-color: red; }
It does work: http://jsfiddle.net/d8swfdbj/ 它确实有效: http : //jsfiddle.net/d8swfdbj/
.ok{ color: green;}
.chyba{ color: red; }
Your green/red classes are simply the wrong way round. 你的绿色/红色课程只是错误的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.