Created
August 30, 2013 07:50
-
-
Save composite/6387302 to your computer and use it in GitHub Desktop.
jQuery validate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function ($) { | |
| var spes = { checkbox: 1, radio: 1 }; | |
| $.fn.validate = function (def, fns) { | |
| if (!this.is('form')) return this; | |
| var form = this.get(0); | |
| if (!arguments.length && $.isFunction(form.__VALIDFUNC)) { | |
| return form.__VALIDFUNC.call(form); | |
| } | |
| if (!$.isPlainObject(def)) return this; | |
| def = def || {}; fns = fns || {}; | |
| fns.onerror = $.isFunction(fns.onerror) ? fns.onerror : $.validate.onerror; | |
| form.__VALIDFUNC = function (e) { | |
| for (var nm in def) { | |
| var input = form.elements[nm], valids = def[nm]; | |
| if (!input || !$.isPlainObject(valids)) { continue; } | |
| for (var con in valids) { | |
| var chk = $.validate.fn[con], args = valids[con]; | |
| if (!$.isFunction(chk)) { continue; } | |
| if ($.isArray(args)) { | |
| var msg = args.pop(); | |
| if (input.length) { | |
| var valid = false; | |
| for (var i = 0, len = input.length; i < len; i++) { | |
| if (spes[input[i].type] && chk.apply(input[i], args)) { | |
| valid = true; break; | |
| } else if (!chk.apply(input[i], args)) { | |
| valid = false; break; | |
| } | |
| } | |
| if (!valid) { | |
| fns.onerror.call(input, msg || $.validate.msg); | |
| return false; | |
| } | |
| }else if (!chk.apply(input, args)) { | |
| fns.onerror.call(input, msg || $.validate.msg); | |
| return false; | |
| } | |
| } else { | |
| var msg = args; | |
| if (input.length) { | |
| var valid = false; | |
| for (var i = 0, len = input.length; i < len; i++) { | |
| if (spes[input[i].type] && chk.call(input)) { | |
| valid = true; break; | |
| } else if (!chk.call(input)) { | |
| valid = false; break; | |
| } | |
| } | |
| if (!valid) { | |
| fns.onerror.call(input, msg || $.validate.msg); | |
| return false; | |
| } | |
| } else if (!chk.call(input)) { | |
| fns.onerror.call(input, msg || $.validate.msg); | |
| return false; | |
| } | |
| } | |
| } | |
| } | |
| return true; | |
| }; | |
| form.onsubmit = function (e) { | |
| e = e || window.event; | |
| if ($.isFunction(fns.onbefore)) { fns.onbefore.call(form,e); } | |
| var result = form.__VALIDFUNC.call(form, e); | |
| if (!result) { | |
| if (e.preventDefault) e.preventDefault(); | |
| else e.cancelable = false; | |
| } | |
| if ($.isFunction(fns.onafter)) { fns.onafter.call(form, e); } | |
| return result; | |
| }; | |
| return this; | |
| }; | |
| $.validate = { | |
| fn: { | |
| required: function () { | |
| return $.trim(this.value) != '' || input.checked; | |
| }, | |
| minlength: function (len) { | |
| return this.value.length >= len; | |
| }, | |
| maxlength: function (len) { | |
| return this.value.length <= len; | |
| }, | |
| alpha: function () { | |
| return /^[a-z\s]*$/i.test(this.value); | |
| }, | |
| numeric: function () { | |
| return this.value.length == 0 || !isNaN(+this.value); | |
| }, | |
| alphanumeric: function () { | |
| return /^[\S\s]*$/.test(this.value); | |
| }, | |
| latin: function () { | |
| return /^\w*$/.test(this.value); | |
| }, | |
| password: function () { | |
| return this.value ? /[a-zA-Z]/.test(this.value)&&/[0-9]/.test(this.value) : true; | |
| }, | |
| strongPassword: function () { | |
| return this.value ? /[a-z]/.test(this.value) && /[A-Z]/.test(this.value) && /[0-9]/.test(this.value) && /[!@#$%^&*-=+~]/.test(this.value) : true; | |
| }, | |
| notspecial:function(){ | |
| return !/[~`$%\^&*-+:;'"\/?]/.test(this.value); | |
| }, | |
| email:function(){ | |
| return /^[\w-\._\+%]+@(?:[\w-]+\.)+[\w]{2,6}$/.test(this.value); | |
| }, | |
| url:function(){ | |
| return /[-a-zA-Z0-9@:%_\+.~#?&\/\/=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)?/.test(this.value); | |
| }, | |
| phone:function(){ | |
| return /(02|01[0126789]|0[3-6][1-4]|070)-[0-9]{3,4}-[0-9]{4}/.test(this.value); | |
| }, | |
| tel:function(){ | |
| return /^(\(?(\d{3})\)?\s?-?\s?(\d{3})\s?-?\s?(\d{4}))$/.test(this.value); | |
| }, | |
| equal: function (val) { | |
| return this.value == val; | |
| }, | |
| contains: function (val) { | |
| return ~this.value.indexOf(val)>0; | |
| }, | |
| first: function (val) { | |
| return this.value.indexOf(val) == 0; | |
| }, | |
| last: function (val) { | |
| return this.value.indexOf(val) == this.value.length - val.length; | |
| }, | |
| pattern: function (regex) { | |
| return (regex instanceof RegExp) ? regex.test(this.value) : new RegExp(regex).test(this.value); | |
| }, | |
| custom: function (fn) { | |
| return $.isFunction(fn) ? !!fn.call(this) : true; | |
| } | |
| }, msg: 'This fields are not valid.' | |
| ,onerror:function(msg){ | |
| alert(msg); | |
| this.focus(); | |
| } | |
| }; | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment