Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Js determine whether the input is empty?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to judge whether the input is empty by Js". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to judge whether the input is empty by Js".

1. Determine whether the input is empty.

Function IsNull () {

Var str = document.getElementById ('str'). Value.trim ()

If (str.length==0) {

Alert ('Sorry, the text box cannot be empty or blank!'); / / Please change the text box to the name of the property you need to verify!

}

}

2. Determine whether the date type is in YYYY-MM-DD format.

Function IsDate () {

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Var reg = / ^ (d {1pm 4}) (- | /) (d {1pm 2}) 2 (d {1pm 2}) $/

Var r = str.match (reg)

If (r==null)

Alert ('Sorry, the date you entered is not in the correct format!'); / / Please change the date to the name of the attribute you need to verify!

}

}

3. Determine whether the date type is in YYYY-MM-DD hh:mm:ss format.

Function IsDateTime () {

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Var reg = / ^ (d {1Magne4}) (- | /) (d {1magin2}) 2 (d {1magin2}) (d {1magin2}): (d {1magin2}): (d {1magin2}) $/

Var r = str.match (reg)

If (r==null)

Alert ('Sorry, the date you entered is not in the correct format!'); / / Please change the date to the name of the attribute you need to verify!

}

}

4. Determine whether the date type is in hh:mm:ss format.

Function IsTime ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ ((20 | 21 | 22 | 23 | [0-1] d): [0-5] [0-9]) (: [0-5] [0-9])? $/

If (! reg.test (str)) {

Alert ("Sorry, the date you entered is not in the correct format!"); / / Please change the "date" to the name of the attribute you need to verify!

}

}

}

5. Judge whether the input characters are English letters.

Function IsLetter ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [a-zA-Z] + $/

If (! reg.test (str)) {

Alert ("Sorry, the alphabet type you entered is not in the correct format!"); / / Please change the "alphabet type" to the attribute name you need to verify!

}

}

}

6. Determine whether the input character is an integer

Function IsInteger ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [- +]

If (! reg.test (str)) {

Alert ("Sorry, the integer type you entered is not in the correct format!"); / / Please replace the integer type with the attribute name you want to verify!

}

}

}

7. Judge whether the input character is double precision.

Function IsDouble (val)

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [- +]? d + (.d +)? $/

If (! reg.test (str)) {

Alert ("Sorry, the double type you entered is not in the correct format!"); / / Please replace the "double type" with the attribute name you want to verify!

}

}

}

8. Judge whether the input character is: a murazine, a melody, 0-9

Function IsString ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [a-zA-Z0-9 _] + $/

If (! reg.test (str)) {

Alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify!

}

}

}

9. Judge whether the input character is Chinese or not

Function IsChinese ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [u0391-uFFE5] + $/

If (! reg.test (str)) {

Alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify!

}

}

}

10. Determine whether the input EMAIL format is correct

Function IsEmail ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ w + ([- +.] w +) * @ w + ([-.] w +) * .w + ([-.] w +) * $/

If (! reg.test (str)) {

Alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify!

}

}

}

11. Judge whether the zip code entered (only six digits) is correct.

Function IsZIP ()

{

Var str = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ d {6} $/

If (! reg.test (str)) {

Alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify!

}

}

}

12. Judge that the input number is no more than a specific number

Function MaxValue ()

{

Var val = document.getElementById ('str'). Value.trim ()

If (str.roomthread0) {

Reg=/ ^ [- +]? dailies /

If (! reg.test (str)) {/ / determine whether it is a numeric type

If (val > parseInt ('123')) / / the maximum value set for yourself

{

Alert ('Sorry, the number you entered is out of range'); / / Please change the number to the name of the attribute you want to verify!

}

}

}

}

Phone: / ^ ((d {2rem 3})) | (d {3} -))? (0d {2jue 3}) | 0d {2pm 3} -)? [1-9] d {6Pert 7} (- d {1jue 4})? $/

Mobile: / ^ ((d {2jin3})) | (d {3} -))? 13d {9} $/

Url: / ^ http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^""])*$/

IdCard: / ^ d {15} (d {2} [A-Za-z0-9])? $/

QQ: / ^ [1-9] d {4jue 8} $/

Some special amount: / ^ (d {1jin3} (, d {3}) *) | (d +) (.d {2})? $/ description: except for "XXX XX,XXX XX,XXX.00" format

