Common js tools

Source: Internet
Author: User

SUMMARY:
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) compares whether the two strings are equal
Function compute signorecase (str1, str2) ignores the case when comparing the two strings to see if they are equal
Function isChinese (str) determines whether it is Chinese
Function isEmail (strEmail) email?
Function isImg (str) is an image file jpg | jpeg | swf | gif
Function isInteger (str) is an integer
Whether function isFloat is a floating point number
Function isPost (str) is the zip code (one to six digits)
Function isMobile (str) is the mobile phone number
Function isPhone (str) is a telephone number that must contain a zone number and can contain an extension number.
Function isQQ (str) is a valid QQ number?
Whether function isIP (str) is a valid IP Address
Function isDate (str) is a date type (for example, 2005-12-12)
Function isIdCardNo (idNumber) is a valid ID card number
*/

/**
* Get the object based on the Object id
* Id: id of the object
*/
Function obj $ (id)
{
Return document. getElementById (id );
}

/**
* Obtain the object Value Based on the Object id.
* Id: id of the object
*/
Function val $ (id)
{
Var obj = document. getElementById (id );
If (obj! = Null)
{
Return obj. value;
}
Return null;
}

/**
* Remove the leading and trailing spaces of the string.
* Str: string to remove Spaces
*/
Function trim (str)
{
Return str. replace (/(^ \ s *) | (\ s * $)/g ,'');
}

/**
* Remove the spaces before the string.
* Str: string to remove Spaces
*/
Function ltrim (str)
{
Return str. replace (/^ \ s */g ,'');
}

/**
* Remove the space after the string
* Str: string to remove Spaces
*/
Function rtrim (str)
{
Return str. replace (/\ s * $ /,'');
}

/**
* Whether the string has a value
* Str: string to be checked
*/
Function isEmpty (str)
{
If (str! = Null & str. length> 0)
{
Return true;
}
Return false;
}

/**
* Compare the two strings to see if they are equal.
* Str1: String 1 to be compared
* Str2: String 2 to be compared
*/
Function equals (str1, str2)
{
If (str1 = str2)
{
Return true;
}
Return false;
}

/**
* Case-insensitive comparison between two strings
* Str1: String 1 to be compared
* Str2: String 2 to be compared
*/
Function compute signorecase (str1, str2)
{
If (str1.toUpperCase () = str2.toUpperCase ())
{
Return true;
}
Return false;
}

/**
* Whether it is Chinese
* Str: string to be checked
*/
Function isChinese (str)
{
Var str = str. replace (/(^ \ s *) | (\ s * $)/g ,'');
If (! (/^ [\ U4E00-\ uFA29] * $/. test (str)
&&(! /^ [\ UE7C7-\ uE7F3] * $/. test (str ))))
{
Return false;
}
Return true;
}

/**
* Whether it is Email
* Str: string to be checked
*/
Function isEmail (str)
{
If (/^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ /. test (str ))
{
Return true
}
Return false;
}

/**
* Whether it is an image file
* Str: file name to be tested
*/
Function isImg (str)
{
Var objReg = new RegExp ("[.] + (jpg | jpeg | swf | gif) $", "gi ");
If (objReg. test (str ))
{
Return true;
}
Return false;
}

/**
* Whether it is an integer
* Str: string to be checked
*/
Function isInteger (str)
{
If (/^ -? \ D + $/. test (str ))
{
Return true;
}
Return false;
}

/**
* Whether it is a floating point number
* Str: string to be checked
*/
Function isFloat (str)
{
If (/^ (-? \ D +) (\. \ d + )? $/. Test (str)
{
Return true;
}
Return false;
}

/**
* Whether it is zip code
* Str: string to be checked
*/
Function isPost (str)
{
If (/^ \ d {1, 6} $/. test (str ))
{
Return true;
}
Return false;
}

/**
* Whether it is a mobile phone number
* Str: string to be checked
*/
Function isMobile (str)
{
If (/^ 1 [35] \ d {9}/. test (str ))
{
Return true;
}
Return false;
}

/**
* Whether it is a telephone number
* Str: string to be checked
* The telephone number must have a zone number and a location number.
*/
Function isPhone (str)
{
If (/^ (0 [1-9] \ d {}-) \ d {} (-\ d })? /. Test (str ))
{
Return true;
}
Return false;
}

/**
* Is it a valid QQ number?
* Str: string to be checked
*/
Function isQQ (str ){
If (/^ \ d {5, 9} $/. test (str ))
{
Return true;
}
Return false;
}

/**
* Whether the IP address is valid
* Str: string to be checked
*/
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;
}

/**
* Whether it is a legal date
* Str: string to be checked
*/
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;
}

/**
* Whether it is an ID card
* Str: string to be checked
*/
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 <idNumber. length; 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;
}

Article Source: DIY tribe (http://www.diybl.com/course/1_web/javascript/jsjs/20081010/149679.html)

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.