Home  >  Article  >  Web Front-end  >  Summary of js character verification methods_javascript skills

Summary of js character verification methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:16:05964browse

本文實例匯總了js對字元的驗證方法。分享給大家供大家參考。具體如下:

複製程式碼 程式碼如下:
/**//**
* 입력한 문자열이
라는 문자인지 확인하세요. * 입력: 문자열 문자열
* 반환: true 또는 flase, true는 모든 문자가 한자를 제외함을 의미합니다.
​*/
함수 checkStr(str){
    if (/[^\x00-\xff]/g.test(str)) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* 입력한 문자열에 한자가 포함되어 있는지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 한자를 포함함을 의미합니다.
​*/
함수 check중국어(str){
    if (escape(str).indexOf("%u") != -1) {
        true를 반환합니다.
    }
    그렇지 않으면 {
        false를 반환합니다.
    }
}

/**//**
* 입력한 이메일 형식이 올바른지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
함수 checkEmail(str){
    if (str.match(/[A-Za-z0-9_-] [@](\S*)(net|com|cn|org|cc|tv|[0-9]{1,3})( \S*)/g) == null) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* 입력한 휴대폰 번호 형식이 올바른지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
함수 checkMobilePhone(str){
    if (str.match(/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/) == null) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* 입력한 유선전화번호가 맞는지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
기능 checkTelephone(str){
    if (str.match(/^(([0\ ]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d {3,}))?$/) == null) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* QQ 형식이 올바른지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
함수 checkQQ(str){
    if (str.match(/^\d{5,10}$/) == null) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* 입력한 주민등록번호가 맞는지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
함수 checkCard(str){
    //15位数身份证正则表达式
    var arg1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1] )\d{3}$/;
    //18位数身份证正则表达式
    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}[A-Z])$/;
    if (str.match(arg1) == null && str.match(arg2) == null) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* 입력한 IP 주소가 맞는지 확인하세요
* 입력: 문자열 문자열
* 반환: true 또는 false는 형식이 정확함을 의미합니다.
​*/
함수 checkIP(str){
    var 인수 = /^(\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) {
        false를 반환합니다.
    }
    그렇지 않으면 {
        true를 반환합니다.
    }
}

/**//**
* Check whether the entered URL address is correct
* Input: str string
* Return: true or false; 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 characters have special characters
* Input: str string
* Return: true or false; true means it contains special characters
* Mainly used for verification when registering information
​*/
function checkQuote(str){
    var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
    items.push(":", ";", "'", "|", "\\", "<", ">", "?", "/", "<<", ">>", "||", "//");
    items.push("admin", "administrators", "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 the character
* Input: str string
* Return: true or flase; true means all characters are excluding 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 false; true means containing Chinese characters
​*/
function IsChinese(str)
{
 var reg=/^[\u0391-\uFFE5] $/;
 return reg.test(str);
}

/**//**
* Check whether the entered email format is correct
* Input: str string
* Return: true or false; 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 false; 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("请输入正确的手机号码!");   
   } 
   else{ 
        ; 
   } 

/**//**
* Check whether the entered landline phone number is correct
* Input: str string
* Return: true or false; 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;
    }
}

/**//**
 * 檢查輸入的IP位址是否正確
 * 輸入:str  字串
 *  回傳:true 或 flase; true表示格式正確
 */
函數 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) {
        回傳錯誤;
    }
    否則{
        返回真實;
    }
}

/**//**
 * 檢查輸入的URL位址是否正確
 * 輸入:str  字串
 *  回傳:true 或 flase; true表示格式正確
 */
函數 checkURL(str){
    if (str.match(/(http[s]?|ftp):\/\/[^\/\.] ?\.. \w$/i) == null) {
        回傳錯誤
    }
    否則{
        返回真實;
    }
}

/**//**
 * 檢查輸入的字元是否具有特殊字元
 * 輸入:str  字串
 * 回傳:true 或 flase; true表示包含特殊字元
 * 主要用於註冊資訊的時候驗證
 */
函數 checkQuote(str){
var items = new Array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")");
items.push(":", ";", "'", "|", "\\", "", "?", "/", ">” 、「||」、「//」);
    items.push("admin", "administrators", "administrator", "管理員", "系統管理員");
    items.push("選擇"、"刪除"、"更新"、"插入"、"創建"、"刪除"、"更改"、"轉錄");
    str = str.toLowerCase();
    for (var i = 0; i         if (str.indexOf(items[i]) >= 0) {
            返回真實;
        }
    }
    回傳錯誤;
}

希望本文對大家介紹的javascript程式設計有幫助。

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