Javascript verification form part 2 _ javascript skills
Source: Internet
Author: User
The second part of the js verification form: JavaScript script, which verifies the data item begin in the form
-------------------------------------------------------------------------------
*/
Function checkForm (objFrm ){
Var len = 0;
Len = objFrm. elements. length;
Var I = 0;
Var objCheck;
// Text box
For (I = 0; I <len; I ++ ){
ObjCheck = objFrm. elements [I];
If (objCheck. type = "text "&&! F_checkTextValid (objCheck )){
Return false;
}
}
// Drop-down list
For (I = 0; I <len; I ++ ){
ObjCheck = objFrm. elements [I];
If (objCheck. type = "select-one "&&! F_checkSelectValid (objCheck )){
Return false;
}
}
// Valid time range
If (f_checkStartAndEndDate (objFrm) = false) return false;
Return true;
}
Function f_checkSelectValid (obj ){
// Alert ("check select ");
If (obj. options. length <= 0 ){
Alert ("no data in the drop-down list! ");
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 ++ ){
ObjCheck = frm. elements [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 ){
// Cannot be blank
If (obj. getAttribute ("isNeed ")! = Null ){
If (f_isNotNull (obj) = false) return false;
}
// The length cannot exceed
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, "null value is not allowed! ");
Return false;
}
Return true;
}
Function f_isNumber (obj ){
If (isNaN (obj. value )){
F_alert (obj, "Value Type ");
Return false;
}
Return true;
}
Function f_checkDate (obj ){
If (checkDate (obj. value) = false ){
F_alert (obj, "the date format is not legal! ");
Return false;
}
Return true;
}
Function f_checkLength (obj ){
If (getTotalBytes (obj)> Math. abs (obj. getAttribute ("maxlength "))){
F_alert (obj, "exceeds the length 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 ){
// Assume that the numeric type has been determined.
Var strTemp;
Var numpric;
Var numLen;
Var strArr;
Var defaultLen = 19;
Var defapripric = 5;
Try {
If (numType = null | numType = "") return f_checkNumLenPrec (obj, defaultLen, defaultpric );
If (numType. indexOf ("(") <0 | numType. indexOf (")") <0) return f_checkNumLenPrec (obj, defaultLen, defapripric );
StrTemp = numType. substr (numType. indexOf ("(") + 1, numType. indexOf (")")-numType. indexOf ("(")-1 );
If (strTemp = null | strTemp = "") return f_checkNumLenPrec (obj, defaultLen, defapripric );
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, defapripric );
}
}
Function f_checkNumLenPrec (obj, len, pric ){
Var numReg;
Var value = obj. value;
Var strValueTemp, strInt, strDec;
// Alert (value + "=" + len + "=" + pric );
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, "the input must be of the integer type! ");
Return false;
}
If (strDec. length> pric ){
F_alert (obj, "the decimal point cannot 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, "must be a positive number! ");
Return false;
}
Return true;
}
/*
Function selectedCheckboxCount (form)
Function Description: counts the optional items in the Form.
Parameter description:
Form: the specified form.
*/
Function selectedCheckboxCount (form ){
Var length = 0;
Var I = 0;
Var count = 0;
Eles = form. elements;
While (I Obj = eles. item (I );
// Type = obj. attributes. item ("type"). nodeValue;
Type = obj. type;
If (type = "checkbox "){
If (obj. checked ){
Count ++;
}
}
I ++;
}
Return count;
}
// Get the byte length
Function getByteLen (str)
{
Var l = str. length;
Var n = l;
For (var I = 0; I If (str. charCodeAt (I) <0 | str. charCodeAt (I)> 255)
N = n + 1;
Return n
}
/*
Note:
1. Clear data in the table (0.0 and 0)
2. If no data exists in the cell, a space is automatically added.
3. Clear the checkbox of the blank row
Parameters:
Clearzero: whether to clear "0", "0.0", false not clear, true clear (true by default)
Tablename: name of the table to be cleared. The default value is sortTable.
*/
Function clear_table (clearzero, tablename)
{
Var tobject;
If (tablename = null)
Tobject = gmobj ("sortTable ");
Else
Tobject = gmobj (tablename );
// If the table is not defined, no filtering is performed.
If (tobject = null)
Return;
// If the function call parameter is null, the values 0 and 0.0 are to be cleared. Otherwise, the values 0 and 0.0 are not cleared.
Var clear = (clearzero = null )? True: clearzero;
// Clear 0 and 0.0 and fill in spaces
Var rows = tobject. rows;
Var j = 0;
For (var I = 0; I {
// Obtain the clear attribute of the first cell. If it is 1, the row has no data, and all data of the row is cleared.
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;
}
/*
-------------------------------------------------------------------------------
Description: it is a JavaScript script that verifies the end Of the data item in the form.
-------------------------------------------------------------------------------
*/
/*
Purpose: Check whether the input string is in decimal number format. It can be a negative number (and meets the specified precision)
Input: str: String
L: Total digits
D: number of digits after the decimal point
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.
*/
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
& Amp; RegExp. $2. length <= d) return true;
}
Return false;
}
Onclick = "isNull ('co. PageForm. competitorname ');"
Co -- refers to PageForm -- refers to formName CompetitorName -- refers to the text column name.
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.