繁体   English   中英

MVC3中的jQuery Mobile自定义验证

[英]jQuery mobile custom validation in mvc3

要进行表单验证以检查电子邮件是否存在,我需要自定义验证,我尝试过钱,但没有成功,总是返回电子邮件已存在,我粘贴代码,请确认我的错误

视图中的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});
        }

提前致谢

由于您的ajax请求是异步的,因此在ajax请求完成之前返回函数时,验证器将始终未定义(或false)响应。

在函数返回之前,您将需要使您的请求同步以设置响应。 您可以通过在ajax请求中添加'async:false'参数来完成此操作

async: false

编辑:

您的代码还有其他问题。 您需要添加一种数据类型以告诉JSON您期待JSON响应。 另外,成功响应中的msg变量需要一个字符串,但这是不正确的,因为JSON对象的第一个属性是success: 当您将$.ajax与dataType'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();
            }
        });


    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM