什么是验证用户输入的最佳方法。 PHP还是Javascript?

时间:2017-08-28 20:05:53

标签: javascript php jquery

我正在联系我,我差点完成它,但我想确保人们不容易向我的邮件发送垃圾邮件。目前我正在检查用户是否使用Javascript填写了所有字段,如果没有,则会将元素文本更改为:“您必须填写此字段”。现在我想使用php中的filter_var函数验证文本和电子邮件,然后将文本更改为:“请填写有效的电子邮件”。这是我正在使用的Javascript代码

$(document).ready(function() {
  document.getElementById('submitButton').onclick = function() {
    var firstName = document.forms["contactForm"]["firstName"].value;
    var lastName = document.forms["contactForm"]["lastName"].value;
    var email = document.forms["contactForm"]["email"].value;
    var message = document.forms["contactForm"]["message"].value;

    var submit = true;

    if (firstName == null || firstName == "") {
      document.getElementById("firstName_error").innerHTML = "Please enter your first name";
      submit = false;
    }

    if (lastName == null || lastName == "") {
      document.getElementById("lastName_error").innerHTML = "Please enter your last name";
      submit = false;
    }

    if (email == null || email == "") {
      document.getElementById("email_error").innerHTML = "Please enter your email";
      submit = false;
    }

    if (message == null || message == "") {
      document.getElementById("message_error").innerHTML = "Please enter your message";
      submit = false;
    }

    return submit;
  }

  document.getElementById("firstName").onkeyup = removeWarning;
  document.getElementById("lastName").onkeyup = removeWarning;
  document.getElementById("email").onkeyup = removeWarning;
  document.getElementById("message").onkeyup = removeWarning;
});

function removeWarning() {
  document.getElementById(this.id + "_error").innerHTML = "";
}

0 个答案:

没有答案