【问题标题】:Multiple Forms with common JS validation具有通用 JS 验证的多个表单
【发布时间】:2015-10-22 10:02:01
【问题描述】:

我是 Javascript 和 jQuery 的新手。我得到的只是这个 JS 函数来验证我的表单。我在这两种形式中都有一些共同的领域。而不是调用不同的函数来验证不同的表单,我想要一个 JS 函数来验证两种表单中的所有公共字段。仅供参考,这两种形式都在不同的页面中。

JS

function check1(){
    var x = document.forms["form1"]["type"].value;
    if (x == null || x == "") {
        alert("Please Enter Type");
        return false;
    }

function check2(){
    var x = document.forms["form2"]["type"].value;
    if (x == null || x == "") {
        alert("Please Enter Type");
        return false;
    }

HTML

form1

<form name="form1" method="post" action="#" onsubmit="return check1()">
<input type="text" name="type" id="type">
</form>

form2

<form name="form2" method="post" action="#" onsubmit="return check2()">
<inout type="text" name="type" id="type">
</form>

提前致谢。请容忍我,我是新手。

【问题讨论】:

    标签: javascript jquery html forms validation


    【解决方案1】:

    以下是两种形式的共同功能: (即使用 check 代替 check1 和 check2)。

    function check(){
        var frm = document.forms["form1"] || document.forms["form2"];
        var x = frm["type"].value;
        if (x == null || x == "") {
            alert("Please Enter Type");
            return false;
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-05
      • 2014-07-06
      • 2018-02-18
      • 2021-04-28
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      相关资源
      最近更新 更多