What I want to achieve is to match every single word and character, except two, Two, TWO words. This code is part of a form validation, where two, Two, TWO are the answers for the captcha question:

我想要达到的是匹配每个单词和字符,除了两个,两个,两个单词。此代码是表单验证的一部分,其中两个,两个,两个是验证码问题的答案:

//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:

JSFiddle: https://jsfiddle.net/Lanti/ja77k6ca/2/

Only with relevant code: https://jsfiddle.net/Lanti/ja77k6ca/8/

仅使用相关代码:https://jsfiddle.net/Lanti/ja77k6ca/8/

2 个解决方案

#1


1

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.

编辑:您的实际问题来自于字段不为空时未在输入上触发的无效事件。要使它触发事件,您应该在input元素上使用pattern属性。

See an updated JSFiddle.

查看更新的JSFiddle。

更多相关文章

  1. 在bootstrap中为同一元素使用两个数据切换
  2. 从两个数组生成JSON
  3. 【JavaScript】中两个小括号 ()() 是什么意思
  4. 有没有办法检查两个数组是否具有相同的元素?
  5. HTML Select字段通过ajax调用Web服务
  6. 阻止用户在表单字段中输入
  7. CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers
  8. 如何在选择单选按钮时显示文本字段
  9. 将输入文本字段显示为纯文本

随机推荐

  1. C++解决方法:多线程同步经典案例之生产者
  2. C++---浅拷贝、深拷贝、写时拷贝讲解(附代
  3. 第六章C++:函数基础与应用
  4. C#入门经典学习阶段小结(凌乱)
  5. C#使用Newtonsoft的Json.NET进行对象的序
  6. 第五章C++:语句的相关介绍
  7. C#_调用封装的一个类实现导出Excel表格的
  8. C# 使用NPOI生成Word文档(按照模板)
  9. 第四章C++:表达式概念-运算符的应用
  10. 如何看待C#中的out和ref?探讨它们之间的区