Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery custom validation - adding error object
    primarykey
    data
    text
    <p>Below function works perfectly fine. At present if user name is not available, I'm alerting a message "Email ID already registered - please log in". </p> <p>My query is, instead of alerting, can i add above message into the validation error object so that it is picked by default errorPlacement function (which then displays the error message on jsp - the way it normally does for other validation failuer)?</p> <pre><code> &lt;script type="text/javascript"&gt; $(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; } }); } }); &lt;/script&gt; </code></pre> <p>I think i can use addMethod() but not sure about it. One of the example i was referring to is as below ...</p> <pre><code> 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}")); </code></pre> <p>OK, I got this working using adMethod.. Code is as below.</p> <pre><code> &lt;script type="text/javascript"&gt; $(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(); } } }); </code></pre> <p>});</p> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload