function chkLen(field_name, allow_len, label_name,defaultEmpty) {
var s =trim(field_name.value);
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (getTextLen(s) > allow_len) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name + " length not more than " + allow_len + " bytes,Please reInput!");
return false;
}
return true;
}
function chkEmail(field_name,label_name,defaultEmpty) {
var s = field_name.value;
var str = /^[_\-\.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!str.test(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input right Email.");
return false;
}
return true;
}
function chkMoreEmail(field_name,label_name,defaultEmpty,seperator) {
var s = field_name.value;
var str = /^[_\-\.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
var ss=s.split(seperator);
var mail;
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
for( var i=0;i<ss.length;i++){
mail=ss[i];
if (!str.test(mail)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input right Email.");
return false;
}
}
return true;
}
function chkNum(field_name,label_name,defaultEmpty) {
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
var s = field_name.value;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!isNum(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" not valid,Please input Number!");
return false;
}
return true;
}
function chkCharNum(field_name,label_name,defaultEmpty) {
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
var s = field_name.value;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!isCharNum(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input number or letter!");
return false;
}
return true;
}
var s =trim(field_name.value);
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (getTextLen(s) > allow_len) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name + " length not more than " + allow_len + " bytes,Please reInput!");
return false;
}
return true;
}
function chkEmail(field_name,label_name,defaultEmpty) {
var s = field_name.value;
var str = /^[_\-\.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!str.test(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input right Email.");
return false;
}
return true;
}
function chkMoreEmail(field_name,label_name,defaultEmpty,seperator) {
var s = field_name.value;
var str = /^[_\-\.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
var ss=s.split(seperator);
var mail;
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
for( var i=0;i<ss.length;i++){
mail=ss[i];
if (!str.test(mail)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input right Email.");
return false;
}
}
return true;
}
function chkNum(field_name,label_name,defaultEmpty) {
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
var s = field_name.value;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!isNum(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" not valid,Please input Number!");
return false;
}
return true;
}
function chkCharNum(field_name,label_name,defaultEmpty) {
if (typeof(defaultEmpty) == "undefined") defaultEmpty = true;
var s = field_name.value;
if(isEmpty(s)) {
if (!defaultEmpty) {
field_name.focus();
field_name.style.background=fieldbg;
alert ("Please input " + label_name);
return false;
} else return true;
}
if (!isCharNum(s)) {
field_name.focus();
field_name.style.background=fieldbg;
alert(label_name+" input invalid,Please input number or letter!");
return false;
}
return true;
}
表单验证脚本
本文介绍了一组用于前端表单验证的JavaScript函数,包括检查长度、邮箱格式、数字及字母输入等,确保用户输入符合预期格式。
1167

被折叠的 条评论
为什么被折叠?



