var token = $("meta[name='_csrf']").attr("content"); var header = $("meta[name='_csrf_header']").attr("content"); var AddNewUser = function () { var handleAddCuratedUser = function() { jQuery.validator.addMethod("laxEmail", function(value, element) { // allow any non-whitespace characters as the host part return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@(?:\S{1,63})$/.test( value ); }, 'Please enter a valid email address.'); jQuery.validator.addMethod("userRoleCheck", function(value, element) { return value!=0; }, 'Please select a role of user.'); $('.add-new-user-form-class').validate({ errorElement: 'span', //default input error message container errorClass: 'help-block', // default input error message class focusInvalid: false, // do not focus the last invalid input rules : { email :{ required : true, maxlength : 50, minlength : 4, remote: { url: contextPath+"check-email-for-reset-password", type: "post", beforeSend: function(xhr) { xhr.setRequestHeader(header, token); } }, laxEmail : true } }, messages: { email:{ required: "Email is Required.", minlength:"Minimum Length is of 4 chracters.", remote: "This Email Not Exist in Our Database.Please try different !" } }, invalidHandler: function (event, validator) { //display error alert on form submit $('.alert-danger', $('.add-new-user-form-class')).show(); }, highlight: function (element) { // hightlight error inputs $(element) .closest('.form-group').addClass('has-error'); // set error class to the control group }, success: function (label) { label.closest('.form-group').removeClass('has-error'); label.remove(); }, errorPlacement: function (error, element) { error.insertAfter(element.closest('.input-field')); }, submitHandler: function (form) { form.submit(); // form validation success, call ajax form submit } }); $('.add-new-user-form-class input').keypress(function (e) { if (e.which == 13) { if ($('.add-new-user-form-class').validate().form()) { $('.add-new-user-form-class').submit(); //form validation success, call ajax form submit } return false; } }); } return { //main function to initiate the module init: function () { handleAddCuratedUser(); } }; }();