-
Notifications
You must be signed in to change notification settings - Fork 2
/
ajaxForm.min.js
11 lines (10 loc) · 1.92 KB
/
ajaxForm.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
* jQuery ajaxForm Plugin
* https://github.com/szahharenko/ajaxForm
* author: Sergei Zahharenko
* code-essence.eu
* Dual licensed under the MIT or GPL Version 2 licenses.
* https://www.opensource.org/licenses/mit-license.php
* https://www.opensource.org/licenses/GPL-2.0
**/
jQuery.fn.ajaxForm=function(e){function n(e){var t=[],n=false;e.each(function(){var e=$(this),i=e.is(":checkbox")?e.is(":checked"):true,s=e.val(),o=e.data("defval"),u=e.data("type"),a=e.data("match");if(!i||s==o||s==""){r(e);n=true}else if(u=="email"){var f=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;if(!f.test(s)){r(e);n=true}}else if(u=="numeric"){var l=/^\d+$/.test(s);if(!l){r(e);n=true}}else if(a){t.push(e)}});if(t.length>1){var i=$(t[0]),s=$(t[1]),o=i.val(),u=s.val();if(o!=u){n=true;r(i);r(s);$(i).val("").trigger("blur");$(s).val("").trigger("blur")}}return n}function r(e,t){e=$(e);t=t||"error";e.addClass(t).bind("focus.err, change.err",function(){$(this).unbind("focus.err, change.err").removeClass(t).parent().removeClass(t)}).parent().addClass(t)}var t=$(this);var i=$.extend({validate:true,fields:"input:not(:hidden):not([data-type=optional]),textarea:not(:hidden):not([data-type=optional]),select:not([data-type=optional])",onSuccess:null,onValidationError:null,onError:null,beforeSubmit:null},e);t.submit(function(e){e.preventDefault();if(t.data("processing"))return false;var r=t.find(i.fields);if(i.validate){if(n(r)){if(typeof i.onValidationError=="function")i.onValidationError();return false}}if(typeof i.beforeSubmit=="function")i.beforeSubmit();t.data("processing",true);$.ajax({type:t.attr("method")||"post",url:t.attr("action"),data:t.serialize(),success:function(e){if(typeof i.onSuccess=="function")i.onSuccess(e);t.data("processing",false)},error:function(e){if(typeof i.onError=="function")i.onError(e);t.data("processing",false)}})})}