[英]RegExp: Matching everything except regex values in Javascript
What I want to achieve is to match every single word and character, except two, Two, TWO
words. 我要实现的是匹配每个单词和字符,除了
two, Two, TWO
单词。 This code is part of a form validation, where two, Two, TWO
are the answers for the captcha question: 此代码是表单验证的一部分,其中
two, Two, TWO
是验证码问题的答案:
//if(! event.target.validity.valid) {
if(! event.target.validity.valid && input.value === /(^(?!two$|Two$|TWO$).*)/gm) {
elem.className = 'error';
parentDiv.className += ' error-input';
elem.style.display = 'block';
};
Unfortunately this is not working. 不幸的是,这不起作用。
UPDATE: 更新:
I tried all regex here and finally the loop also, but still not working. 我在这里尝试了所有正则表达式,最后也尝试了循环,但仍然无法正常工作。 I copying here the entire function to have a more view what's going on:
我在这里复制整个功能,以查看发生了什么事:
var lang = document.getElementsByTagName('html')[0].getAttribute('lang');
(function() {
function field(name, langif, langelse) {
var elem = document.createElement('div');
elem.style.display = 'none';
var input = document.getElementById(name);
var parentDiv = document.getElementsByClassName(name)[0];
input.parentNode.appendChild(elem);
// Turning on when error is presented
input.addEventListener('invalid', function(event) {
event.preventDefault();
if(!event.target.validity.valid && name !== 'captcha') {
elem.className = 'error';
parentDiv.className += ' error-input';
elem.style.display = 'block';
};
if(!event.target.validity.valid && name === 'captcha' && input.value === input.value.match(/(^(?!kettő$|ketto$|Kettő$|Ketto$|KETTŐ$|KETTO$|two$|Two$|TWO$).*)/gm)[0]) {
elem.className = 'error-captcha';
parentDiv.className += ' error-input-captcha';
elem.style.display = 'block';
};
if(!event.target.validity.valid && lang === 'hu-HU') {
elem.textContent = langif;
} else {
elem.textContent = langelse;
};
});
// Turning off when error is not presented
input.addEventListener('input', function() {
if(elem.style.display === 'block') {
elem.className = '';
parentDiv.classList.remove('error-input');
elem.style.display = 'none';
};
});
return;
};
// Init function
field('firstname', 'A keresztnév kötelező és/vagy számokat tartalmazott.', 'Firstname is required and/or the field had numbers.');
field('surname', 'A vezetéknév kötelező és/vagy számokat tartalmazott.', 'Surname is required and/or the field had numbers.');
field('email', 'Email cím kötelező. Ajánlott formátum: valami@domain.hu', 'Email address is required. Recommended format: something@domain.com');
field('message', 'Üzenet mező kitöltése kötelező.', 'Message is required.');
field('captcha', 'Captcha kitöltése kötelező.', 'Captcha is required.');
})();
Expected behaviour: if input field is empty, than throw error. 预期行为:如果输入字段为空,则抛出错误。 if input field is not matching the described captcha answers, than throw error.
如果输入字段与所描述的验证码答案不匹配,则抛出错误。 Now only works if the input field is empty.
现在仅在输入字段为空时有效。
UPDATE 2: 更新2:
JSFiddle: https://jsfiddle.net/Lanti/ja77k6ca/2/ JSFiddle: https ://jsfiddle.net/Lanti/ja77k6ca/2/
Only with relevant code: https://jsfiddle.net/Lanti/ja77k6ca/8/ 仅使用相关代码: https : //jsfiddle.net/Lanti/ja77k6ca/8/
This if
statement is trying to see if input.value
, a string , is equal to /(^(?!two$|Two$|TWO$).*)/gm
, a regular expression . 这个
if
语句试图查看input.value
( 一个字符串 )是否等于/( /(^(?!two$|Two$|TWO$).*)/gm
。*)/ /(^(?!two$|Two$|TWO$).*)/gm
(一个正则表达式) 。
They are of different types, so they will never be equal. 它们是不同类型的,因此它们将永远不相等。
You should instead use String.prototype.match()
to see if input.value
matches the regular expression. 相反,您应该使用
String.prototype.match()
来查看input.value
与正则表达式匹配。
This should give you the desired result: 这应该给您期望的结果:
// check to see if calling "match" on the
// regular expression returns the user input
if(! event.target.validity.valid && input.value === input.value.match(/(^(?!two$|Two$|TWO$).*)/gm)[0]) {
elem.className = 'error';
parentDiv.className += ' error-input';
elem.style.display = 'block';
};
Why not just: 为什么不只是:
var captchaAnswers = {'two': true, 'Two': true, 'TWO': true};
if(!event.target.validity.valid && !(input.value in captchaAnswers)) {
...
};
EDIT: Your actual issue is coming from the invalid event not being fired on the input when the field is not empty. 编辑:您的实际问题来自无效事件,当字段不为空时,未在输入上触发无效事件。 To make it fire the event you should use the pattern attribute on the input element.
要使其触发事件,应在输入元素上使用pattern属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.