简体   繁体   English

一次将一个项目推入数组

[英]push item into array one at a time

Is there anyway i can push more letter into the array and call them faster?无论如何我可以将更多的字母推入数组并更快地调用它们吗? I'm trying to code a validation form....... This works but it run one at time and moves to another when the condition is satisfied or input validated or not validated.我正在尝试编写一个验证表单....... 这有效,但它一次运行一个,并在满足条件或输入验证或未验证时移动到另一个。 Thats after click on submit... i need support in javascript. You can check the pic here.那是在点击提交之后...我需要 javascript 的支持。您可以在此处查看图片。 https://prnt.sc/g8IKHTGdF1s2.. . https://prnt.sc/g8IKHTGdF1s2.. I was thinking of using while loop or for loop to add items (a, b etc) to the array but got stucked, dont know where to start我正在考虑使用 while 循环或 for 循环将项目(a、b 等)添加到数组但卡住了,不知道从哪里开始

const errorsFVEd = () => {
  let errors = [];                                     


  const fnames = firstNam.value.trim();
  const lnames = lastNam.value.trim();
  const addys = Addy.value;
  const cits = CitNam.value;
  

  if (!isRequired(fnames)) {
    errors.push("a");
  } else if (!isAlphabet(fnames)) {
    errors.push("b");
  } else if (!isAlphabet(lnames)) {
    errors.push("d");
  } else if (!isRequired(lnames)) {
    errors.push("e");
  } else if (!isRequired(addys)) {
    errors.push("f");
  } else if (!isRequired(cits)) {
    errors.push("g");
  } else if (!isAlphabet(cits)) {
    errors.push("h");
  } else {
    console.log(1);
  }
  return errors;




}


function validaForm() {
    
    let errors = errorsFVEd();
  
  if (errors.length == 0) {
      
    document.getElementById("signup").submit();
      
  }
  else {

      
    for (i = 0; i < errors.length; i++){
      if (errors[i] == "a") {
        document.getElementById("error-first").innerHTML = "First name required.";
      } else if (errors[i] == "b") {
        document.getElementById("error-first").innerHTML = "Only alphabets allowed";
      } else if (errors[i] == "d") {
        document.getElementById("error-last").innerHTML = "Last name required.";
      } else if (errors[i] == "e") {
        document.getElementById("error-last").innerHTML = "Only alphabets allowed.";
      }else if (errors[i] == "f") {
        document.getElementById("error-addy").innerHTML = "Address required";
      } else if (errors[i] == "g") {
        document.getElementById("error-city").innerHTML = "City required";
      }else if (errors[i] == "h") {
        document.getElementById("error-city").innerHTML = "Invalid city";
      }else {
        console.log(1);
      }
      
      
    }
     
      
      
  }
      
  
}
``

Now, I didn't see the form, but I guess that it is something like the below example.现在,我没有看到表格,但我猜它类似于下面的示例。 Here I make use of the attributes required and pattern .在这里,我使用了requiredpattern属性。 A pattern is a regular expression and it is used for testing if the input is OK or not.模式是一个正则表达式,用于测试输入是否正确。

The second thing are the event listeners for the events input and invalid .第二件事是事件inputinvalid的事件监听器。 So, on invalid the class name invalid is added to the input element (when trying to submit) and when starting to input something it is removed.因此,在无效时,class 名称invalid被添加到输入元素(当尝试提交时),当开始输入内容时,它被删除。 This will make the span element show and hide.这将使 span 元素显示和隐藏。

 document.forms.form01.addEventListener('submit', e => { e.preventDefault(); console.log('submit'); }); document.forms.form01.addEventListener('input', e => { e.target.classList.remove('invalid'); }); document.forms.form01.addEventListener('invalid', e => { e.preventDefault(); e.target.classList.add('invalid'); }, true);
 form { display: flex; flex-direction: column; }.error { display: none; } input.invalid+span.error { display: inline; }
 <form name="form01"> <label>First: <input type="text" name="first" pattern="^\S{2,200}$" required> <span class="error">Only characters allowed (between 2 and 200).</span> </label> <label>Last: <input type="text" name="last" pattern="^\S{2,200}$" required> <span class="error">Only characters allowed (between 2 and 200).</span> </label> <label><button>Submit</button></label> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM