JavaScript Common function Library

Source: Internet
Author: User
Tags date define empty expression string split trim valid
javascript| function

/*

--------------function Retrieval--------------
Trim function:                          trim () LTrim () RTrim ()
Verify that the string is empty:                 Checkisnotempty (str)
Verify that the string is an integral type:                Checkisinteger (str)
Verify integer minimum:                     Checkintegerminvalue (str,val)
Verify integer maximum:                     Checkintegermaxvalue (str,val)
Verify that the integer is Non-negative:                Isnotnegativeinteger (str)
Verify that the string is a floating-point type:              CHECKISDOuble (str)
Verify floating-point minimum value:                   Checkdoubleminvalue (str,val)
Verify floating-point size maximum:                   Checkdoublemaxvalue (str,val)
Verify that the floating-point type is non-negative:              isnotnegativedouble (str)
Verify that the string is a date type:              checkisvaliddate (str)
Verify successively:  of two dates                Checkdateearlier (Strstart, Strend)
Verify that the string is an email type:           checkemail (str)

Verify that the string is Chinese: checkischinese (str)
Calculates the length of a string, a character two characters: Reallength ()
Verify that the string conforms to a custom regular expression: Checkmask (Str,pat)
Get file suffix name: Getfilepostfix (ofile)
--------------function Retrieval--------------
*/

/**


* Added by Lxcjie 2004.6.25


* Remove extra space function


* Trim: Remove both sides of the space LTrim: Remove the left space RTrim: Remove the right space


* Usage:


* var str = "Hello";


* str = Str.trim ();


*/


String.prototype.trim = function ()


{


return This.replace (^[\\s]*) | ( [\\s]*$)/g, "");


}


String.prototype.lTrim = function ()


{


Return This.replace (/(^[\\s]*)/g, "");


}


String.prototype.rTrim = function ()


{


return This.replace ([\\s]*$)/g, "");


}


/********************************** Empty **************************************/


/**


* Verify that the string is empty


* return value:


* If not NULL, defines a checksum pass, returns True


* If NULL, checksum does not pass, returns false reference information: the input field cannot be empty!


*/


function Checkisnotempty (str)


{


if (Str.trim () = "")


return false;


Else


return true;


}//~~~


* *---------------------------------Empty--------------------------------------* *


/********************************** Integer *************************************/


/**


* Verify that the string is an integral type


* return value:


* If NULL, define checksum pass, return True


* If the string is all numbers, checksum passes, returns True


* If the checksum does not pass, return false reference information: the input field must be a number!


*/


function Checkisinteger (str)


{


//If empty, pass the checksum


if (str = "")


return true;


if (/^ (\\-?) (\\d+) $/.test (str))


return true;


Else


return false;


}//~~~


/**


* Check integer minimum


*str: The string to verify. Val: The value of the comparison


*


* return value:


* If NULL, define checksum pass, return True


* If the condition is satisfied, greater than or equal to the given value, validation passes, returns True


* If less than the given value, returns false reference information: the input field cannot be less than the given value!


*/


function Checkintegerminvalue (str,val)


{


//If empty, pass the checksum


if (str = "")


return true;


if (typeof (Val)!= "string")


val = val + "";


if (Checkisinteger (str) = = true)


    {


if (parseint (str,10) >=parseint (val,10))


return true;


Else


return false;


    }


Else


return false;


}//~~~


/**


* The maximum value of the check integer


*str: The string to verify. Val: The value of the comparison


*


* return value:


* If NULL, define checksum pass, return True


* If the condition is satisfied, less than or equal to the given value, validation passes and returns True


* If greater than the given value, returns false reference information: the input value cannot be greater than the given value!


*/


function Checkintegermaxvalue (str,val)


{


//If empty, pass the checksum


if (str = "")


return true;


if (typeof (Val)!= "string")


val = val + "";


if (Checkisinteger (str) = = true)


    {


if (parseint (str,10) <=parseint (val,10))


return true;


Else


return false;


    }


Else


return false;


}//~~~


/**


* Verify that the integral type is non-negative


*str: The string to verify.


*


* return value:


* If NULL, define checksum pass, return True


* Returns True if not a negative number


* If a negative number, return false reference information: the input value can not be negative!


*/


