Common Javascript string judgment function code sharing _ javascript skills

Source: Internet
Author: User
Tags rtrim
This article mainly shares a piece of code for common Javascript string judgment functions. Basically, common string judgments are covered and very practical. For more information, see. For more information, see the comments. There is not much nonsense here. go directly to the Code:

The Code is as follows:


/*
Function obj $ (id) Get the object according to the id
Function val $ (id) obtains the object Value Based on the id.
Function trim (str) deletes spaces on the left and right
Function ltrim (str) deletes spaces on the left
Function rtrim (str) deletes spaces on the right
Whether the function isEmpty (str) string has a value
Function equals (str1, str2) js checks whether the two strings are equal
Function compute signorecase (str1, str2) js checks whether the two strings are equal regardless of the case.
Function isChinese (str) js determines whether to judge Chinese
Function isEmail (strEmail) js determines whether an email is sent
Function isImg (str) js is used to determine whether an image is in jpg format | jpeg | swf | gif
Function isInteger (str) js determines if it is an integer
Function isFloat js determines whether it is a floating point number.
Function isPost (str) js determines whether the zip code (one to six digits)
Function isMobile (str) js determines if it is a mobile phone number
Function isPhone (str) js determines whether a telephone number must contain a zone number and can contain an extension number.
Function isQQ (str) js determines whether the QQ number is valid
Function isIP (str) js determines whether it is a valid IP Address
Function isDate (str) js determines whether the date type is used (for example, 2005-12-12)
Function isIdCardNo (idNumber) js determines whether the ID card number is valid
*/
Function obj $ (id)
{
Return document. getElementById (id );
}
Function val $ (id)
{
Var obj = document. getElementById (id );
If (obj! = Null)
{
Return obj. value;
}
Return null;
}
Function trim (str)
{
Return str. replace (/(^ \ s *) | (\ s * $)/g ,'');
}
Function ltrim (str)
{
Return str. replace (/^ \ s */g ,'');
}
Function rtrim (str)
{
Return str. replace (/\ s * $ /,'');
}
Function isEmpty (str)
{
If (str! = Null & str. length> 0)
{
Return true;
}
Return false;
}
Function equals (str1, str2)
{
If (str1 = str2)
{
Return true;
}
Return false;
}
Function compute signorecase (str1, str2)
{
If (str1.toUpperCase () = str2.toUpperCase ())
{
Return true;
}
Return false;
}
Function isChinese (str)
{
Var str = str. replace (/(^ \ s *) | (\ s * $)/g ,'');
If (! (/^ [\ U4E00-\ uFA29] * $/. test (str)
&&(! /^ [\ UE7C7-\ uE7F3] * $/. test (str ))))
{
Return false;
}
Return true;
}
Function isEmail (str)
{
If (/^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ /. test (str ))
{
Return true
}
Return false;
}
Function isImg (str)
{
Var objReg = new RegExp ("[.] + (jpg | jpeg | swf | gif) $", "gi ");
If (objReg. test (str ))
{
Return true;
}
Return false;
}
Function isInteger (str)
{
If (/^ -? \ D + $/. test (str ))
{
Return true;
}
Return false;
}
Function isFloat (str)
{
If (/^ (-? \ D +) (\. \ d + )? $/. Test (str)
{
Return true;
}
Return false;
}
Function isPost (str)
{
If (/^ \ d {1, 6} $/. test (str ))
{
Return true;
}
Return false;
}
Function isMobile (str)
{
If (/^ 1 [35] \ d {9}/. test (str ))
{
Return true;
}
Return false;
}
Function isPhone (str)
{
If (/^ (0 [1-9] \ d {}-) \ d {} (-\ d })? /. Test (str ))
{
Return true;
}
Return false;
}
Function isQQ (str ){
If (/^ \ d {5, 9} $/. test (str ))
{
Return true;
}
Return false;
}
Function isIP (str ){
Var reg =/^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ /;
If (. test (str ))
{
Return true;
}
Return false;
}
Function isDate (str)
{
Var reg =/^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ /;
If (reg. test (str ))
{
Return true;
}
Return false;
}
Function isIdCardNo (idNumber)
{
Var factorArr = new Array );
Var varArray = new Array ();
Var lngProduct = 0;
Var intCheckDigit;
Var idNumber. length =;
If (idNumber. length! = 15) & (idNumber. length! = 18 ))
{
Return false;
}
For (I = 0; I {
VarArray [I] = idNumber. charAt (I );
If (varArray [I] <'0' | varArray [I]> '9') & (I! = 17 ))
{
Return false;
}
Else if (I <17)
{
VarArray [I] = varArray [I] * factorArr [I];
}
}
If (idNumber. length = 18)
{
Var date8 = idNumber. substring (6, 14 );
If (checkDate (date8) = false)
{
Return false;
}
For (I = 0; I <17; I ++)
{
LngProduct = lngProduct + varArray [I];
}
IntCheckDigit = 12-lngProduct % 11;
Switch (intCheckDigit)
{
Case 10:
IntCheckDigit = 'X ';
Break;
Case 11:
IntCheckDigit = 0;
Break;
Case 12:
IntCheckDigit = 1;
Break;
}
If (varArray [17]. toUpperCase ()! = IntCheckDigit)
{
Return false;
}
}
Else
{
Var date6 = idNumber. substring (6, 12 );
If (checkDate (date6) = false)
{
Return false;
}
}
Return true;
}

The common string judgment functions are basically covered. You can choose among them based on your project requirements. If you have any omissions, please leave a message for me. This article continues to be updated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.