【问题标题】:Checking email availability with VB.NET WebMethod + JSON + CustomValidator使用 VB.NET WebMethod + JSON + CustomValidator 检查电子邮件可用性
【发布时间】:2012-09-26 07:46:12
【问题描述】:

我已经阅读了几篇关于这个问题的帖子(这里和其他网站),但没有人为我工作。我正在尝试在注册过程中检查给定的电子邮件是否已在数据库中注册。

我有一个名为 Register.aspx 的页面,我在其中声明了以下 JavaScript 函数:

function check_email_availability(src, args) {
  $.ajax({
    type: "POST",
    async: false,
    timeout: 500,
    contentType: "application/json",
    url: "Register.aspx/CheckEmailAvailability",
    data: "{ 'email' : '" + args.Value + "' }",
    success: function (result) {
      args.IsValid = result.d;
    }
  });
}

另一方面,在 Register.aspx.vb 后面的代码中,我关注 WebMethod:

<System.Web.Services.WebMethod()> _
Public Shared Functiion CheckEmailAvailability(ByVal email As String) As String
  If Membership.GetUser(email) Is Nothing Then
    Return "True"
  Else
    Return "False"
End Function

我正在尝试通过 CustomValidator 启动 JavaScript 函数,但我从 Chrome 的控制台收到以下消息:

Uncaught TypeError: Cannot read property 'Value' of undefined

错误出现在该行中

data: "{ 'email' : '" + args.Value + "' }",

所以-总是使用 Chrome-,我已经在该行设置了一个断点,并且从控制台,我已经写了

args.Value

并且我获得了正确的值,即我输入的电子邮件地址。所以……我有点迷路了。

感谢您的帮助。

【问题讨论】:

  • 不要在控制台中进行评估(可能有多个上下文),而是尝试突出显示该表达式并对其进行监视。
  • 明天早上我告诉你结果。谢谢!

标签: ajax vb.net json webmethod customvalidator


【解决方案1】:

好吧,我终于成功了。我在这里分享使用的代码。进入上下文,在用户注册过程中,我想检查电子邮件的可用性。

Register.aspx的头上:

<script type="text/javascript">
  function check_email_availability(src, args) {
    var isValid;
    $.ajax({
      type: "POST",
      url: "Register.aspx/CheckEmailAvailability",
      timeout: 500,
      data: "{'email' : '" + args.Value + "'}",
      contentType: "application/json; charset=utf-8",
      async: false,
      success: function (result) {
                 isValid = result.d;
               }
    });
    args.IsValid = isValid;
  }
</script>

Register.aspx的正文中:

<asp:TextBox ID="tbEmail" runat="server" CssClass="TextBox" ClientIDMode="Static" TabIndex="1" />
<asp:CustomValidator ID="CValidatorEmail" runat="server" ControlToValidate="tbEmail" ValidateEmptyText="false" ClientValidationFunction="check_email_availability" ErrorMessage="Email already in use" SetFocusOnError="True" />

在代码隐藏中 (Register.aspx.vb):

<System.Web.Services.WebMethod()> _
Public Shared Function CheckEmailAvailability(ByVal email As String) As Boolean
    Dim oMU As MembershipUser = Membership.GetUser(email)
    If oMU IsNot Nothing Then Return False Else Return True
End Function

我希望这对某人有用。有一个 question almost identical using C# 可以帮助我使它工作。

问候,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2020-04-22
    • 2010-11-16
    • 2012-02-09
    相关资源
    最近更新 更多