function Isnotnegativeinteger (str)


{


//If empty, pass the checksum


if (str = "")


return true;


if (Checkisinteger (str) = = true)


    {


if (parseint (str,10) < 0)


return false;


Else


return true;


    }


Else


return false;


}//~~~


/*---------------------------------Integer--------------------------------------/*


/********************************** Double ****************************************/


/**


* Verify that the string is a floating-point type


* return value:


* If NULL, define checksum pass, return True


* If the string is a floating-point type, the checksum passes and returns True


* If the checksum does not pass, return false reference information: the input field is not a legitimate floating-point number!


*/


function checkisdouble (str)


{


//If empty, pass the checksum


if (str = "")


return true;


//If it is an integer, verify the validity of the integer


if (Str.indexof (".") = = 1)


    {


if (Checkisinteger (str) = = true)


return true;


Else


return false;


    }


Else


    {


if (/^ (\\-?) (\\d+) (. {1}) (\\d+) $/g.test (str))


return true;


Else


return false;


    }


}//~~~


/**


* Check floating-point minimum value


*str: The string to verify. Val: The value of the comparison


*


* return value:


* If NULL, define checksum pass, return True


* If the condition is satisfied, greater than or equal to the given value, validation passes, returns True


* If less than the given value, returns false reference information: the input field cannot be less than the given value!


*/


function Checkdoubleminvalue (str,val)


{


//If empty, pass the checksum


if (str = "")


return true;


if (typeof (Val)!= "string")


val = val + "";


if (checkisdouble (str) = = true)


    {


if (parsefloat (str) >=parsefloat (val))


return true;


Else


return false;


    }


Else


return false;


}//~~~


/**


* Check floating-point maximum value


*str: The string to verify. Val: The value of the comparison


*


* return value:


* If NULL, define checksum pass, return True


* If the condition is satisfied, less than or equal to the given value, validation passes and returns True


* If greater than the given value, returns false reference information: the input value cannot be greater than the given value!


*/


function Checkdoublemaxvalue (str,val)


{


//If empty, pass the checksum


if (str = "")


return true;


if (typeof (Val)!= "string")


val = val + "";


if (checkisdouble (str) = = true)


    {


if (parsefloat (str) <=parsefloat (val))


return true;


Else


return false;


    }


Else


return false;


}//~~~


/**


* Verify that the floating-point type is non-negative


*str: The string to verify.


*


* return value:


* If NULL, define checksum pass, return True


* Returns True if not a negative number


* If a negative number, return false reference information: the input value can not be negative!


*/


function isnotnegativedouble (str)


{


//If empty, pass the checksum


if (str = "")


return true;


if (checkisdouble (str) = = true)


    {


if (parsefloat (str) < 0)


return false;


Else


return true;


    }


Else


return false;


}//~~~


/*---------------------------------Double---------------------------------------/*


/********************************** Date ******************************************/


/**


* Verify that the string is a date type


* return value:


* If NULL, define checksum pass, return True


* If the string is a date type, the checksum passes and returns True


* If the date is not valid, return false reference information: the time of the input domain is not legal! (YYYY-MM-DD)


*/


function Checkisvaliddate (str)


{


//If empty, pass the checksum


if (str = "")


return true;


var pattern =/^ ((\\d{4}) | ( \\D{2}))-(\\d{1,2})-(\\d{1,2}) $/g;


if (!pattern.test (str))


return false;


var arrdate = Str.split ("-");


if (parseint (arrdate[0],10) < 100)


Arrdate[0] = Watts + parseint (arrdate[0],10) + "";


var date = new Date (Arrdate[0], (parseint (arrdate[1],10)-1) + "", arrdate[2]);


if (date.getyear () = = Arrdate[0]


&& date.getmonth () = = (parseint (arrdate[1],10)-1) + ""


&& date.getdate () = = Arrdate[2])


return true;


Else


return false;


}//~~~


