1

我无法弄清楚我做错了什么。validateForm() 似乎没有从 onsubmit 执行。这是 validateForm()

function validateForm() {
    var amt = IsNumeric(document.forms["InvGenPay"]["Amount"].value);

    alert(amt);

    if (amt == false)
    {
        alert("placeholder to avoid scrolling.");
        return false;
    }
    else
    {
        return true;
    }
}

function IsNumeric(strString)
{
    //  check for valid numeric strings  
    var strValidChars = "0123456789.";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }

    if (0 > parseFloat(strString))
    {
        return false;
    }
    else
    {
        return blnResult;
    }
}

这是带有 onsubmit 的表单:

<script type="text/javascript" language="JavaScript1.2">
document.write('<form name="InvGenPayDonation" action="'+PostURL+'" onsubmit="return validateForm();" method="POST">');
</script>


<input type='text' name='DonationAmount' value="0.00">
In honor of <span style="color: #FF0000"><br />
<input type='text' name='TransDesc' id='TransDesc' value="" >
<input type="submit" value="Next">
</form>
4

2 回答 2

3

您最大的问题是您的验证代码中没有正确的表单名称(或正确的字段名称)。

var amt = IsNumeric(document.forms["InvGenPay"]["Amount"].value);

对比

'<form name="InvGenPayDonation" action="'+PostURL+'" 

完整的工作代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"         
   "http://www.w3.org/TR/html4/strict.dtd">
<head>
  <title>Sampe file</title>

  <script>
function validateForm() {
    // Badly named variable, since this is actually a boolean of 'IsNumeric'
    var amt = IsNumeric(document.forms["InvGenPayDonation"]["DonationAmount"].value);


    // Can be simplified as simply:
    //return amt;
    alert('Amt = ' + amt);

    if (!amt)
    {
        alert("placeholder to avoid scrolling.");
        return false;
    }
    else
    {
        return true;
    }
}

function IsNumeric(n) {
  // Shamelessly stolen from: 
  // http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
  return !isNaN(parseFloat(n)) && isFinite(n);
}

</script>

<body>
<form name="InvGenPayDonation" action="#"
        onsubmit="return validateForm();" 
        method=POST>


<input type='text' name='DonationAmount' value="0.00">
In honor of <span style="color: #FF0000"><br />
<input type='text' name='TransDesc' id='TransDesc' value="" >
<input type="submit" value="Next">

<script>
  // Assigned for testing purposes
  var  PostURL = "#"
  document.forms.InvGenPayDonation.action = PostURL;
</script>
</form>
</body>
</html>
于 2012-09-12T21:21:31.103 回答
2

JavaScript 字符串中不能有未转义的换行符。检查您的 JavaScript 控制台,您可能会遇到语法错误。该错误是onsubmit未运行的原因。

另外,如建议的那样,不要使用document.write,只需用 HTML 正常编写表单,并使用 JavaScript添加POST url。

<form name="InvGenPayDonation" onsubmit="return validateForm();" method="POST">
    <input type='text' name='DonationAmount' value="0.00">
    In honor of <span style="color: #FF0000"><br />
    <input type='text' name='TransDesc' id='TransDesc' value="" >
    <input type="submit" value="Next">
</form>

<script type="text/javascript">
    document.forms.InvGenPayDonation.action = PostURL;
</script>

PS 正如Jeremy J Starcher指出的那样,您的表格名称在里面是错误的validateForm

于 2012-09-12T21:19:48.153 回答