JS regular expression verification collection

Time:2021-2-10
function validate(){
    var reg = new RegExp("^[0-9]*$");
    var obj = document.getElementById("ID");
    if(!reg.test(obj.value)){
        Alert ("please enter a number!");
    }
    if(!/^[0-9]*$/.test(obj.value)){
        Alert ("please enter a number!");
    }
}

Common regular expression sets:

Verification number: ^ [0-9]*$

Verify n-digit numbers: ^ {D {n}$

Verify at least n digits: ^ – D {n,}$

Verify the number of M-N bits: ^ – D {m, n}$

Verify zero and non-zero numbers: ^ (0 | 1-9 *)$

Verify the positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})$

Verify the positive real number with 1-3 decimal places: ^ [0-9] + (. [0-9] {1,3})$

Verify non-zero positive integers: ^ \ +? 1-9*$

Verify non-zero negative integers: ^ – 1-9*$

Verify non negative integers (positive integers + 0) ^ D+$

Verify non positive integers (negative integers + 0) ^ ((- – D +) | (0 +))$

Verify characters of length 3: ^. {3}$

Verify the 26 letter string: ^ [a-za-z]+$

Verify a string of 26 uppercase English letters: ^ [A-Z]+$

Verify the string composed of 26 lowercase English letters: ^ [A-Z]+$

Verify the string composed of numbers and 26 English letters: ^ [a-za-z0-9]+$

Verify a string of numbers, 26 letters, or underscores: ^ \+$

User name or nickname verification is often used: ^ [[u4e00 – [u9fa5a-za-z0-9]-_ ]*$can only be in Chinese and English, numbers, underscores and minus signs

Verify the user password: ^ [a-za-z] [w {5,17} $the correct format is: it starts with a letter, the length is between 6-18, and can only contain characters, numbers and underscores.

Verify whether there are characters such as ^% & ‘; =? $\ \ “: [^% &’; =? $\ X22]+

Verification of Chinese characters: ^ [[u4e00 – [u9fa5], {0,}$

Verify email address: ^ (W + [- +.] \ W +)@\w+([-.]\w+)\.\w+([-.]\w+)*$

Verify Internet URL:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ ;^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$

Verify the telephone number: ^ ((\ \ (< D {3,4}) | [D {3,4} -)?? [D {7,8} $: — the correct format is: xxx-xxxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxxxxx, XXXXXXXX, XXXXXXXX.

Verify ID number (15 digit or 18 digit): ^\d{15}|\d{}18$

Verify the 12 months of a year: ^ (0? [1-9] | 1 [0-2]) $the correct format is: “01” – “09” and “1” “12”

Verify the 31 days of a month: ^ ((0? [1-9]) | ((1? [2] [0-9]) | 30? [31) $correct format: 01, 09 and 1, 31.

Integer: ^ -? \ D+$

Non negative floating-point number (positive floating-point number + 0): ^ + D + (\. [D +)$

Positive floating point number ^ (([0-9] + \. [0-9]1-9)|([0-9]1-9\.[0-9]+)|([0-9]1-9))$

Non positive floating-point number (negative floating-point number + 0) ^ ((- – D + (\. [D +)?)) | (0 + (\. 0 +)?))$

Negative floating point number ^ (- ([0-9] + \. [0-9]1-9)|([0-9]1-9\.[0-9]+)|([0-9]1-9)))$

Floating point number ^ (-?) (\. [D +)$

Recommended Today

Explore the application of webpack5’s new feature module Federation in Tencent documents

Original text:Explore the application of webpack5’s new feature module Federation in Tencent documents | alloyteamAuthor: tat.jay preface: The exciting new feature module Federation of webpack5 may not excite many developers, but it is really a bright spot for Tencent documents hurt by multiple applications. This article will take you to understand the dilemma of Tencent […]