为什么即使校正值也显示验证消息

时间:2018-07-06 10:28:21

标签: javascript

我有html联系人表格,其中处理过JS中的验证。
如果在任何字段中(例如emailid)有任何不正确的数据/值,验证就可以正常进行,但是即使在更正数据之后,验证仍会弹出。
附加相同的代码和屏幕截图。

请帮助。

function askfordemo() {
  var path = document.URL;
  var url = "";
  url = new URL(path);
  var frm = $(document.forms);
  var formData = JSON.stringify(frm.serialize());
  var name = $("#name").val();
  var emailID = $("#emailID").val();
  var phone = $("#phone").val();
  var companyname = $("#companyname").val();
   
  var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  if (filter.test(emailID)) {
    return true;
  } else {
    $("#emailID")[0].setCustomValidity('Invalid Email Address');
    return false;
  }

  if ($('#date1') != null && $('#date1').is(':visible')) {
    var contactdate = new Date($('#date1').val());
    var dateNow = new Date();
    var msg = "Date cannot be less than todays date";
    if (contactdate < dateNow) {
      $("#date1")[0].setCustomValidity('Date cannot be less than todays date');
      return;
    } else if (contactdate == 'Invalid Date') {
      $("#date1")[0].setCustomValidity('Please fill out this field with Date and Time');
      return
    } else {
      $("#date1")[0].setCustomValidity("");
    }
  }

  if (name === '' || emailID === '' || phone === '' || companyname === '' || noofemployees === null || message === '' ||
    radioValue === '' || (typeof radioValue === 'undefined')) {
    return false;
  }
  var param = new Object();
  param.name = encodeURIComponent(name);
  param.emailID = encodeURIComponent(emailID);
  param.phone = encodeURIComponent(phone);
  param.companyname = encodeURIComponent(companyname);
  param.noofemployees = encodeURIComponent(noofemployees);
  param.message = encodeURIComponent(message);
  param.radioValue = encodeURIComponent(radioValue);
  param.date1 = encodeURIComponent(date1);
  param.timezone = encodeURIComponent(timezone);
  var paramjson = JSON.stringify(param);
  path = path.substring(0, path.lastIndexOf("/"));
  $.ajax({
    url: path + '/ms/askForDemo/sendMail',
    type: 'POST',
    data: paramjson,
    dataType: "json",
    contentType: "application/json",
    crossDomain: 'true',
    success: function(formData) {
      JSON.stringify(formData)
    },
    error: function(x, y, z) {
      alert("Internet connection is lost. Please try again later.");
    }
  });
}
enter image description here

0 个答案:

没有答案