Javascript验证空字段

时间:2015-05-15 09:01:14

标签: javascript

任何人都可以帮我解决如何验证javascript表单的问题 以下字段是空的?它似乎不适用于我的代码。

function addvalidation(){
        var w = document.forms["shop"]["w"].value;
        var t = document.forms["shop"]["t"].value;
        var a = document.forms["shop"]["a"].value;
        var c = document.forms["shop"]["c"].value;
        var s = document.forms["shop"]["s"].value;
        var c = document.forms["shop"]["c"].value;

        if (w == "" || t =="" || a == ""|| c == "" || s == "" || c == "") {
                alert("Mandatory fields must be filled out");
                return false;
        }

http://jsfiddle.net/tn2fvh4p/91/

http://jsfiddle.net/tn2fvh4p/68/

1 个答案:

答案 0 :(得分:0)

请使用以下代码

function addvalidation(){
     var w = document.getElementById("w").value;
     var t = document.getElementById("t").value;
     var a = document.getElementById("a").value;
     var c = document.getElementById("c").value;
     var s = document.getElementById("s").value;
     var c = document.getElementById("c").value;

     if (w == "" || t =="" || a == ""|| c == "" || s == "" || c == "") {
             alert("Mandatory fields must be filled out");
             return false;
         }