溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ajax如何實現(xiàn)郵箱、用戶名唯一性驗證

發(fā)布時間:2021-07-20 14:33:12 來源:億速云 閱讀:104 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細講解有關(guān)Ajax如何實現(xiàn)郵箱、用戶名唯一性驗證,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體代碼如下所示:

 <script type="text/javascript">
    $(function () { 
      $("#txtEmail").blur(function () {
        $.ajax({
          type: "post",
          url: "reg.ashx?email=" + $.trim($("#txtEmail").val()) + "&d=" + (+new Date()),
          success: function (data) {
            var vCount = parseInt(data);
            if (vCount == 0) {
              alert("郵箱可以使用");
            }
            else {
              alert("郵箱已經(jīng)被占用");
            }
          }
        });
      });
      $("#checkpwd").blur(function () {
        return CheckPwd();
      });
    });
    function CheckPwd()
    {
      var bCheck = true;
      if ($.trim($("#pwd").val()) != $.trim($("#checkpwd").val()))
      {
        alert("兩次密碼輸入不一致");
        bCheck = false;
      }
      return bCheck;
    }
  </script>

reg.ashx代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebT1.Ti.html2
{
  /// <summary>
  /// reg 的摘要說明
  /// </summary>
  public class reg : IHttpHandler
  {
    public void ProcessRequest(HttpContext context)
    {
      if (context.Request["email"] != null)
      {
        string strEmail = context.Request["email"];
        List<UserModel> lstUser = DataService.GetUserList();
        var v = lstUser.Where(p => p.Email == strEmail);
        int iCount = 0;
        if (v.Count() > 0)
        {
          iCount = 1;
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write(iCount.ToString());
      }
    }
    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
  public class DataService
  {
    /// <summary>
    /// 模擬已注冊用戶數(shù)據(jù)
    /// </summary>
    public static List<UserModel> GetUserList()
    {
      var list = new List<UserModel>();
      list.Add(new UserModel() { Email = "t1@demo.com" });
      list.Add(new UserModel() { Email = "t2@demo.com" });
      list.Add(new UserModel() { Email = "t3@demo.com" });
      list.Add(new UserModel() { Email = "t4@demo.com" });
      list.Add(new UserModel() { Email = "t5@demo.com" });
      return list;
    }
  }
  public class UserModel
  {
    public string Email { get; set; }
  }
}

關(guān)于“Ajax如何實現(xiàn)郵箱、用戶名唯一性驗證”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI