使用JavaScript和Asp.net方法检查电子邮件是否已经存在

ohtdti5x  于 5个月前  发布在  .NET
关注(0)|答案(3)|浏览(49)

我检查,如果电子邮件存在或不使用JavaScript,但它是发射“电子邮件已经存在”每次即使电子邮件不存在.代码如下:

JavaScript代码:

function chkemailExist(source, args) {
        var exists;
        $.ajax({
            type: "POST",
            url: "Register.aspx/DoesUserExist",
            data: "{'emailid': '" + args.Value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (result) {
                exists = result.d;

            }
        });
        args.IsValid = exists;
        if (!exists) {
            var message="Email already exist for" + args.Value + ". ";
            alert(message);
        }
        else { document.getElementById('ContentPlaceHolder1_CustomValidator1').innerHTML = ""; }
    }

字符串

Web方式:

[WebMethod]
   public static bool DoesUserExist(string emailid)

{
    Boolean flg = true;
    //Int32 cntchk = 0;
    PAL_Register reg = new PAL_Register();
    BAL_Register bal_reg = new BAL_Register();
    reg.UserName = emailid;
    flg = bal_reg.checkusername(reg);
    return flg;
}


请帮助我得到正确的代码,所以它火警报存在的电子邮件,只有当电子邮件已经存在于实际。

ergxz8rk

ergxz8rk1#

AJAX是异步的-你试图对一个还不存在的变量执行逻辑操作,因为调用正在进行中。在回调中执行你的工作吧!

function chkemailExist(source, args) {
    var exists;
    $.ajax({
        type: "POST",
        url: "Register.aspx/DoesUserExist",
        data: "{'emailid': '" + args.Value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (result) {
            exists = result.d;
            args.IsValid = exists;
            if (!exists) {
                var message="Although you indicated you're a new customer, an account already exists for " + args.Value + ". ";
                alert(message);
            }
            else { 
                document.getElementById('ContentPlaceHolder1_CustomValidator1').innerHTML = "";
            }
    });
}

字符串

piv4azn7

piv4azn72#

注解可能是真的,但我觉得还有另一个问题:在JavaScript中,一个非空的字符串总是计算为true,并且由于来自服务器的响应不是键入的(如果我没有弄错的话),它总是计算为true是字符串“true”还是“false”.你应该测试字符串值或返回false的空字符串。
cfr this fiddle

xzlaal3s

xzlaal3s3#

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Registration Form</title>
</head>
<body>
    <form id="registrationForm">
        <label for="email">Email:</label>
        <input type="email" id="email" required>
        <button type="button" onclick="checkEmail()">Submit</button>
    </form>

    <script>
  // Check if the email already exists in local storage
function isEmailExists(email) {
    let storedEmails = JSON.parse(localStorage.getItem('registeredEmails')) || [];
    return storedEmails.includes(email);
}

// Save the email to local storage
function saveEmail(email) {
    let storedEmails = JSON.parse(localStorage.getItem('registeredEmails')) || [];
    storedEmails.push(email);
    localStorage.setItem('registeredEmails', JSON.stringify(storedEmails));
}

// Validate the email and take appropriate action
function checkEmail() {
    let emailInput = document.getElementById('email');
    let email = emailInput.value;

    if (isEmailExists(email)) {
        alert('Email already registered!');
    } else {
        saveEmail(email);
        alert('Email registered successfully!');
        // You can add additional actions here, such as submitting the form or redirecting to another page.
    }
}
</script>
</body>
</html>

字符串

相关问题