Javascript JQuery Ajax提交函数添加验证

Javascript JQuery Ajax提交函数添加验证,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用以下代码: function submitForm() { $.ajax({type:'POST', url: 'index.php', data:$('#ContactForm').serialize(), success: function(response) { $('#ContactForm').find('.form_result').html(response);

我正在使用以下代码:

function submitForm() {
    $.ajax({type:'POST', 
          url: 'index.php', 
          data:$('#ContactForm').serialize(),
          success: function(response) {   
              $('#ContactForm').find('.form_result').html(response);
            }}
    );
    return false;
}
它工作得很好,但我该如何添加验证,例如。。。要在用户名或密码为空时发出警报

我知道有很多类似的脚本,但我特别想将其添加到这个脚本中。

使用
beforeSend()

function submitForm() {
if(!$("#name").val()){
   alert("name field is empty.");
   return false;
}    


$.ajax({type:'POST', 
    url: 'index.php', 
    data:$('#ContactForm').serialize(), success: function(response) {
    $('#ContactForm').find('.form_result').html(response);
    }});

    return false;
}
  function submitForm() {
  $.ajax({type:'POST', 
      url: 'index.php', 
      data:$('#ContactForm').serialize(),
      success: function(response) {   
          $('#ContactForm').find('.form_result').html(response);
        }},
      beforeSend: function(){
        if(!$("#name").val()){
          alert("name field is empty.");
           return false;
          }    
      }
    );
return false;
}
在发送前使用
beforeSend()

  function submitForm() {
  $.ajax({type:'POST', 
      url: 'index.php', 
      data:$('#ContactForm').serialize(),
      success: function(response) {   
          $('#ContactForm').find('.form_result').html(response);
        }},
      beforeSend: function(){
        if(!$("#name").val()){
          alert("name field is empty.");
           return false;
          }    
      }
    );
return false;
}
在发送前使用
beforeSend()

  function submitForm() {
  $.ajax({type:'POST', 
      url: 'index.php', 
      data:$('#ContactForm').serialize(),
      success: function(response) {   
          $('#ContactForm').find('.form_result').html(response);
        }},
      beforeSend: function(){
        if(!$("#name").val()){
          alert("name field is empty.");
           return false;
          }    
      }
    );
return false;
}
在发送前使用
beforeSend()

  function submitForm() {
  $.ajax({type:'POST', 
      url: 'index.php', 
      data:$('#ContactForm').serialize(),
      success: function(response) {   
          $('#ContactForm').find('.form_result').html(response);
        }},
      beforeSend: function(){
        if(!$("#name").val()){
          alert("name field is empty.");
           return false;
          }    
      }
    );
return false;
}

您必须在ajax调用语句之前执行验证

比如说

function submitForm() {

var username=$('#id').value;
if(username!='')
{
    $.ajax({type:'POST', 

            url: 'index.php', 

            data:$('#ContactForm').serialize(), success: function(response) {

              $('#ContactForm').find('.form_result').html(response);

            }});
}
else
{
alert("Please Enter Username");
}

            return false;
    }

您必须在ajax调用语句之前执行验证

比如说

function submitForm() {

var username=$('#id').value;
if(username!='')
{
    $.ajax({type:'POST', 

            url: 'index.php', 

            data:$('#ContactForm').serialize(), success: function(response) {

              $('#ContactForm').find('.form_result').html(response);

            }});
}
else
{
alert("Please Enter Username");
}

            return false;
    }

您必须在ajax调用语句之前执行验证

比如说

function submitForm() {

var username=$('#id').value;
if(username!='')
{
    $.ajax({type:'POST', 

            url: 'index.php', 

            data:$('#ContactForm').serialize(), success: function(response) {

              $('#ContactForm').find('.form_result').html(response);

            }});
}
else
{
alert("Please Enter Username");
}

            return false;
    }

您必须在ajax调用语句之前执行验证

比如说

function submitForm() {

var username=$('#id').value;
if(username!='')
{
    $.ajax({type:'POST', 

            url: 'index.php', 

            data:$('#ContactForm').serialize(), success: function(response) {

              $('#ContactForm').find('.form_result').html(response);

            }});
}
else
{
alert("Please Enter Username");
}

            return false;
    }

你可能想做这样的事情

function submitForm() {
    if (inputIsValid()) {
        sendAjaxRequest();
    } else {
        ShowMessage();
    }
}

function sendAjaxRequest(parameters) {

    $.ajax({
        type: 'POST',
        url: 'index.php',
        data: $('#ContactForm').serialize(),
        success: function (response) {
            $('#ContactForm').find('.form_result').html(response);
        }
    });
}

function inputIsValid() {
    return $("#username").val() && $("#password").val();
}

function ShowMessage() {
    alert("Please provide username and password");
}

你可能想做这样的事情

function submitForm() {
    if (inputIsValid()) {
        sendAjaxRequest();
    } else {
        ShowMessage();
    }
}

function sendAjaxRequest(parameters) {

    $.ajax({
        type: 'POST',
        url: 'index.php',
        data: $('#ContactForm').serialize(),
        success: function (response) {
            $('#ContactForm').find('.form_result').html(response);
        }
    });
}

function inputIsValid() {
    return $("#username").val() && $("#password").val();
}

function ShowMessage() {
    alert("Please provide username and password");
}

你可能想做这样的事情

function submitForm() {
    if (inputIsValid()) {
        sendAjaxRequest();
    } else {
        ShowMessage();
    }
}

function sendAjaxRequest(parameters) {

    $.ajax({
        type: 'POST',
        url: 'index.php',
        data: $('#ContactForm').serialize(),
        success: function (response) {
            $('#ContactForm').find('.form_result').html(response);
        }
    });
}

function inputIsValid() {
    return $("#username").val() && $("#password").val();
}

function ShowMessage() {
    alert("Please provide username and password");
}

你可能想做这样的事情

function submitForm() {
    if (inputIsValid()) {
        sendAjaxRequest();
    } else {
        ShowMessage();
    }
}

function sendAjaxRequest(parameters) {

    $.ajax({
        type: 'POST',
        url: 'index.php',
        data: $('#ContactForm').serialize(),
        success: function (response) {
            $('#ContactForm').find('.form_result').html(response);
        }
    });
}

function inputIsValid() {
    return $("#username").val() && $("#password").val();
}

function ShowMessage() {
    alert("Please provide username and password");
}

使用此链接可参考查看示例或以下给定脚本

function submit()
{
var username = document.getElementById("txtusername").value;
var password = document.getElementById("txtpassword").value;
if(username == "")
{
alert("enter username");
}
if(username == "")
{
alert("enter username");
 }
else if(password == "")
{
    alert("Enter Password");
}
else
{
//ajax call
}
}

使用此链接可参考查看示例或以下给定脚本

function submit()
{
var username = document.getElementById("txtusername").value;
var password = document.getElementById("txtpassword").value;
if(username == "")
{
alert("enter username");
}
if(username == "")
{
alert("enter username");
 }
else if(password == "")
{
    alert("Enter Password");
}
else
{
//ajax call
}
}

使用此链接可参考查看示例或以下给定脚本

function submit()
{
var username = document.getElementById("txtusername").value;
var password = document.getElementById("txtpassword").value;
if(username == "")
{
alert("enter username");
}
if(username == "")
{
alert("enter username");
 }
else if(password == "")
{
    alert("Enter Password");
}
else
{
//ajax call
}
}

使用此链接可参考查看示例或以下给定脚本

function submit()
{
var username = document.getElementById("txtusername").value;
var password = document.getElementById("txtpassword").value;
if(username == "")
{
alert("enter username");
}
if(username == "")
{
alert("enter username");
 }
else if(password == "")
{
    alert("Enter Password");
}
else
{
//ajax call
}
}

验证应该在ajax调用之前还是之后?验证应该在ajax调用之前还是之后?验证应该在ajax调用之前还是之后?验证应该在ajax调用之前还是之后?