Common simple JavaScript Functions

Source: Internet
Author: User

// Function name: strByteLength
// Function Description: returns the length of a string in bytes.
// Parameter description: string to be checked by str
// Return value: String Length
Function strByteLength (str)
{
Var I, sum;
Sum = 0;
For (I = 0; I <str. length; I ++)
{
If (str. charCodeAt (I)> = 0) & (str. charCodeAt (I) <= 255 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
Return sum;
}
// Function name: fucCheckLength
// Function Description: Check whether the form meets the specified length.
// Parameter description: The form object to be checked by obj
// Name Object name
// Length
// Return value: true (yes) or false (NO)
Function fucCheckLength (obj, name, length)
{
Var I, sum;
Sum = 0;
Var strTemp = obj. value;
For (I = 0; I <strTemp. length; I ++)
{
If (strTemp. charCodeAt (I)> = 0) & (strTemp. charCodeAt (I) <= 255 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
If (sum <= length)
{
Return true;
}
Else
{
Alert (name + "exceeds the specified length! The maximum length is "+ length +" characters (two Chinese characters )! ");
Obj. focus ();
Return false;
}
}
// Check whether the email is valid
Function checkEmail (Object)
{
Var pattern =/^ [.-_ A-Za-z0-9] + @ ([-_ A-Za-z0-9] + \.) + [A-Za-z0-9] {2, 3} $ /;
Var strValue = Object. value;
If (strValue. match (pattern) = null ){
Alert ("Email is invalid. Please enter it again! ");
Object. focus ();
Return false;
} Else {
Return true;
}
}
// Return the null interval function
Function Jtrim (str ){
Var I = 0;
Var len = str. length;
If (str = "") return (str );
J = len-1;
Flagbegin = true;
Flagend = true;
While (flagbegin = true & I <len ){
If (str. charAt (I) = ""){
I = I + 1;
Flagbegin = true;
} Else {
Flagbegin = false;
}
}
While (flagend = true & j> = 0 ){
If (str. charAt (j) = ""){
J = J-1;
Flagend = true;
} Else {
Flagend = false;
}
}
If (I> j) return ("")
Trimstr = str. substring (I, j + 1 );
Return trimstr;
}
// Function name: JtrimCn
// Function Description: Remove spaces before and after the string [including Chinese spaces]
// Parameter description: string to be operated by str
// Return value: the string with leading and trailing spaces [including Chinese spaces] is deleted.
Function JtrimCn (str ){
Var I = 0;
If (str = null | str = undefined ){
Return "";
}
Var len = str. length;
If (str = ""){
Return (str );
}
J = len-1;
Flagbegin = true;
Flagend = true;
While (flagbegin = true & I <len ){
If (str. charAt (I) = "" | str. charAt (I) = ""){
I = I + 1;
Flagbegin = true;
} Else {
Flagbegin = false;
}
}
While (flagend = true & j> = 0 ){
If (str. charAt (j) = "" | str. charAt (j) = ""){
J = J-1;
Flagend = true;
} Else {
Flagend = false;
}
}
If (I> j ){
Return ("")
}
Var trimstr = str. substring (I, j + 1 );
Return trimstr;
}
// 0-9, A-Z, a-z standard character judgment
Function isChar (Str ){
Var regu = "^ ([0-9a-zA-Z] +) $ ";
Var re = new RegExp (regu );
If (Str. search (re )! =-1 ){
Return true;
}
Return false;
}
// Determine whether the number is correct.
Function IsNum (Str ){
Var regu = "^ ([0-9] +) $ ";
Var re = new RegExp (regu );
If (Str. search (re )! =-1)
Return true;
{
Return false;
}
}
// Function name: funcIsNotEmpty
// Function Description: Check whether the string is empty
// Parameter description: str string
// Return value: true: Not empty; false: NULL
Function funcIsNotEmpty (str ){
Var s =/\ S /;
If (str = null ){
Return false;
}
Return s. test (str );
}
// Function name: fucCheckLength
// Function Description: Check whether the form meets the specified length.
// Parameter description: value of the form object to be checked by objValue
// Name Object name
// Min length of minLen
// MaxLen maximum length
// Return value: true (yes) or false (NO)
Function fucCheckLengthB (objValue, minLen, maxLen)
{
Var I, sum;
Sum = 0;
Var strTemp = objValue;
For (I = 0; I <strTemp. length; I ++)
{
If (strTemp. charCodeAt (I)> = 0) & (strTemp. charCodeAt (I) <= 255 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
If (sum <= maxLen & sum> = minLen)
{
Return true;
}
Else
{
Return false;
}
}
// SDate1 and sDate2 are in 2002-12-18 format
Function funDateDiff (sDate1, sDate2 ){
Var aDate, oDate1, oDate2, iDays;
ADate = sDate1.split ("-");
// Convert to the 12-18-2002 format
ODate1 = new Date (aDate [1] + '-' + aDate [2] + '-' + aDate [0]);
ADate = sDate2.split ("-");
ODate2 = new Date (aDate [1] + '-' + aDate [2] + '-' + aDate [0]);
// Converts the difference in milliseconds to the number of days
IDays = parseInt (Math. abs (oDate1-oDate2)/1000/60/60/24 );
// If the start time is earlier than the end time
If (sDate1> sDate2)
{
Return (-1 * iDays );
}
Return iDays;
}
// Check whether the email is valid
Function funcCheckEmail (strValue)
{
Var pattern =/^ [.-_ A-Za-z0-9] + @ ([-_ A-Za-z0-9] + \.) + [A-Za-z0-9] {2, 3} $ /;
If (strValue. match (pattern) = null ){
Return false;
} Else {
Return true;
}
}
// Function name: fucCheckMaxLength
// Function Description: Check whether the form meets the specified length.
// Parameter description: value of the form object to be checked by objValue
// Name object Value
// MaxLen maximum length
// Return value: true (yes) or false (NO)
Function fucCheckMaxLength (objValue, maxLen)
{
Return fucCheckLengthB (objValue, 0, maxLen );
}
// Function name: fucCheckMaxLength
// Function Description: Check whether the specified object value complies with the specified length.
// Parameter description: value of the form object to be checked by objValue
// Name object
// MaxLen maximum length
// Return value: true (yes) or false (NO)
Function fucCheckObjMaxLength (obj, maxLen)
{
If (obj = null ){
Return false;
}
Return fucCheckLengthB (obj. value, 0, maxLen );
}
// Function name: funcCheckInvalidChar
// Function Description: determines whether the specified field contains invalid characters. <>
// Parameter description: The form object to be checked by obj
// Return value: true (NO) or false (yes)
Function funcCheckInvalidChar (obj)
{
If (obj = null | obj. value = "")
{
Return true;
}
// Alert (obj. value );
Var pattern =/[<>]/;
If (pattern. test (obj. value )){
Return false;
} Else {
Return true;
}
}
/**
* Determines whether the maximum length of the object with the specified ID is correct.
* Param: objId Object ID
* MaxLength maximum length
* Return: true is correct. false is incorrect.
*/
Function lengthMaxCheckMsg (objId, maxLength, objName, needFocus, showMsg ){
// Check personal information
Var obj = document. getElementById (objId );
If (! FucCheckObjMaxLength (obj, maxLength )){
If (showMsg = null | showMsg = ""){
Alert (objName + "up to" + (maxLength/2) + "Chinese characters (or" + maxLength + "English digit )");
} Else {
Alert (showMsg );
}
If (needFocus ){
Obj. focus ();
}
Return false;
}
Return true;
}
/**
* Determines whether the specified ID object contains invalid characters.
* Param: objId Object ID
* Name of the objName object
* Does the needFocus need to be set?
* Error message displayed by showMsg
* Return: true is correct. false is incorrect.
*/
Function invalidCharCheckMsg (objId, objName, needFocus, showMsg ){
// Check personal information
Var obj = document. getElementById (objId );
If (! FuncCheckInvalidChar (obj )){
If (showMsg = null | showMsg = ""){
Alert (objName + 'cannot contain "<" or "> "');
} Else {
Alert (showMsg );
}
If (needFocus ){
Obj. focus ();
}
Return false;
}
Return true;
}
/**
* Determines whether the object of the specified ID is null.
* Param: objId Object ID
* Name of the objName object
* Does the needFocus need to be set?
* Error message displayed by showMsg
* Return: true is not null, and false is null.
*/
Function emptyCheckMsg (objId, objName, needFocus, showMsg ){
// Check personal information
Var obj = document. getElementById (objId );
If (! FuncIsNotEmpty (obj. value )){
If (showMsg = null | showMsg = ""){
Alert (objName + 'cannot be blank! ');
} Else {
Alert (showMsg );
}
If (needFocus ){
Obj. focus ();
}
Return false;
}
Return true;
}
/* Date calculation functions
* Date 2007-01-01
* Cnt 1 or-1
* Return 2007-01-02
*/
Function getDate (date, cnt ){
If (date. length! = 10 ){
Return "";
}
Var dateArray = date. split ("-")
If (dateArray = null ){
Return "";
}
Var temDate = new Date (dateArray [0], dateArray [1]-1, dateArray [2]);
Var newDate = Date. UTC (temDate. getYear (), temDate. getMonth (), temDate. getDate ())
NewDate = newDate + (cnt * 24x60*60*1000 );
NewDate = new Date (newDate );
Var month = newDate. getMonth () + 1;
Var day = newDate. getDate ();
If (Number (month) <10)
Month = "0" + month;
If (Number (day) <10)
Day = "0" + day;
Var retDate = newDate. getYear () + "-" + month + "-" + day;
Return retDate;
}

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.