validatemaxlength.js
来自「基于B/S模式的网上一行系统」· JavaScript 代码 · 共 44 行
JS
44 行
/*$RCSfile: validateMaxLength.js,v $ $Revision: 1.10 $ $Date: 2004/03/28 16:53:21 $ */
/**
* A field is considered valid if less than the specified maximum.
* Fields are not checked if they are disabled.
* <p>
* <strong>Caution:</strong> Using <code>validateMaxLength</code> on a password field in a
* login page gives unnecessary information away to hackers. While it only slightly
* weakens security, we suggest using it only when modifying a password.</p>
* @param form The form validation is taking place on.
*/
function validateMaxLength(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
var formName = form.getAttributeNode("name");
oMaxLength = eval('new ' + formName.value + '_maxlength()');
for (x in oMaxLength) {
var field = form[oMaxLength[x][0]];
if ((field.type == 'hidden' ||
field.type == 'text' ||
field.type == 'password' ||
field.type == 'textarea') &&
field.disabled == false) {
var iMax = parseInt(oMaxLength[x][2]("maxlength"));
if (field.value.length > iMax) {
if (i == 0) {
focusField = field;
}
fields[i++] = oMaxLength[x][1];
isValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return isValid;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?