本文給大家分享的是使用jquery實現表單驗證并阻止非法提交的代碼,思路非常不錯,這里推薦給小伙伴們參考下。
代碼檢測textarea內填寫的長度,未填寫時提示需要重新填寫,少于15字符時提示需要長于15字符,成功時顯示所填寫建議。
- <script type="text/javascript">
- //jQuery代碼
- function confirm()
- {
- if($("#advice").val().length == 0)
- {
- alert("We can't see your advice. Maybe you should fill the form first.");
- $("#advice").focus();
- return false;
- }
- else if($("#advice").val().length <= 15)
- {
- alert("Your advice should longer than 15 letters.");
- $("#advice").focus();
- return false;
- }
- else
- {
- alert("Thank you for your advice! You will get out reply in 24 hours for advice:/n"+$("#advice").val());
- return true;
- }
- }
- </script>
- <form action="" method="post" onSubmit="return confirm();" >
- <textarea id="advice" rows="20" cols="50"placeholder="Give us some advice?"></textarea>
- <input type="submit"value="Thank you"/>
- </form>
>關鍵點
1.必須要有onSubmit="return confirm();" return 這個詞,不可缺少。
2.自行完整的網頁結構。
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答
圖片精選