简体   繁体   English

如何将我的 javascript 正确链接到我的 html 表单?

[英]How can I correctly link my javascript to my html form?

My javascript isn't running when I click submit on my form page.当我在表单页面上单击提交时,我的 javascript 没有运行。

<form onsubmit="validateReg()">

    <p>
     //email registration
    <input type="text" id="e-mail" placeholder="Email" />
  </p><p>
    //password registration
    <input type="text" id="pswd" placeholder="Password" />
  </p>
    <br>
    <input type="submit" class="submit">
  </for

I've tried multiple times linking the Javascript to the Html form and on the page when I click submit it doesn't return any of my error alerts.我已经多次尝试将 Javascript 链接到 Html 表单,并且在页面上单击提交时它不会返回任何我的错误警报。

//HTML
<form onsubmit="validateReg()">

    <p>
    <input type="text" id="e-mail" placeholder="Email" />
  </p><p>
    <input type="text" id="pswd" placeholder="Password" />
  </p>
    <br>
    <input type="submit" class="submit">
  </form>

//Javascript 
//Main Function
  function validateReg(){
  var email = document.getElementById('e-mail').value;
  var password = document.getElementById('pswd').value;

  var emailRGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

  var emailResult = emailRGEX.test(email);
//validate Email
if(emailResult == false){
  alert("Please enter a valid email address");
  return false;
}
//validate lower case
  var lowerCaseLetters = /[a-z]/g;
if(password.value.match(lowerCaseLetters)) {
    return true;
  }else{
    alert("Password needs a lower case!");
    return false;
  }
  //validate upper case
  var upperCaseLetters = /[A-Z]/g;
if(password.value.match(upperCaseLetters)){
    return true;
  }else{
    alert("Password needs an upper case!");
    return false;
  }
  //validate numbers
  var numbers = /[0-9]/g;
if(password.value.match(numbers)){
  return true;
}else{
  alert("Password needs a number!");
  return false;
}
//validate special characters
  var special = /[!@#$%^&*(),.?":{}|<>]/g;
if(password.value.match(special)){
  return true;
}else{
  alert("Password needs a special character!");
  return false;
}
  if(password.value.length >=8){
    return true;
  }else{ alert("Password needs to be at least 8 characters");
   return false;
 }
}

I expect the code to output errors when a password is incorrectly submitted and when a password and email is correctly submitted so out put thank you.我希望代码在错误提交密码以及正确提交密码和电子邮件时输出错误,所以输出谢谢。

A better way to do this is to add an event listener to your js file and listen for the 'submit' event.更好的方法是在 js 文件中添加一个事件侦听器并侦听“提交”事件。 Followed by your function.其次是你的功能。

Furthermore ensure that your js file is added to your script tag in your HTML file.此外,请确保将您的 js 文件添加到 HTML 文件中的脚本标记中。 That should work if your logic is correct.如果您的逻辑正确,那应该可行。

As Oluwafemi put it you could put an event listener on your 'submit' event instead.正如 Oluwafemi 所说,您可以在“提交”事件上放置一个事件侦听器。 I would put the event on the submit button though.不过,我会将事件放在提交按钮上。 That way you can stop it on the click event without having to fire the submit of the form.这样你就可以在点击事件上停止它,而不必触发表单的提交。 If you update your code it could help with troubleshooting in the future.如果您更新您的代码,它可以帮助您在未来进行故障排除。

It wouldn't take much to modify your code either.修改您的代码也不需要太多。

First, you would need to update your form to look like this:首先,您需要将表单更新为如下所示:

<form id="form">
    <p>
    <input type="text" id="e-mail" placeholder="Email" />
  </p>
  <p>
    <input type="text" id="pswd" placeholder="Password" />
  </p>
  <br />
    <input id="submitButton" type="submit" class="submit">
  </form>

Then add this below your javascript function like so:然后将其添加到您的 javascript 函数下方,如下所示:

document.querySelector("#submitButton").addEventListener("click", function(event) {
         event.preventDefault;

         validateReg()
}, false);

What this is doing is stopping the submit of the form and doing the check as expected.这样做是停止提交表单并按预期进行检查。 You can read more on this on the Mozilla developer site .您可以在Mozilla 开发人员站点上阅读更多相关内容。

You will need to add document.getElementById('form').submit();您需要添加document.getElementById('form').submit(); to any return statement that was set to true .到任何设置为true return 语句。

I did however, update the code to have the submit become the default functionality and the checks just return false if they fail like this:但是,我确实更新了代码以使提交成为默认功能,并且如果它们像这样失败,检查只会返回 false:

//Javascript 
//Main Function
function validateReg() {

  var email = document.getElementById('e-mail').value;
  var password = document.getElementById('pswd').value;

  var emailRGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

  var emailResult = emailRGEX.test(email);

  //validate Email
  if(emailResult == false){
    alert("Please enter a valid email address");
    return false;
  }

  //validate lower case
  var lowerCaseLetters = /[a-z]/g;
  if(password.match(lowerCaseLetters) == null) {
    alert("Password needs a lower case!");
    return false;
  }

  //validate upper case
  var upperCaseLetters = /[A-Z]/g;
  if(password.match(upperCaseLetters) == null){
    alert("Password needs an upper case!");
    return false;
  }

  //validate numbers
  var numbers = /[0-9]/g;
  if(password.match(numbers) == null){
    alert("Password needs a number!");
    return false;
  }

  //validate special characters
  var special = /[!@#$%^&*(),.?":{}|<>]/g;
  if(password.match(special) == null){
    alert("Password needs a special character!");
    return false;
  }

  if(password.length < 8){
    return false;
  }

  document.getElementById('form').submit();
}

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

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