0

メールの存在を確認するためのフォーム検証には、カスタム検証が必要です。お金を試しましたが、機能しませんでした。常にメールが返されます。コードを貼り付けてエラーを確認してください。

ビュー内のjQueryコード

<script type="text/javascript">
        $(document).ready(function () {

             var response;
             //<!-- add vlidator -->
             $.validator.addMethod(
             "unique_email",function(value,element){
                $.ajax({
                url:'SignUp/emailExist',
                type: 'POST',
                data: 'email='+value,
                contentType: 'application/json; charset=utf-8',
                success:function(msg){
                    response = ( msg == 'true' ) ? true : false;
                }              
                })
                return response;
             },"email already exist"
             );
            jQuery.validator.classRuleSettings.unique_email = { unique_email: true };
            $("#register-form").validate({

                submitHandler: function (form) {
                    $('#register-form').submit();
                }
            });


        });
    </script>

jQueryモバイルコード

<div data-role="fieldcontain">
                <label for="email">
                <em>* </em> Email: </label>
               <label> <input type="text" id="email" 
                    name="email" class="required email unique_email" /></label>                  
            </div>

そして最後に、ajaxを介して使用される動作中のコード

[HttpPost]
        public ActionResult emailExist(string email)
        {
            //here i m checking from db that email exist or not result will return 1 
            //if an email exist and 0 if not so i m return false if i found email that is on 1
            int value = su.isEmailExist(email);
            if (value == 1)
                return Json(new { success = false});
            else
                return Json(new { success = true});
        }

前もって感謝します

4

1 に答える 1

1

ajaxリクエストは非同期であるため、ajaxリクエストが終了する前に関数が返されるため、バリデーターは常に未定義(またはfalse)の応答を返します。

関数が戻る前に、応答を設定するために要求を同期させる必要があります。これを行うには、ajaxリクエストに「async:false」パラメーターを追加します

async: false

編集:

コードには他にも問題があります。JSON応答を期待していることをJSONに伝えるために、データ型を追加する必要があります。また、msg成功応答の変数は文字列を期待していましたが、JSONオブジェクトの最初のプロパティがであるため、これは正しくありませんでしたsuccess:$.ajaxdataType'json'で使用しているので、データをJSON文字列として渡す必要があります。次のJavaScriptコードをテストしましたが、機能しているようです。

    $(document).ready(function () {

         var response;
         //<!-- add vlidator -->
         $.validator.addMethod(
             "unique_email",function(value,element){
                $.ajax({
                url:'SignUp/emailExist',
                type: 'POST',
                data: "{ email: '" + value + "' }",
                contentType: 'application/json; charset=utf-8',
                success:function(json){
                    response = json.success;
                },
                async: false,
                dataType: 'json'          
                });
                return response;
             },"email already exist"
         );
        jQuery.validator.classRuleSettings.unique_email = { unique_email: true };
        $("#register-form").validate({

            submitHandler: function (form) {
                form.submit();
            }
        });


    });
于 2012-07-08T15:43:41.243 に答える