所以我在我的 PHP 页面中使用了以下 Javascript 函数来进行表单验证。
include ($_SERVER['DOCUMENT_ROOT'] . '/validators.php');
echo "<script Language='JavaScript' Type='text/javascript'><!--\n";
echo "function Prefix_Validator(theForm)\n";
echo "{\n";
validate_numeric('theForm', 'FieldName', 'FieldDesc');
echo "return (true);\n";
echo "}\n";
echo "//--></script>\n";
echo "\n";
这是我的表单输入的样子:
echo "</td><td>\n";
echo "<input type='text' name='txtQuantity_$DeptID' size = '3' value=''>\n";
echo "</td><td>\n";
echo "<input type='text' name='txtMemo_$DeptID' size='50' value=''>\n";
echo "</font></td></tr>\n";
现在通常,如果输入的 name='' 部分是 name='Qty' 我可以用我的 java 验证来做到这一点:
validate_numeric('theForm', 'Qty', 'Quantity');
但是由于我的表单在名称中使用了 $variable,所以我不知道如何将它与我的验证器一起使用。这是上述验证器函数的扩展部分:
function validate_numeric($formname, $fieldname, $fielddesc){
echo "var checkOK = '0123456789';\n";
echo "var checkStr = $formname.$fieldname.value;\n";
echo "var allValid = true;\n";
echo "var validGroups = true;\n";
echo "var decPoints = 0;\n";
echo "var allNum = '';\n";
echo "for (i = 0; i < checkStr.length; i++)\n";
echo "{\n";
echo "ch = checkStr.charAt(i);\n";
echo "for (j = 0; j < checkOK.length; j++)\n";
echo "if (ch == checkOK.charAt(j))\n";
echo "break;\n";
echo "if (j == checkOK.length)\n";
echo "{\n";
echo "allValid = false;\n";
echo "break;\n";
echo "}\n";
echo "allNum += ch;\n";
echo "}\n";
echo "if (!allValid)\n";
echo "{\n";
echo "alert('Please enter only digit characters in the $fielddesc field.');\n";
echo "$formname.$fieldname.focus();\n";
echo "return (false);\n";
echo "}\n";
return $x;
}
我试过这个,但它不起作用:
validate_numeric('theForm', 'txtQuantity_$DeptID', 'Quantity');
任何建议都会有很大帮助。如果需要更多信息,请告诉我,我会尽快提供。
编辑#1
所以我尝试了你所说的用 dbl 引号做的事情,这就是我在页面上看到的源代码:
var checkOK = '0123456789';
var checkStr = theForm.txtQuantity_.value;
var allValid = true;
var validGroups = true;
var decPoints = 0;
var allNum = '';
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
allNum += ch;
}
if (!allValid)
{
alert('Please enter only digit characters in the Quantity field.');
theForm.txtQuantity_.focus();
return (false);
}
txtQuantity_ 之后不应该有某种价值吗?在 checkStr 上?
我在这个页面上遇到了很多麻烦,我真的认为它只是讨厌我,并且尽其所能不工作。