简体   繁体   English

如何延迟 javascript 警报直到操作完成

[英]How to delay a javascript alert until action is completed

My javascript validates two fields on my form making sure that user input matches the values in my javascript before they are allowed to continue with their registration or submit the form.我的 javascript 验证表单上的两个字段,确保用户输入与我的 javascript 中的值匹配,然后才允许他们继续注册或提交表单。

However, when they provide the wrong values, they get an alert that tells them the values do not match!但是,当他们提供错误的值时,他们会收到警告,告诉他们值不匹配! Also, when they provide the correct values, they also get an alert to continue their registration.此外,当他们提供正确的值时,他们还会收到继续注册的警报。

I noticed that the alert box pops up to notify users once they fill out the first field and it also pops up when the second field is filled.我注意到,一旦用户填写了第一个字段,就会弹出警告框以通知用户,并且在填写第二个字段时它也会弹出。

Is there a possible means to hide the alert until the user completes both fields before the alert pops up to notify the user if their inputs are correct or wrong?是否有一种可能的方法来隐藏警报,直到用户在弹出警报之前完成两个字段以通知用户他们的输入是正确还是错误?

Any help will be truly appreciated.任何帮助将不胜感激。

Below are my codes;以下是我的代码;

 $('.validate').hide(); $('#phone, #email').on('change', function() { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { $(".validate").show(); } else { alert ("Phone or Email do not match!\nYou cannot submit this form!"); $(".validate").hide(); } }); $('#theForm').on('submit', function(e) { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { // validation failed. cancel the event console.log("not submitting"); e.preventDefault(); return false; } }) function isDataInUse(phone, email) { return (phone === "1234" && email === "1234@gmail.com") }
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <form action='' method='POST' id="theForm"> <div class="validate"><span style="color: red;"><b>Phone and Email Matches!</b></span></div> <input type="phone" name='phone' required='' id="phone" placeholder="0000-000-0000" /> <br/><br/> <input type="email" name='email' required='' id="email" placeholder="hello@youremail.com" /> <br/><br/> <div class="validate"> <button href='/' type='submit' id="submitForm">Submit</button> </div> </form>

http://www.nirsoft.net/utils/product_cd_key_viewer.html http://www.nirsoft.net/utils/product_cd_key_viewer.html

Please see potential solution below请参阅下面的潜在解决方案

$('.validate').hide();

$('#phone, #email').on('change', function() {
  let phone = $('#phone').val();
  let email = $('#email').val();
  if (isDataInUse(phone, email)) {
    $(".validate").show();
  } else {
  
  if(phone.trim().length > 0 && email.trim().length > 0) {
   alert ("Phone or Email do not match!\nYou cannot submit this form!");
  }
    $(".validate").hide();
  }
});

$('#theForm').on('submit', function(e) {
  let phone = $('#phone').val();
  let email = $('#email').val();
  if (isDataInUse(phone, email)) {
    // validation failed. cancel the event
    console.log("not submitting");
    e.preventDefault();
    return false;
  }
})

function isDataInUse(phone, email) {
  return (phone === "1234" && email === "1234@gmail.com")
}

Please also see JSFiddle to play around with: https://jsfiddle.net/jamdevgirl/gmeny6j1/9/另请参阅 JSFiddle 玩玩: https ://jsfiddle.net/jamdevgirl/gmeny6j1/9/

Since the validation is occurs on the change event, which is triggered when the input loses focus, you don't need to delay, but only to check that both inputs are filled before the validation.由于验证发生在输入失去焦点时触发的更改事件上,因此您无需延迟,只需在验证之前检查两个输入是否都已填充。

You can do so by turning the else block into a if else block, and checking that both email and phone field values have a length.为此,您可以将 else 块转换为 if else 块,并检查emailphone字段值是否都有长度。

 $('.validate').hide(); $('#phone, #email').on('change', function() { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { $(".validate").show(); } else if (phone.length && email.length && !isDataInUse(phone, email)) { alert ("Phone or Email do not match!\nYou cannot submit this form!"); $(".validate").hide(); } }); $('#theForm').on('submit', function(e) { let phone = $('#phone').val(); let email = $('#email').val(); if (isDataInUse(phone, email)) { // validation failed. cancel the event console.log("not submitting"); e.preventDefault(); return false; } }) function isDataInUse(phone, email) { return (phone === "1234" && email === "1234@gmail.com") }
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <form action='' method='POST' id="theForm"> <div class="validate"><span style="color: red;"><b>Phone and Email Matches!</b></span></div> <input type="phone" name='phone' required='' id="phone" placeholder="0000-000-0000" /> <br/><br/> <input type="email" name='email' required='' id="email" placeholder="hello@youremail.com" /> <br/><br/> <div class="validate"> <button href='/' type='submit' id="submitForm">Submit</button> </div> </form>

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

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