表格

时间:2017-04-20 11:49:27

标签: javascript validation

我重新调整了一些用于计算增值税的代码,但我需要验证它不仅是空值,还是3,000,000以下的值。 Javascript并不是我的强项,所以我很难做一些看似相当基本的事情。到目前为止,我有这个:

<script language="javascript">

    // starts calculation
    function calculate() {
        var vat, total, vatTotal, finalTotal;

        var frm = document.calculator;

        total = evalnum(frm.total.value);
        if (total == "NaN")
            total = 0;
        if (total == 0) {
            alert("Please enter a valid ammount first.");
            frm.total.focus()
            return
        }
        else
            frm.total.value = evalpound(total);
        {
            vatTotal = total * 0.005;
            frm.calcvat.value = evalpound(vatTotal);
            frm.calctotal.value = evalpound(total + vatTotal);
        }
    }

    //----------------------------------------------------------------------------
    //  Scripts for VAT calculator
    //----------------------------------------------------------------------------

    var frm = document.calculator;

    // ----------------------------------------------------------------------------
    function selectBox(frmE, selectText) {

        var i;
        var selectIndex = 0;

        for (var i = 0; i < document.calculator[frmE].length; i++) {
            if (document.calculator[frmE].options[i].value == selectText) {
                selectIndex = i;
            }
        }

        return selectIndex;
    }

    // ----------------------------------------------------------------------------
    function evalnum(numstr) {

        var i, c, neg;
        var ret = "";
        var p = false;

        neg = 1;
        if (numstr.indexOf("-") >= 0) {
            neg = -1;
        }

        for (i = numstr.length - 1; i >= 0; i--) {
            c = numstr.charAt(i);

            if (c == ".") {
                if (p == false)
                    p = true;
                else
                    c = "";
            }

            if ((c < "0" || c > "9") && c != ".")
                c = "";

            ret = c + ret;
        }

        if (ret == "" || ret == "NaN")
            ret = "0";
        return parseFloat(ret * neg);

    }

    // ----------------------------------------------------------------------------
    function evalpound(num) {

        var i, l, d;
        var nums;
        var ret;

        nums = String(Math.round(num * 100));

        while (nums.length < 3)
            nums = "0" + nums;

        l = nums.length - 3;
        ret = "." + nums.charAt(l + 1) + nums.charAt(l + 2);

        d = 0;
        for (i = l; i >= 0; i--) {
            ret = nums.charAt(i) + ret;
            d++;
            if (d == 3 && i > 0) {
                ret = "," + ret;
                d = 0;
            }
        }

        ret = "£" + ret;
        return ret;

    }

</script>

HTML代码如下。

    <form name="calculator">
<td class="calcbody" width="220" valign="top">Salary Bill:</td>
<td class="calcbody" width="150"><input type="text" class="calcinput" name="total" title
min="0" max format value size="9" style="width: 100px; text-align: right;"
onchange="calculate(this)"></td>
<tr>
<td width="100" height="22" class="calcbody" align="center"><a class="calcbutton"
href="javascript:calculate(this)">Calculate</a></td>
<tr>
<td colspan="2" class="calcsubtitle"><b>Results</b></td>
<td></td>
</tr>
<tr>
<td class="calcbody" valign="top">result</td>
<td class="calcbody"><input type="text" class="calcinput" name="calcvat"></td>
<td class="calcbody"></td>
</tr>

1 个答案:

答案 0 :(得分:0)

我修好了。 HTML无论如何都适用(它只是旧代码)。我只是无法改变.js

        total = evalnum(frm.total.value);
    if (total == "NaN")
        total = 0;
    if (total == 0) {

成了

        total = evalnum(frm.total.value);
    if (total == "NaN")
        total = 0;
    if (total < 3000000) {

我一直在尝试总计==&lt; 3000000和东西,而不是替换==。正如我所说的一个非常非常基本的变化 - 感谢任何情况下的帮助:)