/**


* Verification of two dates of the successively


* return value:


* If one of the dates is empty, the checksum passes and returns True


* If the start date is earlier than or equal to the expiration date, the validation passes and returns True


* If the start date is later than the end date, return false reference information: The start date cannot be later than the end date.


*/


function Checkdateearlier (strstart,strend)


{


if (checkisvaliddate (strstart) = = False | | checkisvaliddate (strend) = False)


return false;


//If one of the inputs is empty, pass the check


if (Strstart = "") | | (Strend = ""))


return true;


var arr1 = strstart.split ("-");


var arr2 = strend.split ("-");


var date1 = new Date (Arr1[0],parseint (Arr1[1].replace (/^0/, ""),-1,arr1[2]);


var date2 = new Date (Arr2[0],parseint (Arr2[1].replace (/^0/, ""),-1,arr2[2]);


if (arr1[1].length = 1)


arr1[1] = "0" + arr1[1];


if (arr1[2].length = 1)


arr1[2] = "0" + arr1[2];


if (arr2[1].length = 1)


arr2[1] = "0" + arr2[1];


if (arr2[2].length = 1)


arr2[2]= "0" + arr2[2];


var D1 = arr1[0] + arr1[1] + arr1[2];


var d2 = arr2[0] + arr2[1] + arr2[2];


if (parseint (d1,10) > parseint (d2,10))


return false;


Else


return true;


}//~~~


/*---------------------------------date-----------------------------------------/*


/********************************** Email *****************************************/


/**


* Verify that the string is email type


* return value:


* If NULL, define checksum pass, return True


* If the string is an email type, check pass, return True


* If the email is not valid, return FALSE reference message: The format of the email is not correct!


*/


function Checkemail (str)


{


//If empty, pass the checksum


if (str = "")


return true;


if (Str.charat (0) = = "." | | Str.charat (0) = "@" | | str.indexof ( \ ' @\ ' , 0) =-1


        || Str.indexof (\ '. \ ', 0) = = 1 | | Str.lastindexof ("@") = = Str.length-1 | | Str.lastindexof (".") = = str.length-1)


return false;


Else


return true;


}//~~~


/*---------------------------------Email----------------------------------------/*


/********************************** Chinese ***************************************/


/**


* Verify that the string is Chinese


* return value:


* If NULL, define checksum pass, return True


* If the string is Chinese, the checksum passes and returns True


* If the string is not Chinese, return false reference message: Must be Chinese!


*/


function Checkischinese (str)


{


//If the value is null, pass the checksum


if (str = "")


return true;


var pattern =/^ ([\\u4e00-\\u9fa5]|[ \\UFE30-\\UFFA0]) *$/gi;


if (pattern.test (str))


return true;


Else


return false;


}//~~~


/**


* Computes the length of the string, a character of two characters


*/


String.prototype.realLength = function ()


{


return This.replace (/[^\\x00-\\xff]/g, "* *"). Length;


}


* *---------------------------------Chinese--------------------------------------* *


/********************************** Mask ***************************************/


/**


* Verify that the string conforms to a custom regular expression


*str to verify the string Pat custom regular Expression


* return value:


* If NULL, define checksum pass, return True


* If string compliance, checksum pass, returns True


* If the string does not conform, return FALSE reference message: Must satisfy * * * mode


*/


function Checkmask (str,pat)


{


//If the value is null, pass the checksum


if (str = "")


return true;


var pattern = new RegExp (Pat, "GI")


if (Pattern.test (str))


return true;


Else


return false;


}//~~~


/*---------------------------------mask--------------------------------------/*


/********************************** file ***************************************/


/**


* Added by Lxcjie 2004.6.25


* Get file suffix name


* ofile is a file control object


*/


function Getfilepostfix (ofile)


{


if (ofile = null)


return null;


var pattern =/(. *) \. (. *) $/gi;


if (typeof (ofile) = = "Object")


    {


if (Ofile.value = null | | ofile.value = = "")


return null;


var arr = pattern.exec (ofile.value);


return regexp.$2;


    }


else if (typeof (ofile) = = "string")


    {


var arr = pattern.exec (ofile);


return regexp.$2;


    }


Else


return null;


}//~~~


/*---------------------------------file--------------------------------------/*








Related Article

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.