Home Web Front-end JS Tutorial javascript form verification function pop-up dialog form_javascript skills

javascript form verification function pop-up dialog form_javascript skills

May 16, 2016 pm 06:51 PM
form javascript verify

Copy code The code is as follows:

/**
* Get the Chinese and English character length
* @param {} str
* @return {}
*/
function LengthB(str){
var p1 = new RegExp('%u..', 'g')
var p2 = new RegExp('%.', 'g')
return escape(str).replace(p1, '').replace(p2, '').length
}
/**
* Filter all space characters
* @param {Object} str
*/
function jsTrim(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 out intermediate strings and multiple spaces
* @param {Object} inputString
*/
function trim(inputString){
if (typeof inputString != "string") {
return inputString;
}
var retValue = inputString;
var ch = retValue.substring(0, 1);
while (ch == " ") {
//检查字符串开始部分的空格
retValue = retValue.substring(1, retValue.length);
ch = retValue.substring(0, 1);
}
ch = retValue.substring(retValue.length - 1, retValue.length);
while (ch == " ") {
//检查字符串结束部分的空格
retValue = retValue.substring(0, retValue.length - 1);
ch = retValue.substring(retValue.length - 1, retValue.length);
}
while (retValue.indexOf(" ") != -1) {
//将文字中间多个相连的空格变为一个空格
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;
*/
function FilterStr(str, filterStr){
filterStr = filterStr == "" ? "'~!@#$%^&*()- ."" : filterStr
var ch;
var i;
var temp;
var error = false;//当包含非法字符时,返回True
for (i = 0; i <= (filterStr.length - 1); i ) {
ch = filterStr.charAt(i);
temp = str.indexOf(ch);
if (temp != -1) {
error = true;
break;
}
}
return error;
}
/**
* Filter the specified content string
* @param {Object} str Check the string
* @param {Object} filterStr Filter the string, if the content is empty, filter by default '~!@#$% ^&*()- ."
* @param {Object} alertStr Pop-up dialogue content
* @param {Object} idStr Return error field ID
*/
function ISFilterStr(str, filterStr, alertStr, idStr){
alertStr = "对不起,您输入的 " alertStr " 不允包含 " filterStr " 非法字符";
if (FilterStr(str, filterStr))
this.AlertAndRFalse(alertStr, idStr);
}
/**
* Check if it is a URL
* @param {} str_url
* @param {} alertStr pop-up field content
* @param {} idStr field ID where the cursor is positioned can only receive ID< ;/b>
* @return {Boolean} notURL returns false;
*/
function IsURL(str_url, alertStr, idStr){// 验证url
alertStr = alertStr " 格式不正确!";
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
"?(([0-9a-z_!~*'().&= $%-] : )?[0-9a-z_!~*'().&= $%-] @)?" // ftp的user@

"(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184

"|" // 允许IP和DOMAIN(域名)

"([0-9a-z_!~*'()-] .)*" // 域名- www.

"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名

"[a-z]{2,6})" // first level domain- .com or .museum

"(:[0-9]{1,4})?" // 端口- :80

"((/?)|" // a slash isn't required if there is no file name

"(/[0-9a-z_!~*'().;?:@&= $,%#-] ) /?)$";
var re = new RegExp(strRegex);
if (!re.test(str_url))
this.AlertAndRFalse(alertStr, idStr);
}
/**
* Check if it is an email
* @param {} str
* @param {} alertStr Pop-up field content
* @param {} idStr Field ID positioned by the cursor can only be received ID
* @return {Boolean} notemail returns false;
*/
function IsEmail(str, alertStr, idStr){
alertStr = alertStr " 格式不正确!";
var re = /^([a-zA-Z0-9] [_|-|.]?)*[a-zA-Z0-9] @([a-zA-Z0-9] [_|-|.]?)*[a-zA-Z0-9] .[a-zA-Z]{2,3}$/;
if (!re.test(str))
this.AlertAndRFalse(alertStr, idStr);
}
/**
* 检查是否为数字
* @param {} str
* @param {} alertStr 弹出字段内容
* @param {} idStr 光标定位的字段ID只能接收ID
* @return {Boolean}NotNumber는 false를 반환합니다.
*/
function IsNum(str, AlertStr, idStr){
alertStr = AlertStr "숫자로 구성되어야 합니다!"; re = /^[d] $/
if (!re.test(str))
this.AlertAndRFalse(alertStr, idStr)
}
/**
* 값이 주어진 범위 내에 있는지 확인하세요

* @param {} str_num
* @param {} moreLen은 값보다 크거나 같아야 합니다
* @param { } lessLen은
* @param {} AlertStr 팝업 필드 내용보다 작거나 같아야 합니다.
* @param {} idStr 커서에 위치한 필드 ID는 ID ;
* @return {Boolean } 최소값보다 작거나 최대값보다 큼숫자가 false를 반환합니다.*/
function IsRangeNum(str_num, moreLen, lessLen, AlertStr, idStr){
IsNum(str_num, AlertStr, idStr);
if (moreLen != "") {
alertStr = AlertStr " 값은 다음과 같습니다. " moreLen;
if (str_num < moreLen)
this.AlertAndRFalse(alertStr, idStr);
}
if (lessLen != "") {
alertStr = AlertStr " 값은 다음보다 클 수 없습니다. " lessLen;
if (str_num > lessLen)
this.AlertAndRFalse(alertStr, idStr)
}
if (moreLen == "" && lessLen == "")
this.AlertAndRFalse("최대 및 최소 길이가 정의되지 않았습니다!", idStr)
}
/**
* 정규화된 문자열인지 확인하세요(대소문자 구분 안 함)

* 길이는 6~20자이며, a-z0-9_로 구성된 문자열입니다.
* @ param {} str 검사된 문자열
* @param {} AlertStr 팝업 필드 내용
* @param {} idStr 커서 위치 필드 ID만 수신할 수 있습니다.
* @return {Boolean} not"길이는 6~20자리이며 a-z0-9_로 구성됩니다."는 false를 반환합니다.
*/
function IsLicit(str, AlertStr, idStr) {
alertStr = " 죄송합니다. " AlertStr "은 비워 둘 수 없으며 0~9 a~z 밑줄 6~20자리로만 구성될 수 있습니다! "
var re = /^[_0-9a-zA-Z ]{6,20} $/
if (!re.test(str))
this.AlertAndRFalse(alertStr, idStr)
}
/**
* 두 문자열이 같은지 확인
* @param {} str1 첫 번째 문자열
* @param {} str2 두 번째 문자열
* @param {} AlertStr 팝업 필드 Content
* @param {} idStr 커서에 위치한 필드 ID는 ID만 수신할 수 있습니다.
* @return {Boolean} 문자열이 동일하지 않으면 false를 반환합니다.*/
function IsStrEquals(str1, str2, AlertStr, idStr){
alertStr = "보조" AlertStr "이 일치하지 않습니다!";
if (str1 != str2)
this.AlertAndRFalse(alertStr, idStr);
}
/**
* 문자열이 주어진 길이 범위 내에 있는지 확인(한자는 2바이트로 계산됨)

*
* @param {} str 확인된 문자
* @param {} moreLen
*보다 크거나 같아야 하는 길이 @param {} lessLen
* @param {}보다 작거나 같아야 하는 길이 AlertStr 팝업 필드 내용
* @param {} 커서가 위치한 idStr 필드 ID< ;b>ID만 수신 가능

* @return {Boolean} 최소 길이보다 작거나 최대 길이보다 큼Number 거짓을 반환합니다.
*/
function IsRange(str, moreLen, lessLen, AlertStr, idStr){
var strLen = LengthB(str)
if (moreLen ! = "") {
alertStr = AlertStr "의 길이는 " moreLen " 바이트보다 크거나 같아야 합니다!"
if (strLen < moreLen)
this.AlertAndRFalse(alertStr, idStr);
}
if (lessLen != "") {
alertStr = AlertStr "의 길이는 " lessLen " 바이트보다 작거나 같아야 합니다!"
if (strLen > lessLen)
this.AlertAndRFalse(alertStr, idStr) ;
}
if (moreLen == "" && lessLen == "")
this.AlertAndRFalse("최대 및 최소 길이가 정의되지 않았습니다!", idStr );
}
/* *
* 문자열이 주어진 길이 범위보다 작은지 확인(한자는 2바이트로 계산)

* @param {} str string
* @param {} lessLen less than or Length
* @param {} AlertStr 팝업 필드 내용
* @param {} 커서에 위치한 idStr 필드 ID는 ID만 수신할 수 있습니다. Boolean} 주어진 길이보다 큰 숫자는 false를 반환합니다.*/
function IsLess(str, lessLen, AlertStr, idStr){
IsRange(str, "", lessLen, AlertStr, idStr); 🎜>}
/**
* 문자가 비어 있지 않은지 확인
* @param {} str
* @param {} AlertStr 팝업 필드 내용
* @param {} idStr 커서 위치의 필드 ID< ;b> ID
만 받을 수 있습니다
* @return {Boolean} 문자가 비어 있습니다Return false;*/
function IsEmpty(str, AlertStr, idStr){
alertStr = AlertStr "비워둘 수 없습니다!"
if (str == "" )
this.AlertAndRFalse(alertStr, idStr )
}
/**
* 경고 상자 팝업 및 오류 필드 찾기
* @param {} AlertStr 경고 상자 내용 팝업
* @param {} idStr 필드 포커스 반환
* @return {Boolean} 거짓 반환
*/
function AlertAndRFalse(alertStr, idStr){
alert(alertStr); document.getElementById(idStr).focus();
throw "valueErr";
//JavaScript Document
Call example
function checkForm() {
try {
var title = trim(document.getElementById('title')); //Article title
IsEmpty(title,"Article Title","title");//Cannot be empty
IsLess(title,100,"Article Title","title");//The title should be less than 100 characters long
ISFilterStr(title, "~`!@#$%^&*()-=_ []{}|\;':",./<>?", "Article title","title") //Titles are not allowed to contain these illegal characters
} catch(err) {
if(err == "valueErr")
return false;
}
}
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles