Home >Web Front-end >JS Tutorial >js validation form part 2_javascript skills

js validation form part 2_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:24:00986browse

Description: JavaScript script, verify the data items 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 ){
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 The segment is valid
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 selection box!");
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 null
if(obj.getAttribute("isNeed") != null ){
if(f_isNotNull(obj) == false) return false;
}
//Cannot exceed the 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, "Null values ​​are not allowed! ");
return false;
}
return true;
}
function f_isNumber(obj){
if(isNaN(obj.value)){
f_alert( obj,"should be of numeric type");
return false;

}
return true;
}
function f_checkDate(obj) {
if(checkDate(obj .value) ==false){
f_alert(obj," is not a legal date format!"); 
return false; 


return true; 

function f_checkLength(obj){ 
if(getTotalBytes(obj) > Math.abs( obj.getAttribute("maxlength") ) ){ 
f_alert(obj," 超出长度限制!"); 
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){ 
//假设: 已经进行数字类型判断 
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, ""); 
//整数 
if(pric==0){ 
numReg =/[.]/; 
//alert(numReg.test(value)); 
if(numReg.test(value) == true){ 
f_alert(obj, "输入必须为整数类型!"); 
return false; 



if(strValueTemp.indexOf(".") < 0 ){ 
//alert("lennth=="   strValueTemp); 

if(strValueTemp.length >( len - pric)){ 
f_alert(obj, "整数位不能超过"  (len - pric)  "位"); 
return false; 

}else{ 
strInt = strValueTemp.substr( 0, strValueTemp.indexOf(".") ); 

//alert("lennth=="   strInt); 

if(strInt.length >( len - pric)){ 
f_alert(obj, "整数位不能超过"  (len - pric)  "位"); 
return false; 


strDec = strValueTemp.substr( (strValueTemp.indexOf(".") 1), strValueTemp.length ); 

//alert("pric=="   strDec); 

if(strDec.length > pric){ 
f_alert(obj, "小数位不能超过"   pric  "位"); 
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, "必须为正数!"); 
return false; 

return true; 
}

/*
function selectedCheckboxCount(form)
Function description: Count the selected options 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(iobj= 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< l; i )
if ( str.charCodeAt(i) <0 || str.charCodeAt(i) >255 )
n=n 1;
return n
}
/*
Instructions:
1. Clear the data (0.0 and 0) in the table
2. If there is no data in the cell, a space will be automatically added
3. Clear the checkbox of the blank row
Parameters:
clearzero: whether to clear "0", "0.0", false not to clear, true to clear (default is true)
tablename: name of the table to be cleared, default is sortTable
*/
function clear_table(clearzero,tablename)
{
var tobject;
if(tablename==null)
tobject=gmobj("sortTable");
else
tobject= gmobj(tablename);
//If table is not defined, no filtering will be performed
if(tobject==null)
return;

//If the function call parameter is empty, it means To clear 0, 0.0; conversely, do not clear 0, 0.0.
var clear = (clearzero==null)?true:clearzero;
//Clear 0, 0.0, fill in the spaces
var rows = tobject.rows;
var j=0;
for(var i=0;i{
//Get the attribute clear of the first cell. If it is 1, it means there is no data in the row, then clear all the data in the row
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, verify data items in the form end
----------------------------------------- ----------------------------------------
*/
/ *
Purpose: Check whether the input string is in a decimal number format, which can be a negative number (and meet the specified precision)
Input: str: string
l: total number of digits
d: Number of digits after the decimal point
Returns:
Returns true if the verification is passed, otherwise returns 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');"
Co--refer to this page PageForm--refer to formName CompetitorName---refer to text column name

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn