检查部门名称时出现jquery验证错误

时间:2013-09-25 06:53:38

标签: javascript php jquery

我已经使用jquery验证来检查特定组织中是否已存在部门名称。但它没有返回正确的结果。请有人帮助我。

这是我的Jquery:

$(document).ready(function(){
var validator = $("#frmAddDepartment").validate({
    errorElement:'div',
    rules: {
        organization:{
            required:true
        },
        department: {
            required: true,
            allowChars:true,
            //remote:$('#site_url').val()+"admin/department/check_department/"+$('#department').val()+"/"+$('#Organization').val(),
            checkdeptname:true
        }
    },
    messages: {
        organization:{
            required:languageArray['select_organization']
        },
        department:{
          required:languageArray['enter_department'],
          //remote:jQuery.format(languageArray['dept_exist'])
      }         
    }
});

jQuery.validator.addMethod("allowChars", function(value, element) { 

    var filter = new RegExp(/[^a-zA-Z0-9\-& ]/);

        if(!(filter.test(value)))
        {
            return true;
        }else

            return false;
        },languageArray['please_enter_valid_chars']);

jQuery.validator.addMethod("checkdeptname", function(value, element) {  

    returnResult = false;

    $.ajax({
        url: $('#site_url').val()+"admin/department/check_department",
        type: "GET",
        data:{department:value,organization:$('#Organization').val()},
        success: function (result)
        {
            returnResult = result;
            alert(returnResult);
        }
    });
    //$.get( $('#site_url').val()+"admin/department/check_department", { department:value,organization:$('#Organization').val() }, function(data){alert(data);returnResult = result;} );
    return returnResult;
        },languageArray['dept_exist']);});

PHP控制器的功能:

public function check_department()
{
    $sql_query=$this->department_model->check_department();

    if($sql_query>0)
    {
        echo "false";
    }else
    {
        echo "true";
    }
}

1 个答案:

答案 0 :(得分:0)

我认为,在你的php控制器函数中,你将返回true / false作为String,并且在ajax成功回调函数中,你将它与布尔值true / false进行比较。