/ / provide .trim () attribute for each JS authentication method provided above

String.prototype.trim=function () {

Return this.replace (/ (^ s*) | (slots $) / g, "")

}

Call:

/ / write the JS verification function you want to call in onClick

Var patterms = new Object ()

/ / verify IP

Patterms.ip = / ^ (d {1 ~ 2} | 1dd | 2 [0-4] d | 25 [0-5]) (. (d {1 ~ 2} | 1dd | 2 [0-4] d | 25 [0-5])) {3} $/

/ / verify EMAIL

Patterms.email = / ^ [a-zA-Z0-9 percent -] + @ [a-zA-Z0-9 percent -] + (. [a-zA-Z0-9 percent -] +) + $/

/ / verify date format 2009-07-13

Patterms.date = / ^ d {4}-(0? [1-9] | 1 [0-2])-(0? [1-9] | [1-2] d | 3 [0-1]) $/

/ / verify the time format at 16:55:39

Patterms.time = new RegExp ("^ ([0-1]\ d | 2 [0-3]): [0-5]\ d: [0-5]\ d $")

/ / verify function

Function verify (str,pat)

{

Var thePat

ThePat = patterms [pat]

If (thePat.test (str))

{

Return true

}

Else

{

Return false

}

}

/ / Test

Alert (verify ("asidycom@163.com", "email") + "," + verify ("192.168.1.1", "ip") +

"," + verify ("16:55:39", "time") + "," + verify ("2009-07-13", "date") + "," + verify ("192.168", "ip")

Verification number: ^ [0-9] * $

Verify the n-digit number: ^ d {n} $

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

Verify the number of mmurn digits: ^ d {mrecom n} $

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

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

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

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

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

Validate non-negative integers (positive integers + 0) ^ dunes $

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

Verify a character of length 3: ^. {3} $

Verify the 26-letter string: ^ [A-Za-z] + $

Verify the string of 26 uppercase letters: ^ [Amurz] + $

Verify the string of 26 lowercase letters: ^ [amurz] + $

Verify the string consisting of numbers and 26 letters: ^ [A-Za-z0-9] + $

Verify a string consisting of numbers, 26 letters, or an underscore: ^ walled $

Verification usernames or nicknames are often used: ^ [u4e00-u9fa5A-Za-z0-9Mui _] * $can only be in Chinese and English, numbers, underscores, minus signs

Verify the user password: ^ [a-zA-Z] w {5jin17} $is in the correct format: begins with a letter, is between 6 and 18 in length, and can only contain characters, numbers, and underscores.

Verify whether it contains characters such as ^% &',; =? $": [^% &',; =? $x22] +

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

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

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

Verify the phone number: ^ ((d {3 ~ (4)}) | d {3 ~ (4)})? d {7 ~ (8)} $:-- the correct format is: XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX.

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

Verify that the 12 months of the year: ^ (0? [1-9] | 1 [0-2]) $is in the correct format: "01"-"09" and "1"12"

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

Integer: ^ -? dumped $

Non-negative floating point number (positive floating point number + 0): ^ d + (.d +)? $

Positive floating point number ^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-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] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $

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

Due to the continuous updating of the mobile phone number range, the previous regular expression has been unable to meet the demand. Rewrite this expression.

one

two

three

Var regex = {

Mobile: / ^ 0? (13 [0-9] | 15 [012356789] | 18 [0236789] | 14 [57]) [0-9] {8} $/

}

Expression parsing:

"/" represents a regular expression.

"^" represents the beginning of the string, and "$" represents the end of the string.

"?" It means to match one or zero of the preceding characters, so here 0? It means that the mobile phone number can start with 0 or not.

The next part verifies the 11-digit mobile phone number, starting from 13, because the optional range from 130 to 139 is [0-9], and the number that begins with 15 does not have 154, so there is no 4 in [], of course, it can also be written as [0-35-9]. The numbers opened by 18 and 14 below are the same as above.

The parentheses represent a subexpression in which four optional branches are distinguished by "|". In the rule, the priority of "|" is the lowest. Here, each branch matches 3 characters (a [] can only match one character, which means optional), that is, the first three digits of the mobile phone number, so there are still 8 digits to match. It can be any character of 0-9, so it is "[0-9] {8}", and the number in {} represents the number of matching previous characters. Analysis complete.

At this point, I believe you have a deeper understanding of "Js how to judge whether the input is empty". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report