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.自行完整的网页结构。
以上所述就是本文的全部内容了,希望大家能够喜欢。