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.自行完整的网页结构。
以上所述就是本文的全部内容了,希望大家能够喜欢。
本文地址:http://www.45fan.com/bcdm/15772.html