简体   繁体   English

如何使用Vanilla JavaScript验证任一表单字段

[英]How to validate for either form fields with vanilla JavaScript

I'm attempting to send an error message when either the email field or the phone field of a form doesn't match the regex. 当表单的电子邮件字段或电话字段与正则表达式不匹配时,我尝试发送错误消息。 The validation message shouldn't submit if either fields are filled in. 如果填写了两个字段,则不应提交验证消息。

What happens right now when I go to submit the form with one of the fields filled in with the proper information the form gives me the error message and will not post the form. 现在,当我去提交带有正确信息的字段之一的表单时,会发生什么,该表单会给我错误消息,并且不会发布该表单。 Once I enter the correct input into the other field it processes the form. 在其他字段中输入正确的输入后,它将处理该表单。

What I want it to do is to process the form if either the email field is filled out or the phone field is filled out with information that matches the regular expressions. 我想要做的是如果电子邮件字段或电话字段中填充了与正则表达式匹配的信息,则处理该表单。

If neither of the forms are filled out correctly I want the form to throw the error message. 如果没有正确填写任何表格,我希望表格抛出错误信息。

Here's the if statement I am working with so far. 到目前为止,这是我正在使用的if语句。

<form id="contact_form"  action="" method="POST">
    <input type=hidden name="" value="">
    <input type=hidden name="" value="">

    <p class="errmsg" id="name_errormsg"></p>

    <input  id="name" maxlength="80" name="form_name" placeholder="Name" size="20" type="text" />
    <input  id="email" maxlength="80" name="email" placeholder="Email" size="20" type="text" />
    <input  id="phone" maxlength="40" name="phone" placeholder="Phone number" size="20" type="text" />

    <textarea id="description" name="description" placeholder="How can we help you?"></textarea>

    <input type="submit" name="submit" value="Send message">
</form>
$(document).ready(function() {

  $overlay = $(".modal-overlay");
  $modal = $(".modal-frame");


  $modal.bind('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e){
  if($modal.hasClass('state-leave')) {
    $modal.removeClass('state-leave');
  }
});
$('.form-close-button').on('click', function(){
     $overlay.removeClass('state-show');
     $modal.removeClass('state-appear').addClass('state-leave');
   });

   $('#contactformbtn').on('click', function(){
     $overlay.addClass('state-show');
     $modal.removeClass('state-leave').addClass('state-appear');
   });



var formHandle = document.forms[0];

formHandle.onsubmit = processForm;


function processForm(){


  var emailInput = document.getElementById('email');

  var emailValue = emailInput.value;


  var phoneInput = document.getElementById('phone');

  var phoneValue = phoneInput.value;




  var regexPhone = /^(1?(-?\d{3})-?)?(\d{3})(-?\d{4})$/;
  var regexEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

if((!regexPhone.test(phoneValue)) ||(!regexEmail.test(emailValue))) {
    nameErr = document.getElementById("name_errormsg");
    nameErr.innerHTML = "Please enter your phone number or a valid email address.";
    nameErr.style.color = "red";
    return false;
}

}
});

If any of you could point out where I went wrong this that would be great! 如果你们中有人能指出我出了错的地方,那就太好了!

Thank you for taking the time to read this. 感谢您抽出时间来阅读。

Have a good day. 祝你有美好的一天。

Based on your last comment (which should be in the question) your logic is wrong. 根据您的最后评论(应该在问题中),您的逻辑是错误的。

You're currently checking for failure of either field. 您目前正在检查任何一个字段是否失败。 If phone fails or email fails. 如果电话失败电子邮件失败。 If one field isn't filled in it'll fail because you don't allow blank. 如果没有填写一个字段,它将因为您不允许空白而失败。

You want to test for failure of both fields (with a caveat): 您要测试两个字段的失败情况(警告):

if (!regexPhone.test(phoneValue) && !regexEmail.test(emailValue)) {
    ....

Or you can change your regex. 或者您可以更改您的正则表达式。

The caveat is that say a user enters in a valid phone, but an invalid email: what should happen in that case? 需要注意的是,说用户输入的是有效电话,但输入的是无效电子邮件:在这种情况下应该怎么办? Should validation pass or fail? 验证应该通过还是失败?

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

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