


Common regular rules for javascript form validation_javascript skills
/*
Purpose: Verify the format of the ip address
Input: strIP: ip address
Return: true if the verification is passed, otherwise false;
*/
function isIP(strIP) {
if (isNull(strIP)) return false;
var re = /^(d ).(d ).(d ).(d )$/g //Match IP Regular expression of address
if (re.test(strIP)) {
if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256 ) return true;
}
return false;
}
/*
Purpose: Check whether the input string is empty or all spaces
Input: str
Return :
If all are empty, return true, otherwise return false
*/
function isNull(str) {
if (str == "") return true;
var regu = "^ [ ] $";
var re = new RegExp(regu);
return re.test(str);
}
/*
Purpose: Check whether the value of the input object conforms to an integer Format
Input: str Input string
Return: true if verified, otherwise false
*/
function isInteger(str) {
var regu = /^[-] {0,1}[0-9]{1,}$/;
return regu.test(str);
}
/*
Purpose: Check whether the entered mobile phone number is correct
Input:
s: String
Return:
Return true if verified, false otherwise
*/
function checkMobile(s) {
var regu = /^[ 1][0-9][0-9]{9}$/;
var re = new RegExp(regu);
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string conforms to the positive integer format
Input:
s: string
Returns:
Returns true if the verification is passed, otherwise returns false
*/
function isNumber(s) {
var regu = "^[0-9] $";
var re = new RegExp(regu);
if (s.search(re) != -1) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string is in a numeric format with decimals, it can be a negative number
Input:
s: string
Return:
If the verification is passed Return true, otherwise return false
*/
function isDecimal(str) {
if (isInteger(str)) return true;
var re = /^[-]{0,1}( d )[.] (d )$/;
if (re.test(str)) {
if (RegExp.$1 == 0 && RegExp.$2 == 0) return false;
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the value of the input object conforms to the port number format
Input: str input String
returns: true if verified, false otherwise
*/
function isPort(str) {
return (isNumber(str) && str < 65536);
}
/*
Purpose: Check whether the value of the input object conforms to the E-Mail format
Input: str input string
Return: if it passes the verification, it returns true, otherwise it returns false
*/
function isEmail(str) {
var myReg = /^[-_A-Za-z0-9] @([_A-Za-z0-9] .) [A-Za-z0-9]{ 2,3}$/;
if (myReg.test(str)) return true;
return false;
}
/*
Purpose: Check whether the input string conforms to the amount format
The format is defined as a positive number with decimals, up to three digits after the decimal point
Input:
s: string
Return:
If it passes verification, it returns true, otherwise it returns false
* /
function isMoney(s) {
var regu = "^[0-9] [.][0-9]{0,3}$";
var re = new RegExp(regu) ;
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Usage: Check whether the input string only consists of English letters, numbers and underscores
Input:
s: string
Return:
If it passes the verification, it returns true, otherwise it returns false
*/
function isNumberOr_Letter(s) {//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z_] $";
var re = new RegExp(regu);
if (re.test(s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check input string Whether it consists of only English letters and numbers
Input:
s: String
Return:
Returns true if it passes verification, otherwise returns false
*/
function isNumberOrLetter(s) {//Determine whether it is a number or letter
var regu = "^[0-9a-zA-Z] $";
var re = new RegExp(regu);
if (re.test( s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Check whether the input string only consists of Chinese characters and letters , composed of numbers
Input:
value: string
Return:
If it passes verification, it returns true, otherwise it returns false
*/
function isChinaOrNumbOrLett(s) {//Judge whether It is composed of Chinese characters, letters and numbers
var regu = "^[0-9a-zA-Zu4e00-u9fa5] $";
var re = new RegExp(regu);
if (re.test( s)) {
return true;
} else {
return false;
}
}
/*
Purpose: Determine whether it is a date
Input: date: date; fmt: date format
Return: return true if it passes verification, otherwise return false
*/
function isDate(date, fmt) {
if (fmt == null) fmt = "yyyyMMdd";
var yIndex = fmt.indexOf("yyyy");
if (yIndex == -1) return false;
var year = date.substring(yIndex, yIndex 4);
var mIndex = fmt.indexOf("MM");
if (mIndex == -1) return false;
var month = date.substring(mIndex, mIndex 2);
var dIndex = fmt.indexOf("dd");
if (dIndex == -1) return false;
var day = date. substring(dIndex, dIndex 2);
if (!isNumber(year) || year > "2100" || year < "1900") return false;
if (!isNumber(month) || month > "12" || month < "01") return false;
if (day > getMaxDay(year, month) || day < "01") return false;
return true;
}
function getMaxDay(year, month) {
if (month == 4 || month == 6 || month == 9 || month == 11)
return "30" ;
if (month == 2)
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return "29";
else
return "28";
return "31";
}
/*
Purpose: Whether character 1 ends with string 2
Input: str1: string; str2: The contained string
returns: true if verified, otherwise false
*/
function isLastMatch(str1, str2) {
var index = str1.lastIndexOf(str2);
if (str1.length == index str2.length) return true;
return false;
}
/*
Purpose: Whether character 1 starts with string 2
Input: str1 : String; str2: Contained string
Return: true if verified, otherwise false
*/
function isFirstMatch(str1, str2) {
var index = str1.indexOf (str2);
if (index == 0) return true;
return false;
}
/*
Purpose: Character 1 is the containing string 2
Input: str1 : String; str2: Contained string
Return: true if verified, otherwise false
*/
function isMatch(str1, str2) {
var index = str1.indexOf (str2);
if (index == -1) return false;
return true;
}
/*
Purpose: Check whether the entered start and end dates are correct, the rules are two The format of the date is correct,
and the end is as scheduled> = start date
Input:
startDate: start date, string
endDate: end as scheduled, string
Return:
Returns true if it passes validation, otherwise returns false
*/
function checkTwoDate(startDate, endDate) {
if (!isDate(startDate)) {
alert("The start date is incorrect! ");
return false;
} else if (!isDate(endDate)) {
alert("The end date is incorrect!");
return false;
} else if ( startDate > endDate) {
alert("The start date cannot be greater than the end date!");
return false;
}
return true;
}
/*
Purpose: Check whether the entered Email mailbox format is correct
Input:
strEmail: string
Return:
If it passes verification, it returns true, otherwise it returns false
*/
function checkEmail (strEmail) {
//var emailReg = /^[_a-z0-9] @([_a-z0-9] .) [a-z0-9]{2,3}$/;
var emailReg = /^[w-] (.[w-] )*@[w-] (.[w-] ) $/;
if (emailReg.test(strEmail)) {
return true ;
} else {
alert("The email address format you entered is incorrect! ");
return false;
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use Flask-WTF to implement form validation Flask-WTF is a Flask extension for handling web form validation. It provides a concise and flexible way to validate user-submitted data. This article will show you how to use the Flask-WTF extension to implement form validation. Install Flask-WTF To use Flask-WTF, you first need to install it. You can use the pip command to install: pipinstallFlask-WTF import the required modules in F

Laravel is a popular PHP web development framework that provides many convenient features to speed up the work of developers. Among them, LaravelValidation is a very practical function that can help us easily validate form requests and user-entered data. This article will introduce how to use LaravelValidation to validate form requests. What is LaravelValidationLaravelValidation is La

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

PHP form validation tips: How to use the filter_input function to verify user input Introduction: When developing web applications, forms are an important tool for interacting with users. Correctly validating user input is one of the key steps to ensure data integrity and security. PHP provides the filter_input function, which can easily verify and filter user input. This article will introduce how to use the filter_input function to verify user input and provide relevant code examples. one,

How to use middleware to handle form validation in Laravel, specific code examples are required Introduction: Form validation is a very common task in Laravel. In order to ensure the validity and security of the data entered by users, we usually verify the data submitted in the form. Laravel provides a convenient form validation function and also supports the use of middleware to handle form validation. This article will introduce in detail how to use middleware to handle form validation in Laravel and provide specific code examples.

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

ThinkPHP6 is a PHP-based MVC framework that greatly simplifies the development of web applications. Among them, form validation is a very basic and important function. In this article, we will introduce how to perform form validation operations in ThinkPHP6. 1. Definition of validation rules In ThinkPHP6, validation rules need to be defined in the controller. We can define the rules by defining a $validate attribute in the controller, as shown below: usethinkVa
