JS is often summarized with regular expressions

  • 2020-03-29 23:42:21
  • OfStack


<script type="text/javascript">
     function validate(){
       var reg = new RegExp("^[0-9]*$");
       var obj = document.getElementById("name");
    if(!reg.test(obj.value)){
        alert(" Please enter a number !");
    }
    if(!/^[0-9]*$/.test(obj.value)){
        alert(" Please enter a number !");
    }
  }
</script>

Verify the set of regular expressions for Numbers

Verification number: ^[0-9]*$

Verify n - bit number: ^\d{n}$

Verify at least n digits: ^\d{n,}$

Verify m-n digits: ^\d{m,n}$

Verify zero and non-zero starting Numbers: ^(0|[1-9][0-9]*)$

Verify that there are two decimal positive real Numbers: ^[0-9]+(.[0-9]{2})? $

Verify that there are 1-3 decimal positive real Numbers: ^[0-9]+(.[0-9]{1,3})? $

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

Verify non-zero negative integer: ^\-[1-9][0-9]*$

Verify that the nonnegative integer (positive integer + 0) ^\d+$

Verify that the non-positive integer (negative integer + 0) ^(-\d+)|(0+))$

Verify 3 characters: ^.{3}$

Validate A string of 26 English letters: ^[a-za-z]+$

Validate A string of 26 uppercase English letters: ^[a-z]+$

Validate a string of 26 lowercase English letters: ^[a-z]+$

Verify A string of Numbers and 26 English letters: ^[a-za-z0-9]+$

Validate a string of Numbers, 26 English letters, or underscores: ^\w+$

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

Verify if ^%&',; =? $\" : [^%&',;=?$\x22]+

Verify Chinese characters: ^[\u4e00-\u9fa5],{0,}$

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

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

Verify phone number: ^(\(\d{3,4}\)|\d{3,4}-)? \d{7,8}$: -- correct format: xxx-xxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxx, xxx-xxxxxxxx, XXXXXXX, XXXXXXXX.

Verify id number (15 or 18 digits) : ^\d{15}|\d{}18$

Verify 12 months of the year: ^(0? [1-9]|1[0-2])$

Verify 31 days of a month: ^((0? [1-9])|((1|2)[0-9])|30|31)

Integer: ^ -? \ d + $

Nonnegative floating point number (positive floating point number + 0) : ^\d+(\.\d+)? $

Floating-point ^ (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) | ([0-9] * [1-9] [0-9] *)) $

Nonpositive floating point (negative floating point + 0) to the (-\d+(\.\d+)?) | (0 + (\. 0 +)? ) $

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

Floating-point ^ (-? (\. \ \ d +) d +)? $


Related articles: