JS verification form The second part of _javascript skills

Source: Internet
Author: User
Tags abs

Description: JavaScript script, validating data items in a 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 + +) {
Objcheck = Objfrm.elements[i];
if (Objcheck.type = = "Text" &&!f_checktextvalid (Objcheck)) {
return false;

}
}
Drop down box
for (i = 0; i < len; i + +) {
Objcheck = Objfrm.elements[i];
if (Objcheck.type = = "Select-one" &&!f_checkselectvalid (Objcheck)) {
return false;

}
}
Time period is valid
if (f_checkstartandenddate (OBJFRM) = false) return false;
return true;
}
function F_checkselectvalid (obj) {
Alert ("Check select");
if (obj.options.length <= 0) {
Alert ("dropdown 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++) {
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 empty
if (Obj.getattribute ("isneed")!= null) {
if (f_isnotnull (obj) = false) return false;
}
Cannot exceed length
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 allowed to null value!) ");
return false;
}
return true;
}
function F_isnumber (obj) {
if (isNaN (Obj.value)) {
F_alert (obj, "should be numeric Type");
return false;

}
return true;
}
function F_checkdate (obj) {
if (Checkdate (obj.value) ==false) {
F_alert (obj, "not a valid date format!) ");
return false;

}
return true;
}
function F_checklength (obj) {
if (gettotalbytes (obj) > Math.Abs (Obj.getattribute ("MaxLength")) {
F_alert (obj, "exceeding 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) {
Assuming that the number type has 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 (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, "input must be an integer type!");
return false;
}

}
if (Strvaluetemp.indexof (".") < 0) {
Alert ("lennth==" + strvaluetemp);

if (Strvaluetemp.length > (len-pric)) {
F_alert (obj, "integer digit cannot exceed" + (Len-pric) + "bit");
return false;
}
}else{
Strint = strvaluetemp.substr (0, Strvaluetemp.indexof ("."));

Alert ("lennth==" + strint);

if (Strint.length > (len-pric)) {
F_alert (obj, "integer digit cannot exceed" + (Len-pric) + "bit");
return false;
}

Strdec = Strvaluetemp.substr (Strvaluetemp.indexof (".") +1), strvaluetemp.length);

Alert ("pric==" + Strdec);

if (Strdec.length > Pric) {
F_alert (obj, "decimal places can 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, "must be positive!");
return false;
}
return true;
}

/*
function Selectedcheckboxcount (form)
Feature Description: The optional count of options selected in form
Parameter description:
Form: The specified 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. Clear the data in the table (0.0 and 0)
2. If there is no data in the cell, automatically add a space
3. Clear a checkbox for a blank line
Parameters:
Clearzero: Clear "0", "0.0", false not clear, true clear (default is True)
TableName: The name of the table to be cleared, the default 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 done
if (tobject==null)
Return

If the function call argument is empty, it means to clear 0, 0.0, or not clear 0, 0.0.
var clear = (clearzero==null)? True:clearzero;
Clear 0, 0.0, fill space
var rows = tobject.rows;
var j=0;
for (Var i=0;i<rows.length;i++)
{
Gets the property of the first cell clear, and if 1 indicates that the row has no data, clears the row for all 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;
}
/*
-------------------------------------------------------------------------------
Description: JavaScript script, validating the data item end in the form
-------------------------------------------------------------------------------
*/
/*
Purpose: Check if the input string is a decimal number format, can be negative (and meet the specified precision)
Input: str: string
L: Total number of digits
D: Number of digits after decimal point
Return:
Returns False if the validation returns true
*/
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 ');"
co--refers to this page pageform--refers to the FormName competitorname---refers to the text column name

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.