This article summarizes the validation methods of JS against characters. Share it for your reference. The details are as follows:
Copy the code as follows:/**//**
* Check whether the entered string of characters is a character
* Input:str string
* Return: true or flase; true means that all characters do not contain Chinese characters
*/
function checkStr(str){
if (/[^//x00-//xff]/g.test(str)) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered string of characters contains Chinese characters
* Input:str string
* Return: true or flase; true means containing Chinese characters
*/
function checkChinese(str){
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
}
/**//**
* Check whether the entered mailbox format is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkEmail(str){
if (str.match(/[A-Za-z0-9_-]+[@](//S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(//S*)/g) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered mobile phone number format is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkMobilePhone(str){
if (str.match(/^(?:13//d|15[89])-?//d{5}(//d{3}|//*{3})$/) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check that the entered landline number is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkTelephone(str){
if (str.match(/^(([0//+]//d{2,3}-)?(0//d{2,3})-)(//d{7,8})(-(//d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the QQ format is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkQQ(str){
if (str.match(/^//d{5,10}$/) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered ID number is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkCard(str){
//15-digit ID card regular expression
var arg1 = /^[1-9]//d{7}((0//d)|(1[0-2]))(([0|1|2]//d)|3[0-1])//d{3}$/;
//18-digit ID card regular expression
var arg2 = /^[1-9]//d{5}[1-9]//d{3}((0//d)|(1[0-2]))(([0|1|2]//d)|3[0-1])((//d{4})|//d{3}[AZ])$/;
if (str.match(arg1) == null && str.match(arg2) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered IP address is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkIP(str){
var arg = /^(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])$/;
if (str.match(arg) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered URL address is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkURL(str){
if (str.match(/(http[s]?|ftp):://////[^/////.]+?//..+//w$/i) == null) {
return false
}
else {
return true;
}
}
/**//**
* Check whether the entered character has special characters
* Input:str string
* Return: true or flase; true means that special characters are included
* Mainly used for verification when registering information
*/
function checkQuote(str){
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "////", "<", ">", "?", "?", "<<", ">>", "||", "//");
items.push("admin", "administrators", "administrator", "administrator", "system administrator");
items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate");
str = str.toLowerCase();
for (var i = 0; i < items.length; i++) {
if (str.indexOf(items[i]) >= 0) {
return true;
}
}
return false;
}
/**//**
* Check whether the entered string of characters is a character
* Input:str string
* Return: true or flase; true means that all characters do not contain Chinese characters
*/
function checkStr(str){
if (/[^//x00-//xff]/g.test(str)) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered string of characters contains Chinese characters
* Input:str string
* Return: true or flase; true means containing Chinese characters
*/
function IsChinese(str)
{
var reg=/^[//u0391-//uFFE5]+$/;
return reg.test(str);
}
/**//**
* Check whether the entered mailbox format is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkEmail(str){
if (str.match(/[A-Za-z0-9_-]+[@](//S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(//S*)/g) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered mobile phone number format is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkMobile(v){
var a = /^((//(/(//d{3}//))|(//d{3}//-))?13//d{9}|14[57]//d{8}|15//d{9}|18//d{9}$/ ;
if( v.length!=11||!v.match(a) )
{
alert("Please enter the correct mobile phone number!");
}
else{
;
}
}
/**//**
* Check that the entered landline number is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkTelephone(str){
if (str.match(/^(([0//+]//d{2,3}-)?(0//d{2,3})-)(//d{7,8})(-(//d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered IP address is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkIP(str){
var arg = /^(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])//.(//d{1,2}|1//d//d|2[0-4]//d|25[0-5])$/;
if (str.match(arg) == null) {
return false;
}
else {
return true;
}
}
/**//**
* Check whether the entered URL address is correct
* Input:str string
* Return: true or flase; true means the format is correct
*/
function checkURL(str){
if (str.match(/(http[s]?|ftp):://////[^/////.]+?//..+//w$/i) == null) {
return false
}
else {
return true;
}
}
/**//**
* Check whether the entered character has special characters
* Input:str string
* Return: true or flase; true means that special characters are included
* Mainly used for verification when registering information
*/
function checkQuote(str){
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "////", "<", ">", "?", "?", "<<", ">>", "||", "//");
items.push("admin", "administrators", "administrator", "administrator", "system administrator");
items.push("select", "delete", "update", "insert", "create", "drop", "alter", "trancate");
str = str.toLowerCase();
for (var i = 0; i < items.length; i++) {
if (str.indexOf(items[i]) >= 0) {
return true;
}
}
return false;
}
I hope this article will be helpful to everyone's JavaScript programming.