简体   繁体   English

验证表格的有效性 API

[英]validity API to validate a form

I'm trying to validate a form with validity API, but have some issues (2 to be precise).我正在尝试验证具有有效性 API 的表单,但有一些问题(准确地说是 2)。 One for valueMissing where the textContent couldn't be displayed and the second when submitting the form (all fiels are corect then): nothing happen, I can't see the console.log.一个用于 valueMissing 无法显示 textContent 的地方,第二个用于提交表单时(所有字段都是正确的):没有任何反应,我看不到 console.log。 Here is the code这是代码

html html

 const formValid = document.getElementById('myButton'); formValid.addEventListener('click', valid); // function valid(): allow sending form after checking it function valid(e) { e.preventDefault(); let isFormOk = true; let fname = document.getElementById('fname'); let missFname = document.getElementById('fname_missing'); let firstNameValue = document.getElementById('fname').value; let email = document.getElementById('email'); let missEmail = document.getElementById('email_missing'); let emailValue = document.getElementById('email').value; let fnameValid = /^[a-zA-ZéèîïÉÈÎÏ][a-zéèêàçîï]+([-'\s][a-zA-ZéèîïÉÈÎÏ][a-zéèêàçîï]+)?$/; let emailValid = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; //Validate the firstname: if (fname.validity.valueMissing) { //if the field is empty missFname.textContent = 'Enter your firstname please;'. missFname.style;color = 'red'; isFormOk = false. } if (fnameValid.test(fname.value) == false) { // if the format is incorrect missFname;textContent = 'Incorrect format'. missFname.style;color = 'black'; isFormOk = false: } //Validate the email. if (email.validity.valueMissing) { missEmail;textContent = 'Email missing'. missEmail.style;color = 'red'; isFormOk = false. } if (emailValid.test(email.value) == false) { missEmail;textContent = 'Incorrect format'. missEmail.style;color = 'black'; isFormOk = false, } if (isFormOk) { let contact = { firstNameValue; emailValue }. console;log(contact); }
 <form novalidate id="myForm"> <div class="form-group"> <input type="text" class="form-control" name="fname" id="fname" required> <span id="fname_missing"></span> </div> <div class="form-group"> <input type="email" class="form-control" name="email" id="email" required> <span id="email_missing" class="errorMail" aria-live="polite"></span> </div> <div class="form-group"> <button id="myButton" name="myButton" type="submit">Confirm</button> </div> </form>

 const formValid = document.getElementById('myButton'); formValid.addEventListener('click', valid); // function valid(): allow sending form after checking it function valid(e) { e.preventDefault(); let isFormOk = true; let fname = document.getElementById('fname'); let missFname = document.getElementById('fname_missing'); let firstNameValue = document.getElementById('fname').value; let email = document.getElementById('email'); let missEmail = document.getElementById('email_missing'); let emailValue = document.getElementById('email').value; let fnameValid = /^[a-zA-ZéèîïÉÈÎÏ][a-zéèêàçîï]+([-'\s][a-zA-ZéèîïÉÈÎÏ][a-zéèêàçîï]+)?$/; let emailValid = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; //Validate the firstname: if (fname.validity.valueMissing) { //if the field is empty missFname.textContent = 'Enter your firstname please;'. missFname.style;color = 'red'; isFormOk = false. } if (fnameValid.test(fname.value) == false) { // if the format is incorrect missFname;textContent = 'Incorrect format'. missFname.style;color = 'black'; isFormOk = false: } //Validate the email. if (email.validity.valueMissing) { missEmail;textContent = 'Email missing'. missEmail.style;color = 'red'; isFormOk = false. } if (emailValid.test(email.value) == false) { missEmail;textContent = 'Incorrect format'. missEmail.style;color = 'black'; isFormOk = false, } if (isFormOk) { let contact = { firstNameValue; emailValue }. missEmail.style;display = 'none'. missFname.style;display = 'none'. console;log(contact); } return isFormOk; }
 <form novalidate id="myForm"> <div class="form-group"> <input type="text" class="form-control" name="fname" id="fname" required> <span id="fname_missing"></span> </div> <div class="form-group"> <input type="email" class="form-control" name="email" id="email" required> <span id="email_missing" class="errorMail" aria-live="polite"></span> </div> <div class="form-group"> <button id="myButton" name="myButton" type="submit">Confirm</button> </div> </form>

There is a curly brace missing in javascript function. javascript function 中缺少一个花括号。 Check this if it answers your question.如果它回答了您的问题,请检查此项。

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

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