Javascript 验证文本框时出错 IF 所有三个文本框 <> 则 100%

标签 javascript html validation textbox

我有一个包含 4 个文本框的表单,我需要验证方面的帮助。

我需要总组合值 = 100%,如果小于或大于则显示错误。

例如

框 1 用户输入 50%,框 2 用户输入 30%,框 3 用户输入 19%。 方框 4,将容纳方框 1,2 和 3 的总值(value),即总计 99% 当用户点击提交时,它应该验证并返回错误,例如,总计不等于 100% 如果方框 4 的总和> 100%,情况也是如此

目前我让它计算框 4 的总值,但不知道如何进行验证!

    <SCRIPT language="JavaScript">

function CalculateCardT()
{
    formSectionA.CardTotal.value = 

parseFloat(formSectionA.strPercDeferredGoods.value) + parseFloat(formSectionA.strPercDeposits.value) + parseFloat(formSectionA.strPercSubscriptions.value);


    }
    </SCRIPT>


          <tr>
        <td rowspan="2" nowrap="nowrap"> % of CARD turnover</td>
        <td nowrap="nowrap">% Deferred delivery of goods</td>
        <td nowrap="nowrap">% Deposit is taken</td>
        <td nowrap="nowrap">% Subscriptions</td>
        <td nowrap="nowrap">% Total</td>
      </tr>
      <tr>
        <td nowrap="nowrap">
          <input name="strPercDeferredGoods" type="text" required="required" id="strPercDeferredGoods" tabindex="6" onBlur="CalculateCardT();"  onChange="CalculateCardT();" size="3" maxlength="3" /></td>
        <td nowrap="nowrap">
          <input name="strPercDeposits" type="text" required="required" id="strPercDeposits" " tabindex="7" onBlur="CalculateCardT();" onChange="CalculateCardT();" size="3" maxlength="3" />
         </td>
        <td nowrap="nowrap">
          <input name="strPercSubscriptions" type="text" required="required" id="strPercSubscriptions" tabindex="8"  onblur="CalculateCardT();" onChange="CalculateCardT();" size="3" maxlength="3"/></td>
        <td nowrap="nowrap">
          <input name="CardTotal" type="text" id="CardTotal" style="background-color:#CCC" size="3" maxlength="3" onKeyDown="return false;"/></td>
      </tr>
顺便说一句。我是一名菜鸟,正在使用 Dreamweaver,所以请简单地编写代码!

最佳答案

首先 - 您使用的代码已过时并且存在语法错误。

这是语法正确的 HTML 的较短版本

<table>
    <thead>
        <tr>
            <th>% Deferred delivery of goods</th>
            <th>% Deposit is taken</th>
            <th>% Subscriptions</th>
            <th>% Total</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="text" id="strPercDeferredGoods" />
            </td>
            <td>
                <input type="text" id="strPercDeposits" />
            </td>
            <td>
                <input type="text" id="strPercSubscriptions" />
            </td>
            <td>
                <input type="text" id="CardTotal" disabled="disabled" />
            </td>
        </tr>
    </tbody>
</table>

还有 JavaScript,我在每一行都注释了它的作用,以便您可以从中学习:

// IIFE - create local "document"
(function (document) {

    // Store all of the "input" elements in the variable "elems"
    var elems = document.getElementsByTagName("input");

    // Store an array of id's of the items to sum in the variable "idsToSum"
    var idsToSum = ["strPercDeferredGoods", "strPercDeposits", "strPercSubscriptions"]

    // Loop through the "input" elements
    for (var i = 0; i < elems.length; i++) {

        // For each "input" element, add a "change" event listener
        // When the event happens, run the function
        elems[i].addEventListener("change", function () {

            // Define a variable "sum" and assign a value of 0 to it
            var sum = 0;

            // Loop through all of the "idsToSum"
            for (var a = 0; a < idsToSum.length; a++) {

                // If the value of the current input is a number (removing the % sign if applicable), then add the value to the "sum" variable.  Otherwise, add 0
                sum += isNaN(parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""))) ? 0 : parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""));
            }

            // If the value of "sum" is greater than 100
            if (sum > 100) {

                // Clear the "CardTotal" value
                document.getElementById("CardTotal").value = "";

                // Alert an error
                alert("Total is not equal to 100%");
            }

            // Otherwise
            else {

                // Assign the value of "CardTotal" to the value of "sum" and add a % sign
                document.getElementById("CardTotal").value = sum + "%";
            }
        });
    }

// IIFE - pass in global "document"
})(document);

注意:您的 JavaScript 应放置在 HTML 结束正文之前 </body>

这是一个工作示例:

(function (document) {
    var elems = document.getElementsByTagName("input");
    var idsToSum = ["strPercDeferredGoods", "strPercDeposits", "strPercSubscriptions"]
    for (var i = 0; i < elems.length; i++) {
        elems[i].addEventListener("change", function () {
            var sum = 0;
            for (var a = 0; a < idsToSum.length; a++) {
                sum += isNaN(parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""))) ? 0 : parseFloat(document.getElementById(idsToSum[a]).value.replace("%", ""));
            }
            if (sum > 100) {
                document.getElementById("CardTotal").value = "";
                alert("Total is not equal to 100%");
            }
            else {
                document.getElementById("CardTotal").value = sum + "%";
            }
        });
    }
})(document);
input{width:96%;}
table,th,td{border:1px solid black; border-collapse:collapse;}
<table>
    <thead>
        <tr>
            <th>% Deferred delivery of goods</th>
            <th>% Deposit is taken</th>
            <th>% Subscriptions</th>
            <th>% Total</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="text" id="strPercDeferredGoods" />
            </td>
            <td>
                <input type="text" id="strPercDeposits" />
            </td>
            <td>
                <input type="text" id="strPercSubscriptions" />
            </td>
            <td>
                <input type="text" id="CardTotal" disabled="disabled" />
            </td>
        </tr>
    </tbody>
</table>

另外 - 您确实没有理由使用 Dreamweaver。花点时间学习代码。它确实非常简单,并且有大量针对 HTML 和 JavaScript 的免费资源和教程。

关于Javascript 验证文本框时出错 IF 所有三个文本框 <> 则 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28363513/

相关文章:

javascript - 可见性设置为隐藏后,无法查看 html 表

javascript - 如何在页面上显示字符串

html - 样式 woocommerce 描述

json - 如何使用 Golang 验证 JSON 输入?

javascript - jQuery Tools Validator - 需要在两个字段中的任何一个中输入

javascript - 在关闭另一个 Bootstrap 模式后打开 Bootstrap 模式 - 意外行为

javascript - 隐藏每个 parent 的第一个 child 以外的所有 child

java - 如何处理RequestMapping中的@Valid违规?

javascript - 使用 PHP 和 Javascript 的多个提交按钮

php - 如何逻辑地使用 PHP 显示数据库表的所有可能值?