search
HomeWeb Front-endJS Tutorialjs data verification set, js email verification, js url verification, js length verification, js digital verification and other simple packages_form effects

Some time ago, I wrote a js data verification, js email verification, js url verification, js length verification, js digital verification and other pop-up dialog boxes. However, that kind of unfriendly method is not very popular now, so I rewrote one, It is better encapsulated and more friendly layer form is shared with everyone. If you have any bugs in using it, please leave me a message to improve it. Thank you.

js code

Copy code The code is as follows:

/**
* Data validation framework. When an error occurs in the id field check, a element is added directly after the corresponding response to display the error message.
*
* @author wangzi6hao
* @version 2.1
* @description 2009-05-16
*/
var checkData = new function() {
var idExt="_wangzi6hao_Span";//Generate the id suffix of the span layer
/**
* Get the Chinese and English character length (Chinese is 2 characters)
*
* @param {}
* str
* @return The character length
*/
this.length = function(str) {
var p1 = new RegExp('%u..', 'g')
var p2 = new RegExp('%.', 'g')
return escape(str).replace(p1, '').replace(p2, '').length
}
/**
* Delete the corresponding id element
*/
this. remove = function(id) {
var idObject = document.getElementById(id);
if (idObject != null)
idObject.parentNode.removeChild(idObject);
}
/ **
* Error message after the corresponding id
*
* @param id: The id element that needs to display the error message
* str: Display the error message
*/
this.appendError = function(id, str) {
this.remove(id idExt);// If the span element exists, delete this element first
var spanNew = document.createElement("span"); // Create span
spanNew.id = id idExt; // Generate spanid
spanNew.style.color = "red";
spanNew.appendChild(document.createTextNode (str));// Add content to span
var inputId = document.getElementById(id);
inputId.parentNode.insertBefore(spanNew, inputId.nextSibling);// Add span
}
/**
* @description Filter all space characters.
* @param str: The original string that needs to remove spaces
* @return Returns the string with spaces removed
*/
this.trimSpace = function(str) {
str = "";
while ((str.charAt(0) == ' ') || (str.charAt(0) == '???')
|| (escape(str.charAt(0 )) == '%u3000'))
str = str.substring(1, str.length);
while ((str.charAt(str.length - 1) == ' ')
|| (str.charAt(str.length - 1) == '???')
|| (escape(str.charAt(str.length - 1)) == '%u3000'))
str = str.substring(0, str.length - 1);
return str;
}
/**
* Filter the spaces at the beginning of the string and the spaces at the end of the string. Change multiple connected spaces in the middle of the text into one space
*
* @param {Object}
* inputString
*/
this.trim = function(inputString) {
if (typeof inputString != "string") {
return inputString;
}
var retValue = inputString;
var ch = retValue.substring(0, 1);
while (ch == " ") {
// Check the space at the beginning of the string
retValue = retValue.substring(1, retValue.length);
ch = retValue.substring(0, 1);
}
ch = retValue.substring(retValue.length - 1, retValue.length);
while (ch == " ") {
// Check the space at the end of the string
retValue = retValue.substring(0, retValue.length - 1);
ch = retValue.substring(retValue.length - 1, retValue.length);
}
while (retValue.indexOf(" ") != -1) {
// Change multiple connected spaces in the middle of the text into one space
retValue = retValue.substring(0, retValue.indexOf(" "))
retValue.substring (retValue.indexOf(" ") 1,
retValue.length);
}
return retValue;
}
/**
* Filter string, specify the filter content. If the content is empty, the default filter is '~!@#$%^&*()- ."
*
* @param {Object}
* str
* @param {Object}
* filterStr
*
* @return contains filter content, returns True, otherwise returns false;
*/
this.filterStr = function(str, filterString) {
filterString = filterString == "" ? "'~!@#$%^&*()- ."" : filterString
var ch;
var i;
var temp;
var error = false;// When illegal characters are included, return True
for (i = 0; i ch = filterString.charAt(i);
temp = str.indexOf(ch);
if (temp != -1) {
error = true;
break;
}
}
return error;
}
this.filterStrSpan = function(id, filterString) {
filterString = filterString == "" ? "'~!@#$%^&*( )- ."" : filterString
var val = document.getElementById(id);
if (this.filterStr(val.value, filterString)) {
val.select();
var str = "Cannot contain illegal characters" filterString;
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true ;
}
}
/**
* Check if it is a URL
*
* @param {}
* str_url
* @return {Boolean} true: It is a URL, false: is not > ;URL;
*/
this.isURL = function(str_url) {// Verification url
var strRegex = "^((https|http| ftp|rtsp|mms)?://)"
"?(([0-9a-z_!~*'().&= $%-] : )?[0-9a-z_!~* '().&= $%-] @)?" // ftp user@
"(([0-9]{1,3}.){3}[0-9]{1,3 }" // URL in IP form - 199.194.52.184
"|" // Allow IP and DOMAIN (domain name)
"([0-9a-z_!~*'()-] .)*" // Domain name - www.
"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // Second-level domain name
"[a-z]{2,6})" // first level domain- .com or .museum
"(:[0-9]{1,4})?" // Port- :80
"((/?)|" // a slash isn't required if there is no file name
"(/[0-9a-z_!~*'().;?:@&= $,% #-] ) /?)$";
var re = new RegExp(strRegex);
return re.test(str_url);
}
this.isURLSpan = function(id) {
var val = document.getElementById(id);
if (!this.isURL(val.value)) {
val.select();
var str = "Link does not conform to the format;" ;
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check if it is an email
*
* @param {}
* str
* @return {Boolean} true: email, false: is not b>Email;
*/
this.isEmail = function(str) {
var re = /^([a-zA-Z0-9] [_|-|.]?) *[a-zA-Z0-9] @([a-zA-Z0-9] [_|-|.]?)*[a-zA-Z0-9] .[a-zA-Z]{2 ,3}$/;
return re.test(str);
}
this.isEmailSpan = function(id) {
var val = document.getElementById(id);
if (!this.isEmail(val.value)) {
val.select();
var str = "Email does not meet the format;";
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check if it is a number
*
* @param {}
* str
* @return {Boolean} true: number, false: is not Number;
*/
this. isNum = function(str) {
var re = /^[d] $/
return re.test(str);
}
this.isNumSpan = function(id) {
var val = document.getElementById(id);
if (!this.isNum(val.value)) {
val.select();
var str = "Must be a number;";
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check whether the value is within the given range, if it is empty, no check is performed

*
* @param {}
* str_num
* @param {}
* small The value that should be greater than or equal to (when this value is empty, only check that it cannot be greater than the maximum value)
* @param {}
* big The value that should be less than or equal to (when this value is empty, only The check cannot be less than the minimum value)
*
* @return {Boolean} Less than the minimum value or greater than the maximum valueNumber returns false, otherwise returns true;
*/
this.isRangeNum = function(str_num, small, big) {
if (!this.isNum(str_num )) // Check if it is a number
return false
if (small == "" && big == "")
throw str_num "No maximum or minimum number is defined!";
if (small != "") {
if (str_num return false;
}
if (big != "") {
if (str_num > big)
return false;
}
return true;
}
this.isRangeNumSpan = function(id, small, big) {
var val = document. getElementById(id);
if (!this.isRangeNum(val.value, small, big)) {
val.select();
var str = "";
if (small ! = "") {
str = "Should be greater than or equal to" small;
}
if (big != "") {
str = "Should be less than or equal to" big;
}
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check whether it is a qualified string (case-insensitive)

* is a string composed of a-z0-9_
*
* @param {}
* str Checked string
* @param {}
* idStr Cursor positioned field ID can only receive ID
* @return {Boolean} No "a-z0-9_" composition returns false, otherwise returns true;
*/
this.isLicit = function(str) {
var re = /^[_0-9a-zA-Z]*$/
return re.test (str);
}
this.isLicitSpan = function(id) {
var val = document.getElementById(id);
if (!this.isLicit(val.value)) {
val.select();
var str = "It is a string consisting of a-z0-9_ (not case sensitive);";
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check whether two strings are equal
*
* @param {}
* str1 first string
* @param {}
* str2 second character String
* @return {Boolean} If the strings are not equal, return false, otherwise return true;
*/
this .isEquals = function(str1, str2) {
return str1 == str2;
}
this.isEqualsSpan = function(id, id1) {
var val = document.getElementById(id);
var val1 = document.getElementById(id1);
if (!this.isEquals(val.value, val1.value)) {
val.select();
var str = "二The input contents must be the same;";
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check whether the string is within the given length range (Chinese characters are calculated as 2 bytes). If the character is empty, no check is performed

*
* @param {}
* str Checked characters
* @param {}
* lessLen The length that should be greater than or equal to
* @param {}
* moreLen The length that should be less than or equal to
*
* @return {Boolean} Less than the minimum length or greater than the maximum lengthNumber returns false;
*/
this.isRange = function(str, lessLen, moreLen) {
var strLen = this.length(str);
if (lessLen != "") {
if (strLen return false;
}
if (moreLen != "") {
if (strLen > moreLen )
return false;
}
if (lessLen == "" && moreLen == "")
throw "No maximum and minimum lengths defined!";
return true;
}
this.isRangeSpan = function(id, lessLen, moreLen) {
var val = document.getElementById(id);
if (!this.isRange(val.value, lessLen, moreLen)) {
var str = "length";
if (lessLen != "")
str = "greater than or equal to " lessLen ";";
if (moreLen != "")
str = "Should be less than or equal to" moreLen;
val.select();
this.appendError(id, str);
return false;
} else {
this.remove( id idExt);
return true;
}
}
/**
* Check whether the string is smaller than the given length range (Chinese characters are calculated as 2 bytes)

*
* @param {}
* str string
* @param {}
* lessLen less than or equal to the length
*
* @return {Boolean} less than the given length number returns false;
*/
this.isLess = function(str, lessLen) {
return this.isRange (str, lessLen, "");
}
this.isLessSpan = function(id, lessLen) {
var val = document.getElementById(id);
if (!this.isLess( val.value, lessLen)) {
var str = "Length is greater than or equal to" lessLen;
val.select();
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check whether the string is larger than the given length range (Chinese characters are calculated as 2 bytes)

*
* @param {}
* str string
* @param {}
* moreLen less than or equal to the length
*
* @return {Boolean} greater than the given length number returns false;
*/
this.isMore = function( str, moreLen) {
return this.isRange(str, "", moreLen);
}
this.isMoreSpan = function(id, moreLen) {
var val = document.getElementById(id );
if (!this.isMore(val.value, moreLen)) {
var str = "Length should be less than or equal to" moreLen;
val.select();
this.appendError (id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
/**
* Check that the character is not empty
*
* @param {}
* str
* @return {Boolean}Character is emptyReturn true, otherwise false;
*/
this.isEmpty = function(str) {
return str == "";
}
this.isEmptySpan = function(id) {
var val = document.getElementById(id);
if (this.isEmpty(val.value)) {
var str = "Not allowed Empty;";
val.select();
this.appendError(id, str);
return false;
} else {
this.remove(id idExt);
return true;
}
}
}

Test page
Copy code The code is as follows:



Webpage title










> ;











< ;/tr>


< ;td>Length greater than control:









Character filtering: Link:
Email: Number :
Number range : a-zA-Z0-9_
Judge equality:
Length control:
Length is less than control: Is it empty:




Statement
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
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool