/** * power by wooshoo copyright 2008-2009 * Program name: JQuery dedicated input checker * Content: Specifically checked for input[text password hidden] and textarea user input * checked The range includes: number of characters, whether it contains special characters, whether it is an integer, whether it conforms to email format, whether it is a phone number, whether * is a website address, whether it is an image address, whether it is a floating point decimal, whether it is RMB currency, Whether it is in date format * whether it is in time format, whether it is in date and time format, whether it is in password format (containing only uppercase and lowercase letters, numbers and underlines), * Not available yet: whether it is non-English numbers Special character format (that is, only large character languages such as Chinese, Japanese, Korean, etc.), whether it contains html tags, whether it contains UBB tags, * */ (function($) { var wshc = $.fn.check = function(){ return wshc.fn.init(this ); } DATETIME = { FULL: "full", SIMPLE: "simple", ENGLISH: "english", JAPANESE: "japanese", CHINESE: "chinese" } wshc.fn = { init: function(obj){ wshc.fn.val = obj.val(); return wshc.fn ; }, notNull: function(){ if(this.val !== "" || this.val !== undefined){ return true; } error("You did not enter any characters."); return false; }, number: function(min,max){//Check whether the number of words exceeds the limit if(this .val.length >= min && this.val.length <= max){ return true; } error("The characters you entered exceed " min "-" max " Limit. "); return false; }, specialChar: function(pat){//Check whether it contains special characters //Special characters include: / @ # $ % ^ & * = < > n r //If you need to customize it, you can set it in the parameters pat = pat || /[\/@#$%^&*=<>nr] /; error("The characters you entered contain some special characters."); return mat(this.val,pat); }, isNum: function(len){//check Is it a number? if(!isNaN(this.val)){ return true; } error("What you entered is not a number."); return false; }, integer: function(){//Check whether it is an integer if(this.val == parseInt(this.val)){ return true; } error ("The input you entered is not an integer. "); return false; }, float: function(){//Check whether it is a decimal if(this.isNum() && !this.integer()){ return true; } error("What you entered is not a decimal."); return false; }, rmb: function(){//Check whether it is currency ( The standard format of RMB is: 0.00 or 10.00 (the first digit is not zero except single digits) var pat = /^([1-9][0-9] |[0-9]).[0-9] {2}$/; error("The format you entered is not RMB currency."); return mat(this.val,pat); }, email: function(pat) {//Check whether it matches the email format pat = pat || /^[a-zA-Z0-9_-.] @[a-zA-Z0-9_-.] .[a-zA-Z] {0,4}$/; error("What you entered is not in email format."); return mat(this.val,pat); }, http: function( pat){//Check whether it is the address of the website (including http) pat = pat || /^(http|HTTP)://[^s]*/; error("What you entered is not Universal URL format. "); return mat(this.val,pat); }, url: function(pat){//Check whether it is a communication address pat = pat || /^[a-zA-z] ://[^s]*/; error("What you entered is not a universal communication protocol format."); return mat(this.val,pat); }, image: function(pat){//Check whether it is an image address (jpg gif png bmp jpeg) pat = pat || /^(http|HTTP)://[^s ]*(jpg|JPG|png|PNG|gif|GIF|bmp|BMP|jpeg|JPEG)$/; error("The image format you entered is not allowed by the webpage."); return mat (this.val,pat); }, password: function(pat){ pat = pat || /^w*$/; error("The password you entered is not in the format . "); return mat(this.val,pat); }, tel: function(pat){ pat = pat || /^d{3}-d{8} $|^d{4}-d{7}$/; error("The format you entered is not a landline phone number in China."); return mat(this.val,pat); }, mobile: function(pat){ pat = pat || /^1d{10}$/; error("The format you entered is not a mobile phone number in China."); return mat(this.val,pat); }, datatime: function(){ return DATETIME; }, date: function(type){ var pat; switch(type){ case DATETIME.FULL: pat = /^(([1-9]d{0,3}|0)-d{2}-d {2})|(([1-9]d{0,3}|0).d{2}.d{2})|(([1-9]d{0,3}|0)/ d{2}/d{2})$/; break; case DATETIME.SIMPLE: pat = /^(d{2}-d{1,2}-d{1, 2})|(d{2}.d{1,2}.d{1,2})|(d{2}/d{1,2}/d{1,2})$/; break; case DATETIME.ENGLISH: pat = /^w* d{1,2},(([1-9]d{0,3}|0)| ([1-9] d{0,3}|0))$/; break; case DATETIME.JAPANESE: pat = /^(([1-9]d{0,3}|0)year d{2}month d{2}day)$/; break; case DATETIME.CHINESE: pat = /^(([1-9]d{0,3}|0) Year d{2} month d{2} day)$/; break; } error("The date format you entered is incorrect."); return mat(this.val,pat); }, time: function(type){ var pat; switch(type){ case DATETIME .FULL: pat = /^d{2}:d{2}:d{2}$/; break; case DATETIME.SIMPLE: pat = /^d{1 ,2}:d{1,2}:d{1,2}$/; break; case DATETIME.ENGLISH: pat = /^d{1,2}:d{1 ,2}:d{1,2}$/; break; case DATETIME.JAPANESE: pat = /^d{1,2} hour d{1,2} minute d{1 ,2} seconds $/; break; case DATETIME.CHINESE: pat = /^d{1,2} hours d{1,2} minutes d{1,2} seconds $/ ; break; } error("The time format you entered is incorrect."); return mat(this.val,pat); } } var mat = function(val,pat){ if (val.match(pat)) { return true; } return false; } var error = function(err){ wshc.fn.error = err || "Unformatted error. "; } })(jQuery); Usage: The premise is that you have imported the jQuery3.2 package JS: $(function(){ //Check whether the number of words exceeds the limit $(#"do_check").bind("click",function(){ alert($("#wooshoo_ipt" ).check().number()); alert($("#wooshoo_ipt").check().error); }); }); HTML:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn