0

我对下面的编码有两个问题。

首先,现在我想在提交前进行验证。如果从验证功能中检测到一些错误,我该如何停止提交?它只是在每个错误消息后返回 false 吗?但是,它似乎仍在检查所有字段,而不是在出现一个错误后停止。

其次,我想通过 php 插入数据。每次都可以成功地将数据添加到数据库中,但是它总是提示“错误:错误”。我不知道错误来自哪里......

$(document).ready(function()
{
    $('#test').click(function(){
    validation();   
    });

    function validation(){  
    var loginID=$("#loginID").val();

    if (loginID=="" || loginID==null)
    {
        $('#errorID').empty();
        $('#errorID').append(
        '<h6>' + "The Login Name cannot be empty" + '</h6>');
        $("#errorID").show();

    }
    else
    {

    }

    // check pw
    $("#errorPW").hide();
    if ($("#loginPW").val()=="" || $("#loginPW").val()==null)
    {
        $('#errorPW').empty();
        $('#errorPW').append(
        '<h6>' + "The Login Password cannot be empty" + '</h6>');
        $("#errorPW").show();

    }
    else
    {

    }

    //return false;

    } // end of #validation


    $('form').submit(function(){

        validation();
        $.ajax({
            type: 'POST',
            data: 
            {
                    loginID:        $("#loginID").val(),
                    // some data here

            },
            url: 'http://mydomain.com/reg.php',
            success: function(data){
                alert('successfully.');         
            },
            error: function(jqXHR, textStatus){
                alert("Error: " + textStatus);
            }
        });

        return false;
    });
});
4

5 回答 5

1

你可以使用return false。它将停止执行

<form onSubmit="validatdeForm();"></form>


function validatdeForm()
{
  //here return true if validation passed otherwise return false
}

或者

 if (loginID=="" || loginID==null)
    {
        $('#errorID').empty();
        $('#errorID').append(
        '<h6>' + "The Login Name cannot be empty" + '</h6>');
        $("#errorID").show();
        return false;

    }

    if ($("#loginPW").val()=="" || $("#loginPW").val()==null)
{
    $('#errorPW').empty();
    $('#errorPW').append(
    '<h6>' + "The Login Password cannot be empty" + '</h6>');
    $("#errorPW").show();
    return false;

}
于 2013-03-22T06:57:14.407 回答
1

为了进行正确的表单验证,我建议您以更有条理的方式进行。更容易调试。尝试这个:

var validation = {
  // Checking your login ID
  'loginID' : function() {
    // Login ID validation code here...
    // If a validation fails set validation.errors = true;
    // Additionally you can have a validation.idError that contains
    // some error message for an id error.
  },

  // Checking your password
  'loginPW' : function() {
    // Password validation code here...
    // If a validation fails set validation.errors = true;
    // As with id, you can have a validation.pwError that contains
    // some error message for a password error.
  },

  'sendRequest' : function () {
    if(!validation.errors) {
      // Code for whatever you want to do at form submit.
    }
  }
};

$('#test').click(function(){
  validation.errors = false;
  validation.loginID();
  validation.loginPW();
  validation.sendRequest();
  return false;
});
于 2013-03-22T11:28:40.567 回答
1

它应该如下所示。出现错误时返回错误的停止执行脚本。

function validation(){  
    var loginID=$("#loginID").val();

    if (loginID=="" || loginID==null)
    {
        $('#errorID').empty();
        $('#errorID').append(
        '<h6>' + "The Login Name cannot be empty" + '</h6>');
        $("#errorID").show();
        return false;

    }
    else
    {
       return true; 
    }

    // check pw
    $("#errorPW").hide();
    if ($("#loginPW").val()=="" || $("#loginPW").val()==null)
    {
        $('#errorPW').empty();
        $('#errorPW').append(
        '<h6>' + "The Login Password cannot be empty" + '</h6>');
        $("#errorPW").show();
        return false;

    }
    else
    {
        return true;
    }

    return true;

    } // end of #validation
于 2013-03-22T06:59:10.117 回答
1

如下设计您的validation功能,

function validation()
{  
    var isValid = true;

    if(field validation fail)
    {
        isValid = false;
    }
    else if(field validation fail)
    {
        isValid = false;
    }

    return isValid;
}

背后的基本思想codereturning false每当你validation失败时。

于 2013-03-22T06:59:21.163 回答
0
function validateimage() { if($("#photo").val() !== '' ) {

    var extensions = new Array("jpg","jpeg","gif","png","bmp");
    var image_file = document.form_useradd.photo.value;
    var image_length = document.form_useradd.photo.value.length;
    var pos = image_file.lastIndexOf('.') + 1;
    var ext = image_file.substring(pos, image_length);
    var final_ext = ext.toLowerCase();
    for (i = 0; i < extensions.length; i++)
    {
    if(extensions[i] == final_ext)
    {
    return true;
    }
    }
    alert(" Upload an image file with one of the following extensions: "+ extensions.join(', ') +".");

    //$("#error-innertxt_photo").show().fadeOut(5000);
    //$("#error-innertxt_photo").html('Enter valid file type');
    //$("#photo").focus();
    return false;

}
于 2013-10-21T10:09:16.603 回答