2017-12-07 14 views
-2

----------ソルバード------------HTML形式のJavascriptによる検証が機能しない

フォームの検証をしようとしていますフィールドのいずれかが空白のままされている場合という、警告があり、フォームは文句を言わない通過、それはまだこれらの2行に、データベース

function validateAdd() { 
 
    var a = document.add_product.product_id.value; 
 
    var b = document.add_product.brand.value; 
 
    var c = document.add_product.model.value; 
 
    var h = document.add_product.description.value; 
 
    var d = document.add_product.cam_mpx.value; 
 
    var e = document.add_product.storage.value; 
 
    var f = document.add_product.more_storage.value; 
 
    var g = document.add_product.price.value; 
 
    if (a == "" || b == "" || c == "" || d == "" || e == "" || f = "" || g == "" || h == "" || 
 
    a == null || b == null || c == null || d == null || e == null || f = null || g == null || h == null) { 
 
    alert("All fields must be filled in"); 
 
    return false; 
 
    } 
 
}
<form name="add_product" action="index.php" method="post" onsubmit="return validateAdd()"> 
 
    <label>Product ID:</label><br> 
 
    <input type="text" name="product_id"><br> 
 
    <label>Brand:</label><br> 
 
    <input type="text" name="brand"><br> 
 
    <label>Model:</label><br> 
 
    <input type="text" name="model"><br> 
 
    <label>Description:</label><br> 
 
    <input type="text" name="description"><br> 
 
    <label>Camera Megapixel:</label><br> 
 
    <input type="text" name="cam_mpx"><br> 
 
    <label>Storage:</label><br> 
 
    <input type="text" name="storage"><br> 
 
    <label>Expandable Storage?:</label><br> 
 
    <input type="text" name="more_storage"><br> 
 
    <label>Price:</label><br> 
 
    <input type="text" name="price"><br> 
 
    <button class="button" type="submit" name="save" onclick="return confirm('Are you sure you want to add this to the system?')">Add</button><br> 
 
    <p></p> 
 
</form>

+2

Javascriptのデバッグを試したことがありますか?すべての主要なブラウザには、これらのデバッグツールが組み込まれています。 – cteski

+0

AN入力の値はnullにはなりません。 – epascarello

+2

コンソール出力とは何ですか?デバッグをしましたか? –

答えて

1

ウェルにnull値を入力しています、の代わりにf = ""f = nullを実行しますとf == null:あなたはUncaught ReferenceError: Invalid left-hand side in assignmentそこから探すことに気づいているだろう

if (a == "" || b == "" || c == "" || d == "" || e == "" || f = "" || g == "" || h == "" 
     || a == null || b == null || c == null || d == null || e == null || f = null || g == null || h == null) { 

は、コンソールにエラーが発生したされています。

私はそれがデバッグへの道簡単ですので、あなたが

if (!(a && b && c && d && e && f && g && h)) { 

にそれをダウン短縮することを示唆しています。

+0

まだ動作せず、データベースに値を入力しようとしました –

+1

@oliverthomas他にエラーがありますか? – ericw31415