2017-10-28 45 views
0

我填寫表格中的所有字段後,我有一個表單並啓用提交按鈕。我使用jquery禁用按鈕也使用disabled="disabled"在提交按鈕。現在我在瀏覽器上,按鈕顯示禁用。哪一種安全的方法來禁用按鈕?

現在我做了什麼,右鍵單擊並檢查元素並進入我的註冊按鈕,並從HTML中刪除disabled=" disabled",並且我的按鈕在未填寫詳細信息的情況下啓用,並且我沒有單擊提交的按鈕表單。

我只想知道有沒有其他解決方案來處理這個問題?因爲任何人都可以在不填寫表單的情況下啓用它並訪問它。

你能幫我解決嗎?

enter image description here

(function() { 
 
    $('form > input').keyup(function() { 
 
     var empty = false; 
 
     $('form > input').each(function() { 
 
      if ($(this).val() == '') { 
 
       empty = true; 
 
      } 
 
     }); 
 
     if (empty) { 
 
      $('#register').attr('disabled', 'disabled'); 
 
     } else { 
 
      $('#register').removeAttr('disabled'); 
 
     } 
 
    }); 
 
})()
<form> 
 
    Username<br /> 
 
    <input type="text" id="user_input" name="username" /><br /> 
 
    Password<br /> 
 
    <input type="password" id="pass_input" name="password" /><br /> 
 
    Email<br /> 
 
    <input type="text" id="email" name="email" /><br />  
 
    <input type="submit" id="register" value="Register" disabled="disabled" /> 
 
</form> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

+4

這是不可能使前端那樣的安全,這就是爲什麼它總是重要添加服務器端驗證 – user184994

+0

@ user184994,感謝您的回覆,那我知道要添加服務器端驗證。我認爲還有另一種處理方式。 –

+0

唯一的方法是使用服務器端處理 –

回答

2

禁用的屬性是好的,但它或多或少的化妝品。

有一種簡單的方法來阻止使用JavaScript提交表單提交事件處理程序。您檢查是否符合條件 - 否則您提交取消

0

您可以防止形式通過簡單地修改你的函數此提交:

(function() { 
$('form > input').keyup(function(e) { 
    e.preventDefault(); 
    var empty = false; 
    $('form > input').each(function() { 
     if ($(this).val() == '') { 
      empty = true; 
     } 
    }); 
    if (empty) { 
     $('#register').attr('disabled', 'disabled'); 
    } else { 
     $('#register').removeAttr('disabled'); 
    } 
}); 

})()

相關問題