簡體   English   中英

JS表單驗證不起作用

[英]JS form validation is not working

我有一個表單,其中有JS表單驗證。 但是驗證過程不起作用。 插入過程正常。

以下是將數據插入mysql數據庫的形式

<?php
session_start();
include("includes/connection.php");
include("header.php");
include("includes/adminmenu.php");
if(isset($_SESSION['username']))
        {
        //echo $_SESSION['username'];
        ?>
        <!--Javasript Validation File Import -->
<script type="text/javascript" src="js/qcheck.js"></script>
<script type="text/javascript" language="JavaScript">

function HidePart(d) { document.getElementById(d).style.display = "none";  }
function ShowPart(d) { document.getElementById(d).style.display = "block"; }
function CheckboxChecked(b,d)
{
   if(b) { ShowPart(d); }
   else  { HidePart(d); }
}
</script>
    <br />
    <div class="userstat">

        <div style="background-color:#666666; text-align:center; font-weight:bold; color:#FFFFFF; font-size:24px;"><span>Insert A new Question</span></div>
        <br />
        <div class="statdata">
            <form action="includes/insertq.php" method="POST" name="qform" onSubmit="return valide()">
                    <div style="text-align:center;">
                <select name="subject" size="0">
                <option selected="0" value="">Select Subject</option>
                <option value="bangla">Bangla</option>
                <option value="english">English</option>
                <option value="physics">Physics</option>
                <option value="chemistry">Chemistry</option>
                <option value="math">Mathematics</option>
                <option value="biology">Biology</option>
                <option value="gk">General knowledge</option>
                </select>
                     </div>
                 <br /><br />
                <label for="question">Write Down the Question below</label>
                <textarea name="question" rows="3" cols="40"></textarea><br /><br />
                <label for="ans">Options</label><br /><br />
                <label for="option1">a.</label>
                <input type="text" name="option1" size="40" /><br />
                <label for="option2">b.</label>
                <input type="text" name="option2" size="40" /><br />
                <label for="option3">c.</label>
                <input type="text" name="option3" size="40" /><br />
                <label for="option4">d.</label>
                <input type="text" name="option4" size="40" /><br /><br />
                <label for="correct">Correct.</label><br />&nbsp;&nbsp;&nbsp;
                <input type="text" name="correct" size="40" /><br /><br /><br />
                <div style="text-align:center;">
                <input type="submit" name="submit" value="Submit Question" />
                </div>
                <br />
                <br />
            </form>
        </div>
        </div>

     </body>
        <?php
        }



    else
        {
        header("location: admin.php");
        }
?>

<?php
include("includes/footer.php");
?>

Javascript文件是

function valide()
{
var subject=document.forms["qform"]["subject"].value;
var question=document.forms["qform"]["question"].value;
var option1=document.forms["qform"]["option1"].value;
var option2=document.forms["qform"]["option2"].value;
var option3=document.forms["qform"]["option3"].value;
var option4=document.forms["qform"]["option4"].value;
var correct=document.forms["qform"]["correct"].value;

if(subject == null || Subject == "Select Subject")
    {
        alert("Select subject Type");
        return false;

        }
    else if(question==null || question=="" || question.length<5)
        {
            alert("Insert Valid question");
            return false;
            }   
    else if(option1==null || option1=="")
        {
            alert("Insert Option 1.");
            return false;
            }
    else if(option2==null || option2=="")
        {
            alert("Insert Option 2.");
            return false;
            }
    else if(option3==null || option3=="")
        {
            alert("Insert option 3.");
            return false;
            }   
    else if(option4==null || option4=="")
        {
            alert("Insert option 4.");
            return false;
            }   
    else if(correct==null || correct=="")
        {
            alert("Insert correct option.");
            return false;
            }                                   





}

案件很重要

if(subject == null || Subject == "Select Subject")
                      ^

主題!==主題

而且值不會為空。 您應該檢查長度為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM