简体   繁体   English

使用 addEventListener 的 Web 表单验证不起作用(使用原始 javascript?)

[英]Web form validation using addEventListener is not working (using raw javascript?)

Currently working on an assignment where we are asked to:目前正在执行一项任务,我们被要求:

Listen for the form submission (submit event) and check that each field is populated.监听表单提交(提交事件)并检查每个字段是否已填充。 Allow the form to submit if all fields are populated.如果填充了所有字段,则允许提交表单。 Otherwise, cancel the submission and inform the user of the field that is missing.否则,取消提交并通知用户缺少的字段。

I'm not sure as to why my code isn't working.我不确定为什么我的代码不起作用。 I used addEventListener to listen for the form's submit event, and then created a list of if statements where if the user's input is blank, (""), then an alert button will pop up notifying the user, and it will stop the form from submitting using.preventdefault.我使用 addEventListener 来监听表单的提交事件,然后创建了一个 if 语句列表,如果用户的输入为空(“”),则会弹出一个警报按钮通知用户,它会停止表单使用.preventdefault 提交。

Here's my javascript:这是我的 javascript:



form.addEventListener("submit", function validate(evt){
    
    if(evt.target[0]== ""){
        alert("Please enter your name!");
        evt.preventDefault;
    }

    if(evt.target[1]== ""){
        alert("Please enter your email!");
        evt.preventDefault;
    }

    if(evt.target[2]== ""){
        alert("Please enter the message's subject!");
        evt.preventDefault;
    }

    if(evt.target[3]== ""){
        alert("Please enter a message!");
        evt.preventDefault;
    }
    
});

And here is the link to the full page: https://github.uconn.edu/pages/ssw19002/dmd-3475/Week-8/web-form/web-form.html这是完整页面的链接: https://github.uconn.edu/pages/ssw19002/dmd-3475/Week-8/web-form/web-form.html

I'm trying to do this using pure raw Javascript.我正在尝试使用纯原始 Javascript 来做到这一点。 No JQuery, or any other frameworks just because that's outside the scope of this assignment.没有 JQuery 或任何其他框架,只是因为它不在此作业的 scope 范围内。

You are not calling the preventDefault method, but only giving a reference to the method.您没有调用preventDefault方法,而只是提供了对该方法的引用。 You are missing the brackets to actually execute the function.您缺少实际执行 function 的括号。 But here you go with another learning: You can store functions in variables and pass them around without using the brackets.但是在这里你 go 有另一个学习:你可以将函数存储在变量中并在不使用括号的情况下传递它们。

        evt.preventDefault; // wrong
        evt.preventDefault(); // correct

Edit: Missed that, indeed - as lil devil pointed out - you also need to take .value of your dom node to get the actual content.编辑:确实错过了 - 正如 lil devil 指出的那样 - 你还需要获取你的 dom 节点的.value来获取实际内容。

However, a more profound way to perform your task would be actually referencing the inputs in your checks instead of going for children, as the form might contain other dom nodes as well.但是,执行任务的更深入的方法实际上是在检查中引用输入,而不是使用子项,因为表单也可能包含其他 dom 节点。 So something like this:所以是这样的:

form.addEventListener('submit', e => {

  let invalid = [];

  Array.from(form.querySelectorAll('input')).forEach(node => {
    switch( node.getAttribute('type' ) {
       case 'text':
          // Check for Text values
          if( !textValidationFunction( node ) ) {
            invalid.push(node)
          }
          break;
       case 'checkbox':
          // Check for Checkbox values
          break;
     }
  })

   // Do the same for dropdowns etc.
   // And then, at best, split everything into single methods
 
  if( invalid.length > 0 ) {
    e.preventDefault();
    // Now you have all your invalid inputs in the invalid array to display them in whatever way you want.
    // Give them a red border, get the name attribute, ...
  }
   // Of course there are lots of other ways to structure this :)

})

You need to reference evt.target[0].value instead of just evt.target[0]您需要引用evt.target[0].value而不仅仅是evt.target[0]

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

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