0

これで壁に頭をぶつけているので、助けてくれてありがとう。メールが現在メンバーシップテーブルにない/存在しない場合にtrue/falseを返すjavascript関数を指すように作成した検証メソッドがいくつかあります。javascript関数(CheckEmail)はうまく機能し、コントローラーアクションは正常に機能しますが、CheckEmailによって返されるtrueまたはfalseに関係なく、常に無効なフィールドと評価されます。助けてください:

検証

 $.validator.addMethod("emailCheck", function (value, element) {
                return this.optional(element) || CheckEmail(value);
            }, "This email is already registered");

            Validator = $("#RegForm").validate({
                rules: {
                    Email: {
                        required: true,
                        email: true,
                        emailCheck: true
                    },
                    Password: {
                        required: true,
                        minlength: 6
                    },
                    ConfirmPassword: {
                        required: true,
                        equalTo: "#Password"
                    },
                    "UserObj.FirstName": {
                        required: true,
                        minlength: 2
                    },
                    "UserObj.LastName": {
                        required: true,
                        minlength: 2
                    }
                }

            });

javascriptメソッド:

function CheckEmail(email) {
    $.getJSON("/account/jsonCheckEmail", { EmailToCheck: email }, function (data) {
        if (data.status = "OK") {
            if (data.msg = "True") {
                return true;
            }
            else {
                return false;
            }
        }
    });

}

コントローラのアクション:

public JsonNetResult jsonCheckEmail(string EmailToCheck)
{
    JsonNetResult jsonEmail = new JsonNetResult();
    jsonEmail.Formatting = Formatting.Indented;
    try
    {

        string user = Membership.GetUserNameByEmail(EmailToCheck);

        if (string.IsNullOrWhiteSpace(user))
        {
            jsonEmail.Data = new jsonResponseObj("True", "True");
        }
        else {
            jsonEmail.Data = new jsonResponseObj("False","False");
        }

    }
    catch (Exception ex)
    {
        jsonEmail.Data = new jsonResponseObj(ex);
        //jsonEmail.Data = new jsonResponseObj("False", "False");
    }

    return jsonEmail;
}

HTML:

               <tr>
                    <td>@Html.LabelFor(m => m.Email)
                    </td>
                    <td>@Html.TextBoxFor(m => m.Email)
                        @Html.ValidationMessageFor(m => m.Email)
                    </td>
                </tr>
4

1 に答える 1

1

独自のjQueryハンドラーを作成するのではなく、プラグインのリモート機能を活用できます。

Email: {
     required: true,
     email: true,
     remote: "/account/jsonCheckEmail"
},
于 2012-09-27T03:42:47.243 に答える