Javascript没有'不适用于输入验证

Javascript doesn't work for input validation

本文关键字:适用于 输入 验证 不适用 没有 Javascript      更新时间:2023-09-26

我见过很多不同的方法,其中任何一种都适用于我

我正在使用引导程序。

表单直接进入"操作"(我想首先在"onupdate"上验证)

脚本:

[...]
<!-- Bootstrap core CSS -->
<link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="../style.css" rel="stylesheet" type="text/css/">
<script type="text/javascript">
        function valForm() 
        {
            alert("HALOOOOOOOOO");
            var errors = 0;
            var title = document.forms["tutorial_form"]["title"].value;
            var description = document.forms["tutorial_form"]["description"].value;
            var url = document.forms["tutorial_form"]["url"].value;
            if (title == null || title == "") 
            {
                document.getElementById("title_div").style.class = "form-group has-warning";
                errors++;
            }
            if (description == null || description == "") 
            {
                document.getElementById("description_div").style.class = "form-group has-warning";
                errors++;
            }
            if (url == null || url == "") 
            {
                document.getElementById("url_div").style.class = "form-group has-warning";
                errors++;
            }
            if( errors > 0)
            {
                document.getElementById("error_container").innerHTML = "<div class="alert alert-danger" role="alert"><p><b>ERROR!</b>All the information must be fulfilled.</p></div>";
                return false;
            }
        }
    </script>
</head>

形式:

<div class="container">
    <form name="tutorial_form" enctype="multipart/form-data" action="insert_tutorial.php" onsubmit="return valForm()" method="post">
        <div id="title_div" class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="Title input">
        </div>
        <div id="description_div" class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" id="description" name="description" placeholder="Description" rows="5"></textarea>
        </div>
        <div id="url_div" class="form-group">
            <label for="url">Video URL</label>
            <input type="text" class="form-control" id="url" name="url" placeholder="Insert youtube url">
        </div>
        <div class="form-group">
            <label for="file">Insert image file</label>
            <input type="file" name="image" id="file">
            <p class="help-block">Select the file. Take care that it's size is no longer that 16 MB.</p>
        </div>
        <input type="submit" value="Submit" name="submit" class="btn btn-success">
    </form>
    <hr>
</div>

提前感谢!!!

如果上一个if语句中出现语法错误,请将双引号改为单引号:

document.getElementById("error_container").innerHTML = "<div class='alert alert-danger' role='alert'><p><b>ERROR!</b>All the information must be fulfilled.</p></div>";

或者你也可以逃离它们:

"<div class='"alert alert-danger'" role='"alert'"><p><b>ERROR!</b>All the information must be fulfilled.</p></div>";