1. Verify that the URL is valid
function validateURL(textval) {
const urlregex = /^(https? |ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?) (\. (25 [0 to 5] | 2 [0 to 4] [0-9] [0-9] {2} | 1 | [1-9]? [0-9])){3}|([a-zA-Z0-9-]+\.) *[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA- Z0-9.,?'\\+&%$#=~_-]+))*$/
return urlregex.test(textval)
}
/ / case
validateURL("https://juejin.cn/editor/drafts/6969756478932516894") //true
validateURL("https://editor/drafts/6969756478932516894") //false
validateURL("https://editor.commmmm/drafts/6969756478932516894") //false
validateURL("ht://editor.com/drafts/6969756478932516894") //false
Copy the code
2. Verify that the email address is valid
function isEmail(s) {
return / ^ ([a - zA - Z0 - _ - 9]) + @ ([a zA - Z0 - _ - 9]) + ((. [a zA - Z0 - _ - 9] {2, 3})} {1, 2) $/.test(s)
}
/ / case
isEmail("[email protected]") //true
isEmail("[email protected]") //true
isEmail("ddgasd.com") //false
isEmail("[email protected]") //false
Copy the code
2. Check whether the mobile phone number is valid
function isMobile(s) {
return / ^ 1 [3 4 5 7 | | | | 8] [0-9] {9} $/.test(s)
}
/ / case
isMobile("13556525845") //true
isMobile("11454787548") //false
isMobile("135555555554") //false
isMobile("135555555") //false
Copy the code
3. Check whether there is no content
function isNoval(val) {
if (typeof val == 'boolean') {
return false;
}
if (typeof val == 'number') {
return false;
}
if (val instanceof Array) {
if (val.length == 0) return true;
} else if (val instanceof Object) {
if (JSON.stringify(val) === '{}') return true;
} else {
if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == ' ') return true;
return false;
}
return false;
}
/ / case
isNoval(0) //false
isNoval(null) //true
isNoval({}) //true
isNoval("") //true
Copy the code
4. Verify whether it is a valid ID number
function isIdcard(code) {
code+=' '
let list = [];
let result = false;
let msg = ' ';
let city = {
11: "Beijing".12: "Tianjin".13: "Hebei".14: "Shanxi".15: Inner Mongolia.21: "Liaoning".22: "Jilin".23: "Heilongjiang".31: "Shanghai".32: "Jiangsu".33: "Zhejiang".34: "Anhui province".35: "Fujian".36: "Jiangxi".37: "Shandong".41: "Henan".42: "Hubei".43: "Hunan".44: "Guangdong".45: "Guangxi".46: "Hainan".50: "Chongqing".51: "Sichuan".52: "Guizhou".53: "Yunnan".54: "Tibet".61: "Shaanxi".62: "Gansu".63: "Qinghai".64: "The ningxia".65: "Xinjiang".71: "Taiwan".81: "Hong Kong".82: "Macau".91: "Foreign"
};
if(! isNoval(code)) {if (code.length == 18) {
if(! code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
msg = "Incorrect format of id number";
} else if(! city[code.substr(0.2)]) {
msg = "Address code error";
} else {
// The last check bit of the 18-bit id card needs to be verified
code = code.split(' ');
/ / ∑ * Wi (ai) (mod)
// Weighting factor
var factor = [7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2];
/ / check digit
var parity = [1.0.'X'.9.8.7.6.5.4.3.2.'x'];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i++) {
ai = code[i];
wi = factor[i];
sum += ai * wi;
}
if (parity[sum % 11] != code[17]) {
msg = "Incorrect verification digit of CERTIFICATE number";
} else {
result = true;
msg = "Verification of ID number successful"; }}}else {
msg = "No longer than 18 digits"; }}else {
msg = "No blank id number.";
}
list.push(result);
list.push(msg);
return list;
}
/ / case
isIdcard() // [false, "ID number cannot be empty "]
isIdcard(11021547575) // [false, "ID no longer than 18 digits "]
isIdcard(110215475754487875) // [false, "ID check bit error "]
isIdcard("142636198604225314") // [true, "id verification succeeded "]
Copy the code
4. Verify whether JSON can be parsed
function isJSON(str) {
if (typeof str == 'string') {
try {
var obj=JSON.parse(str);
if(typeof obj == 'object' && obj ){
return true;
}else{
return false; }}catch(e) {
console.log('Error:'+str+'!!!!!! '+e);
return false; }}console.log('Must be a string')
return false
}
/ / case
isJSON(123) //false
isJSON('{name:1}') //false
isJSON('{"name":"Liza"}') //true
Copy the code