JavaScript scripting examples of commonly used code
Related Tags:
/ *
-------------------------------------------------- -----------------------------
File name: check.js
Note: JavaScript scripts, used to check the Web form input data
Version: 1.0
* /
/ *
Uses: checking the ip address format
Input: strIP: ip address
Return: If proven true return, or else return false;
* /
Function isIP (strIP) (
If (isNull (strIP)) return false;
Var re = / ^ (\ d) \. (\ D) \. (\ D) \. (\ D) $ / g / / IP address match a regular expression
If (re.test (strIP))
(
If (RegExp. $ 1 <256 & RegExp. $ 2 <256 & RegExp. $ 3 <256 & RegExp. $ 4 <256) return true;
)
Return false;
)
/ *
Uses: Check whether the input string, or all of them are empty spaces
Input: str
Back to:
If all air returns true, otherwise returns false
* /
Function isNull (str) (
If (str == "") return true;
Var regu = "^ [] $";
Var re = new RegExp (regu);
Return re.test (str);
)
/ *
Uses: check whether the value of the importation of objects with integer format
Input: input string str
Return: If validated returns true, otherwise returns false
* /
Function isInteger (str) (
Var regu /^[-]{ 0,1) = [0-9] (1 ,}$/;
Return regu.test (str);
)
/ *
Uses: check whether the correct input phone numbers
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function checkMobile (s) (
Var regu = / ^ [1] [3] [0-9] (9) $ /;
Var re = new RegExp (regu);
If (re.test (s)) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: Check whether the input string with positive integer format
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function isNumber (s) (
Var regu = "^ [0-9] $";
Var re = new RegExp (regu);
If (s.search (re)! = -1) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: Check whether the input string with a small number of digital format, it could be negative
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function isDecimal (str) (
If (isInteger (str)) return true;
Var re /^[-]{ 0,1) = (\ d) [\] (\ d) $ /;
If (re.test (str)) (
If (RegExp. $ 1 == 0 & & RegExp. $ 2 == 0) return false;
Return true;
Else ()
Return false;
)
)
/ *
Uses: Check whether the input object with the value of the port format
Input: input string str
Return: If validated returns true, otherwise returns false
* /
Function isPort (str) (
Return (isNumber (str) & str <65536);
)
/ *
Uses: Check whether the input object with the value of E-Mail format
Input: input string str
Return: If validated returns true, otherwise returns false
* /
Function isEmail (str) (
Var myReg = /^[-_ A-Za-z0-9] @ ([_A-Za-z0-9] \.) [A-Za-z0-9] (2,3) $ /;
If (myReg.test (str)) return true;
Return false;
)
/ *
Uses: check with the amount of input format string
Decimal format defined as the positive zone, up to three decimal places
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function isMoney (s) (
Var regu = "^ [0-9] [\] [0-9] (0,3) $";
Var re = new RegExp (regu);
If (re.test (s)) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: Check whether the input string only in the English alphabet and numbers, and underscores composition
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function isNumberOr_Letter (s) (/ / determine whether a numeric or alphanumeric
Var regu = "^ [0-9a-zA-Z \ _] $";
Var re = new RegExp (regu);
If (re.test (s)) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: Check whether the input string only in the English alphabet and numbers.
Input:
S: string
Back to:
If proven true return, or else return false
* /
Function isNumberOrLetter (s) (/ / determine whether a numeric or alphanumeric
Var regu = "^ [0-9a-zA-Z] $";
Var re = new RegExp (regu);
If (re.test (s)) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: check whether only by the string of Chinese characters, letters, numbers
Input:
Value: string
Back to:
If proven true return, or else return false
* /
Function isChinaOrNumbOrLett (s) (/ / determine whether the Chinese characters, letters, numbers
Var regu = "^ [0-9a-zA-Z \ u4e00-\ u9fa5] $";
Var re = new RegExp (regu);
If (re.test (s)) (
Return true;
Else ()
Return false;
)
)
/ *
Uses: determine whether the date
Input: date: Date; fmt: Date format
Return: If validated returns true, otherwise returns false
* /
Function isDate (date, fmt) (
If (fmt == null) fmt = "yyyyMMdd";
Var yIndex = fmt.indexOf ( "yyyy");
If (yIndex ==- 1) return false;
Var year = date.substring (yIndex, yIndex 4);
Var mIndex = fmt.indexOf ( "MM");
If (mIndex ==- 1) return false;
Var month = date.substring (mIndex, mIndex 2);
Var dIndex = fmt.indexOf ( "dd");
If (dIndex ==- 1) return false;
Var day = date.substring (dIndex, dIndex 2);
If (! IsNumber (year) | | year> "2100" | | year < "1900") return false;
If (! IsNumber (month) | | month> "12" | | month < "01") return false;
If (day> getMaxDay (year, month) | | day < "01") return false;
Return true;
)
Function getMaxDay (year, month) (
If (month == 4 | | 6 month == | | 9 month == | | month == 11)
Return "30";
If (month == 2)
If (year% 4 == 0 & & year 0! = 0 | | year @ 0 == 0)
Return "29";
Else
Return "28";
Return "31";
)
/ *
Uses: whether a character string to the end of 2
Input: str1: string; str2: the string to be included
Return: If validated returns true, otherwise returns false
* /
Function isLastMatch (str1, str2)
(
Var index = str1.lastIndexOf (str2);
If (str1.length == index str2.length) return true;
Return false;
)
/ *
Uses: whether a character string to start 2
Input: str1: string; str2: the string to be included
Return: If validated returns true, otherwise returns false
* /
Function isFirstMatch (str1, str2)
(
Var index = str1.indexOf (str2);
If (index == 0) return true;
Return false;
)
/ *
Uses: a character string that contains 2
Input: str1: string; str2: the string to be included
Return: If validated returns true, otherwise returns false
* /
Function isMatch (str1, str2)
(
Var index = str1.indexOf (str2);
If (index ==- 1) return false;
Return true;
)
/ *
Uses: check the importation of beginning and ending date is correct, the date of the Rules for the two correct format
And end on schedule> = Start Date
Input:
StartDate: Start date, string
EndDate: an end as scheduled, string
Back to:
If proven true return, or else return false
* /
Function checkTwoDate (startDate, endDate) (
If (! IsDate (startDate)) (
Alert ( "Start date is not correct!");
Return false;
) Else if (! IsDate (endDate)) (
Alert ( "the date of termination incorrect!");
Return false;
) Else if (startDate> endDate) (
Alert ( "Start date is not greater than the date of termination.");
Return false;
)
Return true;
)
/ *
Uses: check imported Email address in the correct format
Input:
StrEmail: string
Back to:
If proven true return, or else return false
* /
Function checkEmail (strEmail) (
/ / Var emailReg = / ^ [_a-z0-9] @ ([_a-z0-9] \.) [A-z0-9] (2,3) $ /;
Var emailReg = / ^ [\ w-] (\. [\ W-]) * @ [\ w-] (\. [\ W-]) $ /;
If (emailReg.test (strEmail)) (
Return true;
Else ()
Alert ( "Your input format Email addresses incorrect!");
Return false;
)
)
/ *
Uses: check the importation of telephone numbers in the correct format
Input:
StrPhone: string
Back to:
If proven true return, or else return false
* /
Function checkPhone (strPhone) (
Var phoneRegWithArea = / ^ [0] [1-9] (2,3) - [0-9] (5,10) $ /;
Var phoneRegNoArea = / ^ [1-9] (1) [0-9] (4-5) $ /;
Var prompt = "you enter the phone number is not correct!"
If (strPhone.length> 9) (
If (phoneRegWithArea.test (strPhone)) (
Return true;
Else ()
Alert (prompt);
Return false;
)
Else ()
If (phoneRegNoArea.test (strPhone)) (
Return true;
Else ()
Alert (prompt);
Return false;
)
)
)
/ *
Uses: check the number is selected
Input:
CheckboxID: string
Back to:
Return to the box was selected in the number of
* /
Function checkSelect (checkboxID) (
Var check = 0;
Var i = 0;
If (document.all (checkboxID). Length> 0) (
For (i = 0; i <document.all (checkboxID). Length; i) (
If (document.all (checkboxID). Item (i). Checked) (
Check = 1;
)
)
Else ()
If (document.all (checkboxID). Checked)
Check = 1;
)
Return check;
)
Function getTotalBytes (varField) (
If (varField == null)
Return -1;
Var totalCount = 0;
For (i = 0; i <varField.value.length; i) (
If (varField.value.charCodeAt (i)> 127)
TotalCount = 2;
Else
TotalCount;
)
Return totalCount;
)
Function getFirstSelectedValue (checkboxID) (
Var value = null;
Var i = 0;
If (document.all (checkboxID). Length> 0) (
For (i = 0; i <document.all (checkboxID). Length; i) (
If (document.all (checkboxID). Item (i). Checked) (
Value = document.all (checkboxID). Item (i). Value;
Break;
)
)
Else ()
If (document.all (checkboxID). Checked)
Value = document.all (checkboxID). Value;
)
Return value;
)
Function getFirstSelectedIndex (checkboxID) (
Var value = 2;
Var i = 0;
If (document.all (checkboxID). Length> 0) (
For (i = 0; i <document.all (checkboxID). Length; i) (
If (document.all (checkboxID). Item (i). Checked) (
Value = i;
Break;
)
)
Else ()
If (document.all (checkboxID). Checked)
Value = 1;
)
Return value;
)
Function selectAll (checkboxID, status) (
If (document.all (checkboxID) == null)
Return;
If (document.all (checkboxID). Length> 0) (
For (i = 0; i <document.all (checkboxID). Length; i) (
Document.all (checkboxID). Item (i). Checked = status;
)
Else ()
Document.all (checkboxID). Checked = status;
)
)
Function selectInverse (checkboxID) (
If (document.all (checkboxID) == null)
Return;
If (document.all (checkboxID). Length> 0) (
For (i = 0; i <document.all (checkboxID). Length; i) (
Document.all (checkboxID). Item (i). Checked =! Document.all (checkboxID). Item (i). Checked;
)
Else ()
Document.all (checkboxID). Checked =! Document.all (checkboxID). Checked;
)
)
Function checkDate (value) (
If (value =='') return true;
If (value.length! = 8 | |! IsNumber (value)) return false;
Var year value.substring = (0,4);
If (year> "2100" | | year < "1900")
Return false;
Value.substring var month = (4,6);
If (month> "12" | | month < "01") return false;
Value.substring var day = (6,8);
If (day> getMaxDay (year, month) | | day < "01") return false;
Return true;
)
/ *
Uses: check the importation of beginning and ending date is correct, the rules for the two dates are correct format or is empty
And the end date> = Start Date
Input:
StartDate: Start date, string
EndDate: an end date, string
Back to:
If proven true return, or else return false
* /
Function checkPeriod (startDate, endDate) (
If (! CheckDate (startDate)) (
Alert ( "Start date is not correct!");
Return false;
) Else if (! CheckDate (endDate)) (
Alert ( "the date of termination incorrect!");
Return false;
) Else if (startDate> endDate) (
Alert ( "Start date is not greater than the date of termination.");
Return false;
)
Return true;
)
/ *
Uses: check whether the correct code Securities
Input:
SecCode: securities code
Back to:
If proven true return, or else return false
* /
Function checkSecCode (secCode) (
If (secCode.length! = 6) (
Alert ( "securities code length should be six");
Return false;
)
If (! IsNumber (secCode)) (
Alert ( "securities code can contain only numbers");
Return false;
)
Return true;
)
/************************************************* ***
Function: cTrim (sInputString, iType)
Description: string to a function of space
Parameters: iType: 1 = removed from the left side of the box strings
2 = removed from the left side of the box strings
0 = Remove string and the box to the right of the left
Return value: Remove the string space
************************************************** ** /
Function cTrim (sInputString, iType)
(
Var sTmpStr = '';
Var i = 1;
If (iType == 0 | | iType == 1)
(
While (sTmpStr == '')
(
I;
STmpStr = sInputString.substr (i, 1);
)
SInputString = sInputString.substring (i);
)
If (iType == 0 | | iType == 2)
(
STmpStr = '';
I = sInputString.length;
While (sTmpStr == '')
(
-- I;
STmpStr = sInputString.substr (i, 1);
)
SInputString.substring sInputString = (0, i 1);
)
Return sInputString;
)
/ *
-------------------------------------------------- -----------------------------
Note: JavaScript scripts, verification of the data in the form begin
-------------------------------------------------- -----------------------------
* /
Function checkForm (objFrm) (
Var len = 0;
Len = objFrm.elements.length;
Var i = 0;
Var objCheck;
/ / Text box
For (i = 0; i <len; i) (
ObjFrm.elements objCheck = [i];
If (objCheck.type == "text" & &! F_checkTextValid (objCheck)) (
Return false;
)
)
/ / The drop-down box
For (i = 0; i <len; i) (
ObjFrm.elements objCheck = [i];
If (objCheck.type == "select-one" & &! F_checkSelectValid (objCheck)) (
Return false;
)
)
/ / Effective time
If (f_checkStartAndEndDate (objFrm) == false) return false;
Return true;
)
Function f_checkSelectValid (obj) (
/ / Alert ( "check select");
If (obj.options.length <= 0) (
Alert ( "drop-down box no data!");
Return false;
)
Return true;
)
Function f_checkStartAndEndDate (frm) (
Var len = frm.elements.length;
If (len == null & len == 0) return true;
Var i = 0;
Var temp;
Var objCheck;
Var objStartDate;
Var objEndDate;
/ / Alert ( "start date period check");
Try (
For (i = 0; i <len; i) (
Frm.elements objCheck = [i];
Temp = objCheck.name;
If (temp.indexOf ( "startDate")> 0 | | temp.indexOf ( "beginDate")> 0)
ObjStartDate = objCheck;
If (temp.indexOf ( "endDate")> 0)
ObjEndDate = objCheck;
)
/ / Alert (objStartDate.value);
/ / Alert (objEndDate.value);
If (objStartDate.value == null | | objStartDate.value == "" | | objStartDate.value == null | | objStartDate.value ==""){
Return true;
)
Return checkTwoDate (objStartDate.value, objEndDate.value);
/ / Alert ( "end date period check");
) Catch (E) ()
Return true;
)
Function f_checkTextValid (obj) (
/ / Can not be empty
If (obj.getAttribute ( "isNeed")! = Null) (
If (f_isNotNull (obj) == false) return false;
)
/ / Can not exceed the length of
If (obj.getAttribute ( "maxlength")! = Null) (
If (f_checkLength (obj) == false) return false;
)
Var checkType = "";
CheckType = obj.getAttribute ( "checkType");
If (checkType == null | | checkType =="") return true;
/ /
If (checkType.indexOf ( "number")> = 0) (
If (f_isNumber (obj) == false) return false;
If (f_checkNumType (obj, checkType) == false) return false;
)
/ /
If (checkType.indexOf ( "positive")> = 0) (
If (f_isNumber (obj) == false) return false;
If (f_isPositive (obj) == false) return false;
If (f_checkNumType (obj, checkType) == false) return false;
)
If (checkType.indexOf ( "date")> = 0) (
If (f_checkDate (obj) == false) return false;
)
/ *
Switch (checkType) (
Case "number": if (f_isNumber (obj) == false) return false; break;
Case "date": if (f_checkDate (obj) == false) return false; break;
Default:
)
* /
Return true;
)
Function f_isNotNull (obj) (
If (obj.value == "") (
F_alert (obj, "not to an empty value!");
Return false;
)
Return true;
)
Function f_isNumber (obj) (
If (isNaN (obj.value)) (
F_alert (obj, "should be numerical type");
Return false;
)
Return true;
)
Function f_checkDate (obj) (
If (checkDate (obj.value) == false) (
F_alert (obj, "is not a legitimate date format!");
Return false;
)
Return true;
)
Function f_checkLength (obj) (
If (getTotalBytes (obj)> Math.abs (obj.getAttribute ( "maxlength"))) (
F_alert (obj, "beyond the limit!");
Return false;
)
Return true;
)
Function f_alert (obj, alertStr) (
Var fielName = obj.getAttribute ( "fieldName");
If (fielName == null)
FielName = "";
Alert (fielName "\ n" alertStr);
Obj.select ();
Obj.focus ();
)
Function f_checkNumType (obj, numType) (
/ / Assumptions: digital type have been judged
Var strTemp;
Var numpric;
Var numLen;
Var strArr;
Var defaultLen = 19;
Var defaultpric = 5;
Try (
If (numType == null | | numType =="") return f_checkNumLenPrec (obj, defaultLen, defaultpric);
If (numType.indexOf ("(") <0 | | numType.indexOf (")") <0) return f_checkNumLenPrec (obj, defaultLen, defaultpric);
StrTemp = numType.substr (numType.indexOf ("(") 1, numType.indexOf (")") - numType.indexOf ("(") -1);
If (strTemp == null | | strTemp =="") return f_checkNumLenPrec (obj, defaultLen, defaultpric);
StrArr = strTemp.split (",");
NumLen = Math.abs (strArr [0]);
Numpric = Math.abs (strArr [1]);
Return f_checkNumLenPrec (obj, numLen, numpric);
) Catch (e) (
Alert ( "in f_checkNumType =" e);
Return f_checkNumLenPrec (obj, defaultLen, defaultpric);
)
)
Function f_checkNumLenPrec (obj, len, pric) (
Var numReg;
Var value = obj.value;
Var strValueTemp, strInt, strDec;
/ / Alert ("=====" pric value "=====" len);
Try (
NumReg = / [\ -] /;
StrValueTemp = value.replace (numReg, "");
StrValueTemp = strValueTemp.replace (numReg, "");
/ / Integer
If (pric == 0) (
NumReg = / [\.] /;
/ / Alert (numReg.test (value));
If (numReg.test (value) == true) (
F_alert (obj, "must be input integer type!");
Return false;
)
)
If (strValueTemp.indexOf (".") <0) (
/ / Alert ( "lennth ==" strValueTemp);
If (strValueTemp.length> (len - pric)) (
F_alert (obj, "integer of not more than" (len - pric) "-");
Return false;
)
Else ()
StrValueTemp.substr strInt = (0, strValueTemp.indexOf ("."));
/ / Alert ( "lennth ==" strInt);
If (strInt.length> (len - pric)) (
F_alert (obj, "integer of not more than" (len - pric) "-");
Return false;
)
StrValueTemp.substr strDec = ((strValueTemp.indexOf (".") 1), strValueTemp.length);
/ / Alert ( "pric ==" strDec);
If (strDec.length> pric) (
F_alert (obj, "decimals not exceed" pric "bit");
Return false;
)
)
Return true;
) Catch (e) (
Alert ( "in f_checkNumLenPrec =" e);
Return false;
)
)
Function f_isPositive (obj) (
Var numReg = / [\ -] /;
If (numReg.test (obj.value) == true) (
F_alert (obj, "to be positive!");
Return false;
)
Return true;
)
/ *
Function selectedCheckboxCount (form)
Features: Form options can be selected in the count
Parameters:
Form: the designated form
* /
Function selectedCheckboxCount (form) (
Var length = 0;
Var i = 0;
Var count = 0;
Eles = form.elements;
While (i <eles.length) (
Obj = eles.item (i);
/ / Type = obj.attributes.item ( "type"). NodeValue;
Type = obj.type;
If (type == "checkbox") (
If (obj.checked) (
Count;
)
)
I;
)
Return count;
)
/ / Get byte length
Function getByteLen (str)
(
Var l = str.length;
Var n = l;
For (var i = 0; i <l; i)
If (str.charCodeAt (i) <0 | | str.charCodeAt (i)> 255)
N = n 1;
Return n
)
/ *
Description:
1. Remove data in tabular form (0.0, and 0)
2. If cell in the absence of data, then automatically with a space
3. Eliminate the blank line checkbox
Parameters:
Clearzero: Does removed "0", "0.0", do not remove false, true cleared (default true)
Tablename: to remove the form name, default sortTable
* /
Function clear_table (clearzero, tablename)
(
Var tobject;
If (tablename == null)
Tobject = gmobj ( "sortTable");
Else
Tobject = gmobj (tablename);
/ / If the table undefined, not filtered
If (tobject == null)
Return;
/ / If the function call parameters for air, and that it is necessary to eliminate 0,0.0 the contrary, do not remove 0,0.0.
Var Clear = (clearzero == null)? True: clearzero;
/ / Remove 0,0.0, fill spaces
Var rows = tobject.rows;
Var j = 0;
For (var i = 0; i <rows.length; i)
(
/ / A first cell attributes clear, if it is 1, the bank said that there was no data, the bank removed all the data
While (tobject.rows [i]. Cells [j]! = Null)
(
If (clear)
(
If (tobject.rows [i]. Cells [j]. InnerHTML == 0 | | tobject.rows [i]. Cells [j]. InnerHTML = 0.0 | | tobject.rows [i]. Cells [j]. innerHTML =="")
Tobject.rows [i]. Cells [j]. InnerText = "";
)
Else
(
If (tobject.rows [i]. Cells [j]. InnerHTML =="")
Tobject.rows [i]. Cells [j]. InnerText = "";
)
J;
)
J = 0;
)
Return true;
)
Function gmobj (mtxt) / * Get object by object name * /
(
If (document.getElementById) (
M = document.getElementById (mtxt);
) Else if (document.all) (
M = document.all [mtxt];
) Else if (document.layers) (
M = document.layers [mtxt];
)
Return m;
)
/ *
-------------------------------------------------- -----------------------------
Note: JavaScript scripts, verify the data in the form of end
-------------------------------------------------- -----------------------------
* /
/ *
Uses: Check whether the input string with a small number of digital format, it could be negative (and meet the accuracy requirements)
Input: str: string
L: median
D: to return to the median after the decimal point:
If proven true return, or else return false
* /
Function isDecimal (str, l, d) (
If (isInteger (str)) (
If (l == null) return true;
If (str <0) l -;
If (str.length <= l) return true;
)
Var re /^[-]{ 0,1) = (\ d) [\] (\ d) $ /;
If (re.test (str)) (
If (l == null) return true;
If (d == null) d = 0;
If (RegExp. $ 1 == 0 & & RegExp. $ 2 == 0) return false;
If (RegExp. $ 1.length RegExp. 2.length $ <= l
& RegExp. 2.length $ <= d) return true;
)
Return false;
)
Onclick = "isNull ( 'Co.PageForm.CompetitorName');"
- DIV + CSS layout examples entry (directory)
- Page layout and design of the layout of basic knowledge
- DIV CSS layout examples: DIV CSS News Production
- DIV + CSS layout examples: column 2 column 3 of the layout examples (of Download)
- Examples explain examples of the attributes of flash
- DIV CSS layout examples entry (directory)
- 9 days: CSS layout of the first examples
- DIV CSS layout examples: column 2 column 3 of the layout examples (of Download)
- CSS layout examples: hidden text input
- WEB2.0 standards handbook: 9 days CSS layout of the first examples
- CSS layout appreciate: Taobao Home layout effect
- Div + css layout examples
- Common examples of CSS layout
- Div layout and Table layout
- Css layout
- CSS layout examples of excellent DIV + CSS navigation of the results!
- WEB standards handbook section nine days: CSS layout of the first examples
- DIV CSS layout examples: css layout of the site ten steps Record! (Directory)
- DIV CSS layout examples: + CSS DIV news produced List
- Div css layout of the general layout of paragraphs how?




