The verification plug-in compiled by js seems to be complicated to use, mainly because it does not have the energy to refactor the optimization code and record the backup
/** Name: expression verification plug-in * auth: Zeng xiaobin * version: 1.0.0 * createdate: 2013-11-15 */var formValidators = []; // intercept form submission $. fn. formValidators = function (callback) {$ (this ). click (function () {var isValidator = true; for (var I in formValidators) {var $ input = formValidators [I], validator = $ input. attr ("validator"), selector = $ input. attr ("concateselector"); if (validator = "false" | typeof validator = 'undefined') {if (typeof selector! = 'Undefined') {$ (selector ). blur ();} else {$ input. blur () ;}}for (var I in formValidators) {var $ input = formValidators [I], validator = $ input. attr ("validator"), selector = $ input. attr ("concateselector"); if (validator = "false" | typeof validator = 'undefined') {if (typeof selector! = 'Undefined') {$ (selector ). focus ();} else {$ input. focus () ;}return isValidator = false ;}} if (isValidator) {callback () ;}}) ;}$. fn. formValidator = function (options) {// cache the current DOM object var $ document =$ (document), $ this = $ (this), $ target = $ (""). insertAfter ($ this), selector = $ this. selector; // temporary global variable var temp = {ajaxHandle: null}; // icon type var icon = {info: "icon_info", OK: "icon_ OK", error: "icon_error", wait: "icon_wait", none: null}; var defaults = {Show: "", focus: "", success: "", empty: "", validator: {type: "string", spinner: false, min: null, max: 1000, error: ""}, compare: {target: null, regexp: null, error: ""}, concateCompare: null, concateSelector: null, ajax: {url: null, success: "", wait: "", error: ""}, regExp: {reg: null, error: ""}, forbidKey: null}; var option = $. extend ({}, defaults, options); option. validator = $. extend ({}, defaults. validator, options. validator); option. compare = $. Extend ({}, defaults. compare, options. compare); option. ajax = $. extend ({}, defaults. ajax, options. ajax); option. regExp = $. extend ({}, defaults. regExp, options. regExp); // cache the original value of the ajax control if (typeof options. ajax! = "Undefined") {$ this. attr ("oldValue", $ this. val () ;}// control of cache delegate if (option. concateSelector! = Null) {selector = option. concateSelector; $ this. attr ("concateSelector", selector);} // cache it to the array formValidators. push ($ this); // service layer var service ={// encapsulate html text html: function (ico, text, $ that) {var html_text = ""; if (ico! = Null) {html_text = ''+ text +'';} $ that | $ target.html (html_text) ;}, // perform verification validator: function () {var value = $ this. val (), oldvalue = $ this. attr ("oldvalue"); len = value. length; $ this. removeClass ("input_success "). removeClass ("input_wait "). addClass ("input_error "). attr ("validator", false); // if (option. validator. type = "string" & option. validator. min! = Null & len = 0) {return this.html (icon. error, option. empty);} // verify the regular expression else if (option. validator. type = "string" & option. regExp. reg! = Null &&! Option. regExp. reg. test (value) {return this.html (icon. error, option. regExp. error);} // if this field is optional, else if (option. validator. type = "string" & option. validator. min = null & len = 0) {$ this. removeClass ("input_error "). attr ("validator", true); return this.html (icon. none, null);} // verify the maximum and minimum length of else if (option. validator. type = "string" & (option. validator. min! = Null & (len
Option. validator. max) {return this.html (icon. error, option. validator. error);} else if (option. validator. type = "number "&&(! $. IsNumeric (value) | stringUtil. isNull (value) | value
Option. validator. max) {return this.html (icon. error, option. validator. error);} else if(stringUtil.isNotNull(option.compare.tar get) & option. compare. regexp = "=" & amp; # get (option.compare.tar get ). val ()! = Value) {return this.html (icon. error, option. compare. error);} else if (stringUtil. isNotNull (option. ajax. url) & stringUtil. isNotNull (value) & oldvalue! = Value) {$ this. removeClass ("input_error "). addClass ("input_wait"); // cancel the AJAX request temp. ajaxHandle & temp. ajaxHandle. abort (); var ajaxPara ={}, ajaxUrl = ""; var startIndex = option. ajax. url. lastIndexOf ("&"), endIndex = option. ajax. url. lastIndexOf ("="); if (endIndex> startIndex & startIndex! =-1) {ajaxPara [option. ajax. url. substring (startIndex + 1, endIndex)] = value; ajaxUrl = option. ajax. url. substr (0, startIndex);} temp. ajaxHandle = $. post ("manage/zui_servlet? Sid = "+ ajaxUrl, ajaxPara, function (data) {data = JSON. parse (data); if (typeof data = "object") {if (data. recode = 1 &&! Data. data) {$ this. removeClass ("input_wait "). addClass ("input_success "). attr ("validator", true); return service.html (icon. OK, option. ajax. success);} else {$ this. removeClass ("input_wait "). addClass ("input_error"); return service.html (icon. error, option. ajax. error) ;}} else {$ this. removeClass ("input_wait "). addClass ("input_error"); return service.html (icon. error, "Verification Failed, server error! ") ;}}); Return this.html (icon. wait, option. ajax. wait);} else {if (stringUtil. isNotNull (option. concateCompare) & stringUtil. isNotNull ($ (option. concateCompare ). val () {$ (option. concateCompare ). blur () ;}$ this. removeClass ("input_error "). addClass ("input_success "). attr ("validator", true); return this.html (icon. OK, option. success) ;};}, // Number Automatic-1 spinnerUp: function () {var val = $ this. val (); if (! $. IsNumeric (val) {val = 0; $ this. val (val); service. validator () ;}$ this. val (val
Option. validator. min? -- Val: 0) ;}, // start initialization start: function () {var that = this; // obtain the focus event $ document. on ("focus", selector, function () {$ this. removeClass ("input_success "). removeClass ("input_wait "). removeClass ("input_error" commandid if(stringutil.isnotnull(option.focus?##that.html (icon.info, option. focus) ;}}); // triggers the event $ document when the focus is lost. on ("blur", selector, function () {that. validator () ;}); // if the if (stringUtil. isNotNull (option. forbidKey) {$ document. on ("Keydown", selector, function (e) {var key = window. event? E. keyCode: e. which; if (option. forbidKey. test (key) {e. returnValue = false; e. preventDefault (); return false ;}}) ;}// if it is a number type, convert it to a number control-only numbers can be entered if (option. validator. type = "number") {$ document. on ("keydown", selector, function (e) {var key = window. event? E. keyCode: e. which; // large keyboard, keypad, return, tabulation, direction, delete, home, end, F5, enter key if (e. ctrlKey & (key = 65 | key = 67 | key = 86 | key = 88) | (key> 95 & key <106) | (key> 47 & key <60) |/^ [89] $ | ^ 3 [5679] $ | ^ 46 $ | ^ 116 $ | ^ 13 $ /. test (key) {return true;} else if (option. validator. spinner & key = 38) {that. spinnerUp ();} else if (option. validator. spinner & key = 40) {that. spinnerDown ();} else {e. returnValue = false; e. preventDefault (); return false ;}}); if (option. validator. spinner) {$ this. wrap (""); $ ('
'). InsertAfter ($ this). on ("click", that. spinnerUp); $ ('
'). InsertAfter ($ this ). on ("click", that. spinnerDown);} $ this. attr ("maxlength", option. validator. max. toString (). length);} else {$ this. attr ("maxlength", option. validator. max) ;}}; service. start () ;}; // tool class var stringUtil = {isNull: function (val) {return val = undefined | val = "null" | val = null | val = "" ;}, isNotNull: function (val) {return! StringUtil. isNull (val );}};
Use DEMO
// Click the Save button $ ("# bt_save "). formValidators (addSchool); $ ("# input_groupName "). formValidator ({focus: "Enter your school name", empty: "You have not entered the school name", validator: {min: 0, max: 60}, ajax: {url: "isExistGroupName & groupName =", wait: "checking whether the school name exists", error: "error! The school name already exists "}); $ (" # input_groupType "). formValidator ({focus: "select the school type", empty: "You have not selected the school type", validator: {min: 0, max: 2 }});
// Click the Save button $ ("# bt_save "). formValidators (addTeacher); $ ("# input_nickname "). formValidator ({focus: "4-20 digits, the user name cannot be modified after registration", empty: "You have not entered the user name", validator: {min: 4, max: 20, error: "The username length is incorrect. It should be 4-20 characters long! "}, RegExp: {reg:/^ [a-zA-Z]/, error:" The username format is incorrect. It can only start with a letter "}, ajax: {url: "isExistPhoneOrEmail & nickname =", success: "", wait: "checking whether the user name is available", error: "The user name has been used! "}, ForbidKey:/^ 32 $/}); $ (" # input_password "). formValidator ({focus: "The password must consist of 6-16 digits", empty: "You have not entered the password", validator: {min: 6, max: 16, error: "The password is too short. The minimum password is 6 characters. "}, ConcateCompare:" # input_password_retry "}); $ (" # input_password_retry "). formValidator ({focus: "enter the password again", empty: "You have not entered the password", validator: {min: 6, max: 16, error: "confirm that the password is too short. The minimum password is 6 characters. "}, Compare: {target:" # input_password ", regexp:" = ", error:" inconsistent passwords, please confirm again "}}); $ ("# input_username "). formValidator ({focus: "Please enter your real name", empty: "You have not entered your real name", validator: {min: 0, max: 20 }}); $ ("# input_email "). formValidator ({focus: "Enter your personal email address", empty: "You have not entered your personal email address", validator: {min: 0, max: 40}, ajax: {url: "isExistPhoneOrEmail & email =", success: "", wait: "checking whether the mailbox is available", error: "The mailbox has been bound by another user name. Please change the mailbox! "}); $ (" # Input_phone "). formValidator ({focus: "Enter your mobile phone number. You can leave this field blank.", validator: {max: 20}, ajax: {url: "isExistPhoneOrEmail & phone =", success: ", you can use", wait: "checking whether the mobile phone number has been bound", error: "the mobile phone number has been bound by another user name. Please change the mobile phone number! "}); $ (" # Input_gradeCourseId "). formValidator ({focus: "Please select teaching information", empty: "You have not selected teaching information", validator: {min: 0}, concateSelector: "# gradeId, # courseId "});