Some common Javascript judgment functions _ basic knowledge

Source: Internet
Author: User
Some common Javascript judgment functions are basic js knowledge. If you are interested in js, You can see 1. This is an DIV.


OuterHTML
InnerHTML
InnertText
2. Javascript Error

The Code is as follows:


W1 = 0;
W1 + = Math. round (1333333.99*100 );
W1 + = Math. round (13663.38*100 );
Document. write (w1/100 );
W1 = 0;
W1 ++ = 1333333.99;
W1 ++ = 13663.38;
Document. write (w1 );


3. Form check

The Code is as follows:


// Wanghui added a function checkPort () to check service access and process address port numbers at 20031020 ();
Function toChnDigit (num)
{
Var t = parseInt (num );
If (t = 0) return "0 ";
If (t = 1) return "1 ";
If (t = 2) return "2 ";
If (t = 3) return "3 ";
If (t = 4) return "4 ";
If (t = 5) return "5 ";
If (t = 6) return "6 ";
If (t = 7) return "7 ";
If (t = 8) return "Eight ";
If (t = 9) return "Nine ";
Return "";
}
// @ CheckItem @ OPT-HuTie-20031208 optimization: Add a public function that shields all buttons
Function disableAllButtons (){
For (var I = 0; I Var tmp = document. all. tags ("input") [I];
If (tmp. type = "button" | tmp. type = "submit" | tmp. type = "reset "){
Tmp. disabled = true;
}
}
}
// Add the trim Function
String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
// Function name: checkNUM
// 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 checkNum (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: checkNUM
// 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 checkIntNum (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: checkEmail
// Function Description: Check whether the Email Address is used
// Parameter description: the string to be checked
// Return value: 0: Not 1: Yes
Function checkEmail (){
// @ CheckItem @ Bug141-hutie-20030821 modification interface: Email address to do prohibit Chinese Verification
Var reg =/[^ \ u0000-\ u00FF]/;
If (a. match (reg )! = Null ){
Return 0; // contains Chinese Characters
}
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;
}
// Function name: checkTEL
// 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 checkTel (tel)
{
Var I, j, strTemp;
StrTemp = "0123456789 -()";
For (I = 0; I {
J = strTemp. indexOf (tel. charAt (I ));
If (j =-1)
{
// The specified character is invalid.
Return 0;
}
}
// Valid description
Return 1;
}
// Function name: checkLength
// Function Description: Check the string length
// Parameter description: the string to be checked
// Return value: Length Value
Function checkLength (strTemp)
{
Var I, sum;
Sum = 0;
For (I = 0; I {
// @ CheckItem @ BUG-Renhj-20040604 optimization: Change the function for verification to 128 with the class as a single character. Avoid the "·" symbol
// If (strTemp. charCodeAt (I)> = 0) & (strTemp. charCodeAt (I) <= 255 ))
If (strTemp. charCodeAt (I)> = 0) & (strTemp. charCodeAt (I) <= 128 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
Return sum;
}
// Function name: checkSafe
// Function Description: Check whether "'",' "'," <","> ",";",","
// Parameter description: the string to be checked
// Return value: 0: Yes 1: No
Function checkSafe ()
{
Fiber DN = new Array ("'",' "',"> "," <",",",";");
I = maid. length;
J = a. length;
For (ii = 0; ii {
For (jj = 0; jj {
Temp1 = a. charAt (jj );
Temp2 = maid [ii];
If (temp1 = temp2)
{
Return 0;
}
}
}
Return 1;
}
// Function name: checkChar
// Function Description: Check whether it contains non-letter characters
// Parameter description: the string to be checked
// Return value: 0: containing 1: All letters
Function checkChar (str)
{
Var strSource = "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;
}
}
Return 1;
}
// Function name: checkCharOrDigital
// 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 checkCharOrDigital (str)
{
Var strSource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .()&";
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;
}
}
Return 1;
}
// Function name: checkChinese
// Function Description: Check whether Chinese characters are contained
// Parameter description: the string to be checked
// Return value: 0: Contains 1: No
Function checkChinese (strTemp)
{
Var I, sum;
For (I = 0; I {
If (strTemp. charCodeAt (I) <0) | (strTemp. charCodeAt (I)> 255 ))
Return 0;
}
Return 1;
}
// Function name: compareTime ()
// Function Description: Compare the time
// Parameter description: inyear, inmonth, benginDay, INH, and INM,
// EndYear end year, endMonth end month, endMonth end day, endH end hour, and endM end minute
// Return value: true indicates that the start time is later than the end time, and false indicates that the opposite is true.
Function compareTime (beginYear, beginMonth, benginDay, beginH, beginM, endYear, endMonth, endDay, endH, endM ){
Var date1 = new Date (beginYear, beginMonth-1, benginDay, beginH, beginM );
Var date2 = new Date (endYear, endMonth-1, endDay, endH, endM );
If (date1.getTime ()> date2.getTime ()){
Return false;
}
Return true;
}
// Function name: compareDate ()
// Function Description: Compare the date size
// Parameter description: inyear, inmonth, and benginDay
// End year, end month, end month
// Return value: 0: true indicates that the start time is later than the end time, and false indicates that the opposite is true.
Function compareDate (beginYear, beginMonth, benginDay, endYear, endMonth, endDay ){
Var date1 = new Date (beginYear, beginMonth-1, benginDay );
Var date2 = new Date (End year, endMonth-1, endDay );
If (date1.getTime ()> date2.getTime ()){
Return false;
}
Return true;
}
// Function name: checkUrl
// Function Description: Check whether the Url is valid
// Parameter description: the string to be checked
// Return value: true: valid; false: invalid.
Function checkURL (strTemp)
{
If (strTemp. length = 0) return false;
If (checkChinese (strTemp) = 0) return false;
If (strTemp. toUpperCase (). indexOf ("HTTP ://")! = 0 & strTemp. toUpperCase (). indexOf ("HTTPS ://")! = 0 ){
Return false;
}
Return true;
}
// @ CheckItem @ The OPT-Renhj-20030704 provides a public method to remove Spaces
// Clear spaces on the left
Function js_ltrim (deststr)
{
If (deststr = null) return "";
Var pos = 0;
Var retStr = new String (deststr );
If (retStr. lenght = 0) return retStr;
While (retStr. substring (pos, pos + 1) = "") pos ++;
RetStr = retStr. substring (pos );
Return (retStr );
}
// Clear spaces on the right
Function js_rtrim (deststr)
{
If (deststr = null) return "";
Var retStr = new String (deststr );
Var pos = retStr. length;
If (pos = 0) return retStr;
While (pos & retStr. substring (pos-1, pos) = "") pos --;
RetStr = retStr. substring (0, pos );
Return (retStr );
}
// Clear spaces on the left and right
Function js_trim (deststr)
{
If (deststr = null) return "";
Var retStr = new String (deststr );
Var pos = retStr. length;
If (pos = 0) return retStr;
RetStr = js_ltrim (retStr );
RetStr = js_rtrim (retStr );
Return retStr;
}
// Format the input date string, for example, "2003-9-12" output: "2003-09-12"
Function formatDateStr (inDate ){
If (inDate = null | inDate = "") return "";
Var beginDate = inDate. split ("-");
Var mYear = beginDate [0];
Var mMonth = beginDate [1];
Var mDay = beginDate [2];
MMonth = (mMonth. length = 1 )? ("0" + mMonth): mMonth );
MDay = (mDay. length = 1 )? ("0" + mDay): mDay );
Return mYear + "-" + mMonth + "-" + mDay;
}
// Added by wanghui 20031020 check the validity of the URL address port, which must be a number smaller than 65535
Function checkPort (inValue1, inValue2 ){
// Check the validity of the first parameter. If the second parameter is null, the first parameter indicates the 'service processing address' of the text message service'
If (inValue1! = Null & inValue1.value! = ""){
Var array1 = inValue1.value. split (":");
If (array1.length> = 4) {// if the length of an array separated by ':' is four, there are two ':', so it is invalid.
If (inValue2 = null) alert ("service processing address PORT format error ");
Else alert ("Incorrect business access address PORT format ");
InValue1.focus ();
Return false;
}
If (array1.length> = 3 ){
Port1 = array1 [2];
If (pos1 = port1.indexOf ("/")> = 0) {// check whether there is '/' behind the port. if yes, the port is used before this symbol.
Port1 = port1.substring (0, pos1)
}
If (port1 = "" | checkIntNum (port1) = 0 | port1> 65535 ){
If (inValue2 = null) alert ("the service processing address port must be a number and cannot be greater than 65535 ");
Else alert ("the business access address port must be a number and cannot be greater than 65535 ");
InValue1.focus ();
Return false;
}
}
}
// Check the validity of the second parameter.
If (inValue2! = Null & inValue2.value! = ""){
Var array2 = inValue2.value. split (":");
If (array2.length> = 4 ){
Alert ("service processing address PORT format error ");
InValue2.focus ();
Return false;
}
If (array2.length> = 3 ){
Port2 = array2 [2];
If (pos2 = port2.indexOf ("/")> = 0 ){
Port2 = port2.substring (0, pos2)
}
If (port2 = "" | checkIntNum (port2) = 0 | port2> 65535 ){
Alert ("the service processing address port must be a number and cannot be greater than 65535 ");
InValue2.focus ();
Return false;
}
}
}
Return true;
}
// Obj: Data Object
// DispStr: the error message is a string.
Function checkUrlValid (obj, dispStr)
{
If (obj = null)
{
Alert ("the input object is empty ");
Return false;
}
Var str = obj. value;
Var urlpatern0 =/^ https? : \/. + $/I;
If (! Urlpatern0.test (str ))
{
Alert (dispStr + "invalid: it must start with 'HTTP: \/'or 'https! ");
Obj. focus ();
Return false;
}
Var urlpatern2 =/^ https? : \/([A-zA-Z0-9 _-]) + (\.)?) * (: \ D + )?. + $/I;
If (! Urlpatern2.test (str ))
{
Alert (dispStr + "the port number must be a number and must be between 1! ");
Obj. focus ();
Return false;
}
Var urlpatern1 =/^ https? : \/([A-zA-Z0-9 _-]) + (\.)?) * (: \ D + )? (\/((\.)? (\?)? =? &? [A-zA-Z0-9 _-] (\?) *) * $/I;
If (! Urlpatern1.test (str ))
{
Alert (dispStr + "illegal, please check! ");
Obj. focus ();
Return false;
}
Var s = "0 ";
Var t = 0;
Var re = new RegExp (": \ d +", "ig ");
While (arr = re.exe c (str ))! = Null)
{
S = str. substring (RegExp. index + 1, RegExp. lastIndex );
If (s. substring (0, 1) = "0 ")
{
Alert (dispStr + "the port number cannot start with 0! ");
Obj. focus ();
Return false;
}
T = parseInt (s );
If (t <1 || t> 65535)
{
Alert (dispStr + "the port number must be a number and must be between 1! ");
Obj. focus ();
Return false;
}
}
Return true;
}
// Function name: checkVisibleEnglishChr
// Function Description: Check whether the display is English (! "# $ % & '() * +,-./0123456789:; <=>? @ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _ 'abcdefghijklmnopqrstuvwxyz {| }~)
// Parameter description: the string to be checked
// Return value: true | false
// Add by renhj 2004.01.05
Function checkVisibleEnglishChr (strTemp)
{
Var I;
For (I = 0; I {
If (strTemp. charCodeAt (I) <32) | (strTemp. charCodeAt (I)> 126 ))
Return false;
}
Return true;
}
// Function name: checkInputChr
// Function Description: Check whether non-Input characters are contained
// Parameter description: the string to be checked
// Return value: false: Contains true: All Input characters
// Add by renhj 2004.01.05
Function checkInputChr (str)
{
Var notinput = "\" '<> ";
Var I;
For (I = 0; notinput! = Null & I <notinput. length; I ++ ){
If (str. indexOf (notinput. charAt (I)> = 0) {// if any
Return false;
}
}
Return true;
}
// Function name: checkTelExt
// Function Description: Check whether the phone number is used and the phone number is not assigned.
// Parameter description: the string to be checked
// Return value: 1 is valid, 0 is invalid
Function checkTelExt (tel)
{
Var I, count, isNumber;
Count = 0; // There are several consecutive numeric strings
IsNumber = 0; // not a number
For (I = 0; I {
// Determine whether the current number is used
If (checkIntNum (tel. charAt (I) = 1)
{
If (isNumber = 0)
{
Count = count + 1;
}
IsNumber = 1;
} Else {
IsNumber = 0;
}
If (count> 2)
{
// It indicates that the character is invalid or the location number exists.
Return 0;
}
}
If (checkIntNum (tel. charAt (0) = 1 )&&
(CheckIntNum (tel. charAt (tel. length-1) = 1 )){
// Valid description
Return 1;
} Else {
// The specified character is invalid.
Return 0;
}
}
// Function name: checkFormdata
// Function Description: Check the Form object
// Parameter description:
// Obj: the object to be checked,
// Name: the Chinese name of the object to be checked,
// Length: the length of the object to be checked (<0 is not checked ),
// Notnull: if it is true, the check is not empty,
// If notSpecChar is true, check whether there are any special characters,
// If notChinessChar is set to true, check whether there are any Chinese characters,
// NumOrLetter: true indicates that only numbers or English letters are allowed,
// If pNumber is true, the check can only be a positive integer,
// Return value: false: Check failed. true: all are Input characters.
// Add by renhj 2004.03.19
// @ CheckItem @ BUG: 1641: 718-Renhj-20040902-Add5 modify the information of the verification number
Function checkFormdata (obj, name, length, notnull, notSpecChar, notChinessChar, numOrLetter, pNumber ){
// Check object
If (! Obj) {alert ("the target is not an object. Processing failed! "); Return false ;}
Var msg;
Var ilen;
// Detect Chinese Characters
If (notChinessChar & (checkChinese (obj. value )! = 1 )){
Msg = name + "cannot contain Chinese characters! ";
Alert (msg );
Obj. focus ();
Return false;
}
// Detect special characters
If (notSpecChar &&(! CheckInputChr (obj. value ))){
Var notinput = "\" '<> ";
Msg = name + "illegal characters (" + notinput + ")! ";
Alert (msg );
Obj. focus ();
Return false;
}
// Check the length
If (length> = 0 & (checkLength (obj. value)> length )){
Ilen = length/2;
If (pNumber ){
Msg = name + "cannot exceed" + length + "number! ";
} Else if (notChinessChar ){
Msg = name + "cannot exceed" + length + "English! ";
} Else {
Msg = name + "cannot exceed" + length + "English letters or" + ilen + "Chinese characters! ";
}
Alert (msg );
Obj. focus ();
Return false;
}
// Detect non-empty
If (notnull & obj. value = ""){
Msg = "enter" + name + "! ";
Alert (msg );
Obj. focus ();
Return false;
}
// The detection can only contain numbers or English letters
Re =/[\ W _]/;
If (numOrLetter&&re.exe c (obj. value )){
Msg = name + "can only be numbers or English letters! ";
Alert (msg );
Obj. focus ();
Return false;
}
// The detection can only be a positive integer
Re =/[\ D _]/;
If (pnumber&re.exe c (obj. value )){
Msg = name + "can only be a positive integer! ";
Alert (msg );
Obj. focus ();
Return false;
}
Return true;
}

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.