2017-06-19 133 views
0

我試圖驗證表單使用JavaScript,但表單驗證不起作用。不確定,如果它甚至被路由到該函數。這裏是html表單模式。JavaScript-HTML表單驗證不起作用

<div class="modal-body form"> 
       <form action="#" id="form" class="form-horizontal" name="myForm" onsubmit="return validateForm()" ;> 
        <input type="hidden" value="" name="id" /> 
        <div class="form-body"> 

         <div class="form-group"> 
          <label class="control-label col-md-3">Brand Name</label> 
          <div class="col-md-9"> 
           <input required type="text" name="bname" id="brname" class="form-control" placeholder="Name"> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="control-label col-md-3">Brand Origin</label> 
          <div class="col-md-9"> 
           <input required type="text" name="origin" id="brorigin" class="form-control" placeholder="Origin"> 
          </div> 
         </div> 

        </div> 
       </form> 

       <div class="modal-footer"> 
        <input type="submit" id="btnSave" value="Submit" onclick="save()" class="btn btn-primary"></input> 
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button> 
       </div> 

      </div> 

這裏是javascript的表單驗證腳本

function validateForm() 
    { 
     $('#btnSave').click(function() 
     { 

      var brname = document.getElementById("brname").value; 
      var brorigin = document.getElementById("brorigin").value; 
      if (brname.length == 0) 
      { 
       window.alert("You must enter a name."); 
       brname.focus(); 
       return false; 
      } 

      if (brorigin.length == 0) 
      { 
       window.alert("You must enter the origin country"); 
       brorigin.focus(); 
       return false; 
      } 
     }); 

    } 

感謝..

編輯:有形式模型prolem。 onclick事件監聽器將保存表單。當我在$(document).ready(function(){函數中移動函數時顯示錯誤警告

+0

你有沒有試圖改變 'x.length == 0' 到 'x.length === 0'? – MorganR

+0

您是否嘗試過移動表單標籤內的提交按鈕? –

+0

請指定「表單驗證無效」。什麼**正確**錯誤?代碼的預期結果是什麼? – gus27

回答

0

第一次嘗試提交表單時,它會將點擊事件偵聽器添加到提交按鈕,但沒有任何東西阻止表單提交。將點擊監聽器移出該功能,並排除該功能。然後,從表單中刪除onsubmit屬性。

$('#btnSave').click(function() { 

     var brname = document.getElementById("brname").value; 
     var brorigin = document.getElementById("brorigin").value; 
     if (brname.length == 0) 
     { 
      window.alert("You must enter a name."); 
      brname.focus(); 
      return false; 
     } 

     if (brorigin.length == 0) 
     { 
      window.alert("You must enter the origin country"); 
      brorigin.focus(); 
      return false; 
     } 
}); 
+0

它的工作,但只有一半的方式..它確實顯示了消息,但入口被添加 –

0

validateForm()的代碼將被執行只有當你點擊提交按鈕btnSave。 validateForm()函數只是在點擊時註冊一個事件處理程序,但點擊發生在事件處理程序的註冊之前。並且validateForm可能返回undefined。嘗試把validateForm的或$(document).ready(),或者直接在函數內部的代碼,沒有點擊event.`

function validateForm() 
{ 
     var brname = document.getElementById("brname").value; 
     var brorigin = document.getElementById("brorigin").value; 
     if (brname.length == 0) 
     { 
      window.alert("You must enter a name."); 
      brname.focus(); 
      return false; 
     } 

     if (brorigin.length == 0) 
     { 
      window.alert("You must enter the origin country"); 
      brorigin.focus(); 
      return false; 
     } 

} 

編輯:save()函數被調用,即使validateForm回報false因爲你打電話該功能在提交按鈕的onclick事件上。 嘗試在

+0

我已經嘗試把它直接放在函數內。它沒有工作 –

+0

試試我寫在編輯 –

+0

是的,這樣做的工作...感謝您指出'save()'函數 –

0

這兩個更新後的代碼嘗試撥打save()validateForm立即嘗試。

<div class="modal-body form"> 
        <form action="#" id="form" class="form-horizontal" name="myForm"> 
         <input type="hidden" value="" name="id" /> 
         <div class="form-body"> 

          <div class="form-group"> 
           <label class="control-label col-md-3">Brand Name</label> 
           <div class="col-md-9"> 
            <input required type="text" name="bname" id="brname" class="form-control" placeholder="Name"> 
           </div> 
          </div> 

          <div class="form-group"> 
           <label class="control-label col-md-3">Brand Origin</label> 
           <div class="col-md-9"> 
            <input required type="text" name="origin" id="brorigin" class="form-control" placeholder="Origin"> 
           </div> 
          </div> 

         </div> 
        </form> 

        <div class="modal-footer"> 
         <input type="button" id="btnSave" value="Submit" class="btn btn-primary"></input> 
         <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button> 
        </div> 

       </div> 



$('#btnSave').click(function() 
       { 

        var brname =$('#brname').val() 
        var brorigin = $('#brorigin').val() 
        if (brname == '') 
        { 
         window.alert("You must enter a name."); 
         brname.focus(); 
         return false; 
        } 

        if (brorigin == '') 
        { 
         window.alert("You must enter the origin country"); 
         brorigin.focus(); 
         return false; 
        } 

      return true 
       }); 
+0

我試着將它放在$(document).ready(function())中,並且它工作了一半......它顯示了消息,但提交了空白表單... 我也嘗試刪除功能,只有$(「#btnSave」)事件,這種方式它不工作 –