1

下面的功能工作得很好。目前,如果用户名不可用,我会提示一条消息“电子邮件 ID 已注册 - 请登录”。

我的查询是,而不是警报,我是否可以将上述消息添加到验证错误对象中,以便默认 errorPlacement 函数选择它(然后在 jsp 上显示错误消息 - 它通常对其他验证失败者执行的方式)?

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

         var formAction = '';
         var userAvailable = '';

         $("#myForm").validate({

             onfocusout:false,
             onkeyup:false,
                 onclick:false,

             rules:{                            
                 j_username:{
                     required: true,
                     email: true,
                     minlength: 8                           
                 }                          
             },
             messages: {                            
                 j_username:{           
                     required: "Please enter a valid email address", 
                     minlength: "Please enter a valid email address"
                 }
             }, 
             // the errorPlacement has to take the table layout into account 
             errorPlacement: function(error, element) {
                 if(element.is(":radio") )
                     error.appendTo( element.parent().next());          
                 else
                     error.appendTo( element.parent());
             }, 
             submitHandler: function(form){

                 if($('#accountNo').is(':checked')){
                     var emailId = $('#userName').val();                
                     isUserNameAvailable(emailId);
                     if(userAvailable=="1"){
                         alert("Email ID already registered - please log in");
                         return false;                  
                     }
                     var loginAction = '/MyApp/articles/add.html';
                     $('#myForm').attr('method','POST');
                     $('#myForm').attr('action',loginAction);
                     form.submit();
                 }
                 else{
                     var loginAction = '/MyApp/j_spring_security_check';    
                     $('#myForm').attr('method','POST');
                     $('#myForm').attr('action',loginAction);
                     form.submit();
                 }         
             }
         });

         //Ajax call to check userName availability returns '1' if userName is available
         function isUserNameAvailable(id) {
           $.ajax({
               url: "isUserAvailable.html",
               data: ({emailId : id}),
               async: false,
               success: function(data) { 
                   userAvailable = data;             
               }          
           });       
         }  
     });

     </script>

我想我可以使用 addMethod() 但不确定。我所指的示例之一如下...

     jQuery.validator.addMethod("math", function(value, element, params) { 
      return this.optional(element) || value == params[0] + params[1]; 
     }, jQuery.format("Please enter the correct value for {0} + {1}"));

好的,我使用 adMethod 完成了这项工作。代码如下。

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

var formAction = '';
var response;

$.validator.addMethod("uniqueUserName", function(value, element) {
    var id = $('#userName').val();
    if($('#accountNo').is(':checked')){
      $.ajax({
          url: "isUserAvailable.html",
          data: ({emailId : id}),
          async: false,           
           success: function(msg)
           {
              //If username exists, set response to true
              response = ( msg == '1' ) ? true : false;
           }
        });
   return response;
    }
    else{
        return true;
    }

}, "Username is Already Taken");


$("#myForm").validate({

    onfocusout:false,
    onkeyup:false,
    onclick:false,

    rules:{                         
        j_username:{
            required: true,
            email: true,
            minlength: 8,
            uniqueUserName: true
        }                           
    },
    messages: {                             
        j_username:{            
            required: "Please enter a valid email address", 
            minlength: "Please enter a valid email address",
            uniqueUserName: "Email ID already registered - please log in"
        }
    }, 
    // the errorPlacement has to take the table layout into account 
    errorPlacement: function(error, element) {
        if(element.is(":radio") )
            error.appendTo( element.parent().next());           
        else
            error.appendTo( element.parent());
    }, 
    submitHandler: function(form){

        if($('#accountNo').is(':checked')){             
            var loginAction = '/MyApp/articles/add.html';
            $('#myForm').attr('method','POST');
            $('#myForm').attr('action',loginAction);
            form.submit();
        }
        else{
            var loginAction = '/MyApp/j_spring_security_check'; 
            $('#myForm').attr('method','POST');
            $('#myForm').attr('action',loginAction);
            form.submit();
        }          
    }
});

});

4

0 回答 0