Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JQuery验证程序插件Asp.Net调用服务器方法_C#_Jquery_Asp.net 3.5_Jquery Validate - Fatal编程技术网

C# JQuery验证程序插件Asp.Net调用服务器方法

C# JQuery验证程序插件Asp.Net调用服务器方法,c#,jquery,asp.net-3.5,jquery-validate,C#,Jquery,Asp.net 3.5,Jquery Validate,我使用JQuery验证插件验证表单() 例如,我可以在表单中验证电子邮件 我想做的是验证所选用户名是否已经存在。因此,我需要调用一个查询数据库的服务器方法。我该怎么做 更新 从balexandre的回答中,我尝试以下方法: $.validator.addMethod("userName", function(value, element) { $.get ( "CheckUser.aspx?User=" + value,

我使用JQuery验证插件验证表单()

例如,我可以在表单中验证电子邮件

我想做的是验证所选用户名是否已经存在。因此,我需要调用一个查询数据库的服务器方法。我该怎么做

更新 从balexandre的回答中,我尝试以下方法:

$.validator.addMethod("userName", 
    function(value, element)
    {
        $.get
        (
            "CheckUser.aspx?User=" + value, 
            function(data) 
            {
                if (data == "True") 
                {
                    return false;
                }
                else if (data == "False") 
                {
                    return true;
                }
                else 
                {
                    alert(data);
                }
            }
        );
    },
    "User already exists");

    $(document).ready
    (
        function() 
        {
            $("#aspnetForm").validate
            (
                {
                    rules: 
                    {
                        <%=txtUserName.UniqueID %>: 
                        {
                            required: true,
                            email: true,
                            userName: true                        
                        }
                    }
                }
            );
        }
    );
$.validator.addMethod(“用户名”,
函数(值、元素)
{
美元
(
“CheckUser.aspx?User=“+值,
功能(数据)
{
如果(数据==“真”)
{
返回false;
}
否则如果(数据==“假”)
{
返回true;
}
其他的
{
警报(数据);
}
}
);
},
“用户已存在”);
$(文件)。准备好了吗
(
函数()
{
$(“#aspnetForm”).validate
(
{
规则:
{
: 
{
要求:正确,
电子邮件:是的,
用户名:true
}
}
}
);
}
);
在响应中,我编写了一个布尔值(True of False),而不是1 ou 0


我知道我的验证函数是call。问题是即使用户不存在,它也总是显示错误消息。我试着在返回true之前放置一条警告消息,以查看它是否到达,并且它是。所以我不明白,为什么它不起作用…

这应该可以解决问题

或者

一个答案显示如何调用服务&第二个答案显示如何调用页面方法


非常简单,我在项目中应用了很多,这是一个很好的方法

1-使用代码隐藏文件创建名为verifyUserAvailability.aspx的aspx页面

2-删除aspx文件中除第一行以外的所有行

3-在页面加载事件中的代码隐藏文件中

protected void Page_Load(object sender, EventArgs e) {
    string username = Request["usr"];
    int r = 0;

    if(!String.IsNullOrEmpty(username))
        if( yourDAL.getUserAvailability(username) )
            // your method would send true if user is available to be used
            r = 1;

   Response.Clear();
   Response.Write(r);
   Response.Flush();
}
4-现在在表单页面中,创建一个类似

<input id="btnCheckAvailability" type="button" 
       value="Check availability" 
       onclick="javascript:checkAvailability();" />

5-创建checkAvailability()方法


功能检查可用性(){
var$user=$(“#UsernameInputBox”);
$.get(“verifyUserAvailability.aspx?usr=“+$user.val(),函数(数据)){
//这将调用传递用户名的页面并返回1或0
如果(数据==“1”)
$user.css(“背景色”、“绿色”);
其他的
$user.css(“背景色”、“红色”);
});
}

我认为这是类型和大小写的问题,因此请尝试使用1和0!
<script type="text/language">

    function checkAvailability() {
        var $user = $("#UsernameInputBox");
        $.get("verifyUserAvailability.aspx?usr=" + $user.val(), function(data) {
            // this will call the page passing the username and return 1 or 0
            if( data == "1" ) 
                $user.css("background-color", "green");
            else 
                $user.css("background-color", "red");
        });
    }

</script>