// 是否规定的浮点数,包括小数点前的位数和小数点后的位数,beforeDec是小数点前的位数,afterDec是小数点后的为数
function checkDecimalNumber(theField, fieldName, beforeDec, afterDec){
if (theField.value == "") return true;
var pattern ="/^(|[+-]?(0|([1-9]\\d{0,"+(beforeDec-1)+"})|((0|([1-9]\\d{0,"+(beforeDec-1)+"}))?\\.\\d{1,"+afterDec+"})){1,1})$/";
if (!eval(pattern).test(theField.value)) {
alert(fieldName + "必须为合法数字,允许小数点前"+beforeDec+"位和小数点后"+afterDec+"位之内");
theField.focus();
theField.select();
return false;
}
return true;
}
//使用 prototype 为 String 对象增加一个新方法 length2,找出字符串中有多少个汉字,然后将这个数字加上 length 的值就是我们想要的结果了。
String.prototype.length2 = function() {
var cArr = this.match(/[^\x00-\xff]/ig);
return this.length + (cArr == null ? 0 : cArr.length);
}
// 限制字串最大长度
function checkLength(theField, fieldName, maxLength) {
if(theField.value == "") return true;
if (theField.value.length2() > maxLength) {
alert(fieldName + "的字数最多为" + maxLength + "字!");
theField.select();
theField.focus();
return false;
}
return true;
}
// 限制字串长度,注意参数顺序
function checkLength2(theField, fieldName, maxLength, minLength) {
if(theField.value == "") return true;
if (theField.value.length2() > maxLength) {
alert(fieldName + "的字数最多为" + maxLength + "字!");
theField.focus();
return false;
}
if ((minLength != "") && (theField.value.length2() < minLength)) {
alert(fieldName + "的字数最少为" + minLength + "字!");
theField.focus();
return false;
}
return true;
}
// 所输入字符串是否均为合法字符
// charBag中为包含所有合法字符的字符串
function checkStrLegal(theField, fieldName, charBag) {
if(theField.value == "") return true;
for (var i = 0; i < theField.value.length; i++) {
var c = theField.value.charAt(i);
if (charBag.indexOf(c) == -1) {
alert(fieldName + "含有非法字符(" + c + ")!");
theField.focus();
return false;
}
}
return true;
}
// 所输入字符串是否均为合法字符
// charBag中为包含非法字符的字符串
function checkStrLegal2(theField, fieldName, charBag) {
if(theField.value == "") return true;
for (var i = 0; i < theField.value.length; i++) {
var c = theField.value.charAt(i);
if (charBag.indexOf(c) > -1) {
alert(fieldName + "含有非法字符(" + c +")!");
theField.focus();
return false;
}
}
return true;
}
|