使用asp.net ajax检查电子邮件的可用性,如果存在则使页面无效

时间:2010-09-24 07:49:47

标签: asp.net ajax asp.net-ajax jquery

我正在尝试执行电子邮件地址的ajax验证。我需要知道用户是否已经注册,所以我需要在我的数据库中检查它。有类似的例子:http://www.highoncoding.com/Articles/439_Performing_Instant_UserName_Availability_Check_Using_JQuery_Ajax_API.aspx

我目前的代码是:

function validateEMail(email) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "ajax/services.asmx/validateEMail",
        data: "{'email': '" + email + "'}",
        dataType: "json",
        success: function (result) {
            $("#<%=lEMail.ClientID %>").html(result.d.result);
        }
    });
}

和服务器功能:

public class EMailValidateResult
{
    public bool valid { get; set; }
    public string result { get; set; }
}

[WebMethod]
public EMailValidateResult validateEMail(string email)
{
    EMailValidateResult result = new EMailValidateResult();

    result.valid = false;
    result.result = "<img src=\"icons/accept.png\" title=\"E-Mail is valid\" />";

    return result;
}

如果电子邮件已经存在,我需要拒绝用户页面回发。

3 个答案:

答案 0 :(得分:1)

在客户端设置一个全局变量,允许表单提交基于您在ajax调用上设置的电子邮件用户标志。

var eMailExist = false;

function AllowPostBack()
{
  if(eMailExist)
    alert("This email all ready exist");

  return !eMailExist;
}

在表单标记

onsubmit="return AllowPostBack();"

在你的ajax调用中,如果存在则返回jSon

function validateEMail(email) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "ajax/services.asmx/validateEMail",
        data: "{'email': '" + email + "'}",
        dataType: "json",
        success: function (result) {
            $("#<%=lEMail.ClientID %>").html(result.d.result);
            eMailExist = result.d.valid;
        }
    });
}

在帖子后面再次检查javascript disabled的情况。

答案 1 :(得分:1)

使用customValidator:

<asp:CustomValidator ID="cvEMail" runat="server" ControlToValidate="tEMail"
Display="None" ClientValidationFunction="validateEMail"
OnServerValidate="cvEMail_ServerValidate"/>
<span id="lEMail" runat="server"></span>

客户端javascript:

<script type="text/javascript">
    function validateEMail(source, arguments) {
        var isValid;

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "ajax/services.asmx/validateEMail",
            data: "{'email': '" + arguments.Value + "'}",
            dataType: "json",
            async: false,
            success: function (result) {
                $("#<%=lEMail.ClientID %>").html(result.d.result);
                isValid = result.d.valid;
            }
        });

        arguments.IsValid = isValid;
    }
</script>

服务器端ajax功能:

[WebMethod]
public EMailValidateResult validateEMail(string email)
{
    EMailValidateResult result = new EMailValidateResult();

    result.valid = false;
    result.result = "Invalid...";

    if (Regex.IsMatch(email,@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
    {
        result.valid = true;
        result.result = "Valid...";
    }
    else if(check if exists in database)
    { invalid, exists message... }

    return result;
}

服务器端验证处理程序:

protected void cvEMail_ServerValidate(object source,ServerValidateEventArgs args)
{
    args.IsValid = false;
    lEMail.InnerHtml = "invalid...";

    if(Regex.IsMatch(args.Value,@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
    {
        args.IsValid = true;
        lEMail.InnerHtml = "valid...";
    }
    else if(check if exists in database)
    { invalid, exists message... }
}

来源:http://brian.dobberteen.com/code/jquery_ajax_custom_validator/

答案 2 :(得分:0)

我认为您需要在成功回调中将Page_IsValid设置为false