Some useful Javascript script Blocks

Source: Internet
Author: User

It's taken from the Internet and transferred to another place. I don't know who the author is.

// Function name: chksafe
// Function Description: Check whether "'", "',"/"is included "'",'"',"/"
// Parameter description: the string to be checked
// Return value: 0: Yes 1: No
Function chksafe ()
{
Return 1;
/* Maid = new array ("'",""",",",",",";","/");
I = maid. length;
J = A. length;
For (II = 0; II <I; II ++)
{For (JJ = 0; JJ <j; JJ ++)
{Temp1 = A. charat (jj );
Temp2 = maid [II];
If (TEM '; P1 = temp2)
{Return 0 ;}
}
}
Return 1;
*/
}

// Function name: chkspc
// Function Description: Check whether space exists.
// Parameter description: the string to be checked
// Return value: 0: Yes 1: No
Function chkspc ()
{
VaR I = A. length;
VaR J = 0;
VaR K = 0;
While (k <I)
{
If (A. charat (k )! = "")
J = J + 1;
K = k + 1;
}
If (j = 0)
{
Return 0;
}
 
If (I! = J)
{Return 2 ;}
Else
{
Return 1;
}
}

// Function name: chkemail
// Function Description: Check whether the email address is used
// Parameter description: the string to be checked
// Return value: 0: Not 1: Yes
Function chkemail ()
{Var I = A. length;
VaR temp = A. indexof ('@');
VaR tempd = A. indexof ('.');
If (temp> 1 ){
If (I-Temp)> 3 ){

If (I-tempd)> 0 ){
Return 1;
}

}
}
Return 0;
}

// Opt1 decimal opt2 negative
// When opt2 is 1, check if num is negative.
// When opt1 is 1, check whether num is a decimal number.
// 1 is returned and 0 is incorrect.
Function chknbr (Num, opt1, opt2)
{
VaR I = num. length;
VaR staus;
// The number of staus records.
Status = 0;
If (opt2! = 1) & (Num. charat (0) = '-'))
{
// Alert ("You have enter a invalid number .");
Return 0;
 
}
// An error occurs when the last digit is.
If (Num. charat (I-1) = '.')
{
// Alert ("You have enter a invalid number .");
Return 0;
}

For (j = 0; j <I; j ++)
{
If (Num. charat (j) = '.')
{
Status ++;
}
If (status> 1)
{
// Alert ("You have enter a invalid number .");
Return 0;
}
If (Num. charat (j) <'0' | num. charat (j)> '9 ')
{
If (opt1 = 0) | (Num. charat (j )! = '.') & (J! = 0 ))
{
// Alert ("You have enter a invalid number .");
Return 0;
}
}
}
Return 1;
}

// Function name: chkdate
// Function Description: Check whether the date is used
// Parameter description: the string to be checked
// Return value: 0: Not date 1: Date
Function chkdate (datestr)
{
VaR lthdatestr
If (datestr! = "")
Lthdatestr = datestr. length;
Else
Lthdatestr = 0;

VaR tmpy = "";
VaR tmpm = "";
VaR tmpd = "";
// Var datestr;
VaR status;
Status = 0;
If (lthdatestr = 0)
Return 0

For (I = 0; I <lthdatestr; I ++)
{If (datestr. charat (I) = '-')
{
Status ++;
}
If (status> 2)
{
// Alert ("invalid format of date! ");
Return 0;
}
If (status = 0) & (datestr. charat (I )! = '-'))
{
Tmpy = tmpy + datestr. charat (I)
}
If (status = 1) & (datestr. charat (I )! = '-'))
{
Tmpm = tmpm + datestr. charat (I)
}
If (status = 2) & (datestr. charat (I )! = '-'))
{
Tmpd = tmpd + datestr. charat (I)
}

}
Year = new string (tmpy );
Month = new string (tmpm );
Day = new string (tmpd)
// Tempdate = new string (Year + month + Day );
// Alert (tempdate );
If (tmpy. length! = 4) | (tmpm. length> 2) | (tmpd. length> 2 ))
{
// Alert ("invalid format of date! ");
Return 0;
}
If (! (1 <= month) & (12> = month) & (31> = day) & (1 <= day )))
{
// Alert ("invalid month or day! ");
Return 0;
}
If (! (Year % 4) = 0) & (month = 2) & (Day = 29 ))
{
// Alert ("This is not a leap year! ");
Return 0;
}
If (month <= 7) & (month % 2) = 0) & (day> = 31 ))
{
// Alert ("this month is a small month! ");
Return 0;
 
}
If (month> = 8) & (month % 2) = 1) & (day> = 31 ))
{
// Alert ("this month is a small month! ");
Return 0;
}
If (month = 2) & (Day = 30 ))
{
// Alert ("The febryary never has this day! ");
Return 0;
}
 
Return 1;
}

// Function name: fucpwdchk
// Function Description: Check whether there are non-numbers or letters
// Parameter description: the string to be checked
// Return value: 0: containing 1: All numbers or letters
Function fucpwdchk (STR)
{
VaR strsource = "0123456789 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";
VaR ch;
VaR I;
VaR temp;

For (I = 0; I <= (Str. Length-1); I ++)
{

Ch = Str. charat (I );
Temp = strsource. indexof (CH );
If (temp =-1)
{
Return 0;
}
}
If (strsource. indexof (CH) =-1)
{
Return 0;
}
Else
{
Return 1;
}
}

Function jtrim (STR)
{While (Str. charat (0) = "")
{STR = Str. substr (1 );}
While (Str. charat (Str. Length-1) = "")
{STR = Str. substr (0, str. Length-1 );}
Return (STR );
}

// Function name: fucchecknum
// Function Description: Check whether it is a number
// Parameter description: number to be checked
// Return value: 1 is a number, and 0 is not a number
Function fucchecknum (Num)
{
VaR I, j, strtemp;
Strtemp = "0123456789 ";
If (Num. Length = 0)
Return 0
For (I = 0; I <num. length; I ++)
{
J = strtemp. indexof (Num. charat (I ));
If (j =-1)
{
// It indicates that the character is not a number.
Return 0;
}
}
// The description is a number.
Return 1;
}

// Function name: fucchecktel
// Function Description: Check whether the phone number is used
// Parameter description: the string to be checked
// Return value: 1 is valid, 0 is invalid
Function fucchecktel (TEL)
{
VaR I, j, strtemp;
Strtemp = "0123456789 -()#";
For (I = 0; I <Tel. length; I ++)
{
J = strtemp. indexof (Tel. charat (I ));
If (j =-1)
{
// The specified character is invalid.
Return 0;
}
}
// Valid description
Return 1;
}

// Function name: fucchecklength
// Function Description: Check the string length
// Parameter description: the string to be checked
// Return value: Length Value
Function fucchecklength (strtemp)
{
VaR I, sum;
Sum = 0;
For (I = 0; I <strtemp. length; I ++)
{
If (strtemp. charcodeat (I)> = 0) & (strtemp. charcodeat (I) <= 255 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
Return sum;
}

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.