jquery实现表单验证并阻止非法提交
代码检测textarea内填写的长度,未填写时提示需要重新填写,少于15字符时提示需要长于15字符,成功时显示所填写建议。
<scripttype="text/javascript">
//jQuery代码
functionconfirm()
{
if($("#advice").val().length==0)
{
alert("Wecan'tseeyouradvice.Maybeyoushouldfilltheformfirst.");
$("#advice").focus();
returnfalse;
}
elseif($("#advice").val().length<=15)
{
alert("Youradviceshouldlongerthan15letters.");
$("#advice").focus();
returnfalse;
}
else
{
alert("Thankyouforyouradvice!Youwillgetoutreplyin24hoursforadvice:\n"+$("#advice").val());
returntrue;
}
}
</script>
<formaction=""method="post"onSubmit="returnconfirm();">
<textareaid="advice"rows="20"cols="50"placeholder="Giveussomeadvice?"></textarea>
<inputtype="submit"value="Thankyou"/>
</form>
>关键点
1.必须要有onSubmit="returnconfirm();"return这个词,不可缺少。
2.自行完整的网页结构。
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短