jQuery表單驗(yàn)證功能實(shí)例
本文實(shí)例講述了jQuery表單驗(yàn)證功能。分享給大家供大家參考。具體如下:
這里使用jquery實(shí)現(xiàn)的表單驗(yàn)證效果,以Ajax方式驗(yàn)證你的表單是否填寫正確,如果驗(yàn)證不通過(guò),會(huì)將表單元素背景變成紅色,并給出提示信息,簡(jiǎn)單實(shí)用,jquery表單驗(yàn)證功能已經(jīng)有很多了,本款表單驗(yàn)證特效看上去更簡(jiǎn)單,不懂Ajax的朋友,或許直接套用即可實(shí)現(xiàn)無(wú)刷新表單驗(yàn)證功能。
運(yùn)行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/jquery-table-form-check-codes/
具體代碼如下:
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery表單驗(yàn)證</title> <style type="text/css"> body, input, textarea { font-size:12px; line-height:18px; font-family:Verdana, Geneva, sans-serif; } input {width:200px;} .submit {width:120px;} #error { color:red; font-size:10px; display:none; } .needsfilled { background:red; color:white; } </style> <script type="text/javascript" src="jquery-1.6.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ // Place ID's of all required fields here. required = ["name", "email", "message"]; // If using an ID other than #email or #error then replace it here email = $("#email"); errornotice = $("#error"); // The text to show up within a field when it is incorrect emptyerror = "Please fill out this field."; emailerror = "Please enter a valid e-mail."; $("#theform").submit(function(){ //Validate required fields for (i=0;i<required.length;i++) { var input = $('#'+required[i]); if ((input.val() == "") || (input.val() == emptyerror)) { input.addClass("needsfilled"); input.val(emptyerror); errornotice.fadeIn(750); } else { input.removeClass("needsfilled"); } } // Validate the e-mail. if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) { email.addClass("needsfilled"); email.val(emailerror); } //if any inputs on the page have the class 'needsfilled' the form will not submit if ($(":input").hasClass("needsfilled")) { return false; } else { errornotice.hide(); return true; } }); // Clears any fields in the form when the user clicks on them $(":input").focus(function(){ if ($(this).hasClass("needsfilled") ) { $(this).val(""); $(this).removeClass("needsfilled"); } }); }); </script> </head> <body> <form action="mail.php" id="theform" name="theform" method="post"> <p><label for="name">Name</label><br /><input id="name" type="text" value="" name="name" /></p> <p><label for="email">E-mail</label><br /><input id="email" type="text" value="" name="email" /></p> <p><label for="message">Message</label><br /><textarea id="message" rows="7" cols="30" name="message"></textarea></p> <p><input class="submit" type="submit" name="submit" value="Submit Form" /></p> <p id="error">表單中有錯(cuò)誤信息!</p> </form> </body> </html>
希望本文所述對(duì)大家的jquery程序設(shè)計(jì)有所幫助。
相關(guān)文章
ajax 提交數(shù)據(jù)到后臺(tái)jsp頁(yè)面及頁(yè)面跳轉(zhuǎn)問(wèn)題
這篇文章主要介紹了ajax 提交數(shù)據(jù)到后臺(tái)jsp頁(yè)面及頁(yè)面跳轉(zhuǎn)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-01-01基于Bootstrap和jQuery構(gòu)建前端分頁(yè)工具實(shí)例代碼
本文給大家介紹基于Bootstrap和jQuery構(gòu)建前端分頁(yè)工具實(shí)例代碼,包括前端分頁(yè)的優(yōu)缺點(diǎn)和解決辦法,對(duì)jquery bootstrap分頁(yè)知識(shí)感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-11-11jQuery實(shí)現(xiàn)的簡(jiǎn)單無(wú)刷新評(píng)論功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單無(wú)刷新評(píng)論功能,涉及jQuery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,代碼中備有較為詳盡的注釋便于理解,需要的朋友可以參考下2017-11-11Jquery 分頁(yè)插件之Jquery Pagination
實(shí)用jQuery分頁(yè)特效插件jquery.pagination.js,基于jQuery實(shí)現(xiàn),本文給大家分享jquery分頁(yè)插件之jquery pagination,需要的朋友可以參考下2015-08-08JQEasy-ui在IE9以下版本中二次加載的問(wèn)題分析及處理方法
之前項(xiàng)目中才用了Easy-ui,但是在同時(shí)使用tree和grid的效果時(shí),因?yàn)轫?yè)面有倆個(gè)URL,分別為Ajax樹去后臺(tái)取數(shù)據(jù)和Grid取數(shù)據(jù),在IE9以上以及其他瀏覽器里都沒(méi)有問(wèn)題,在Ie低版本時(shí)會(huì)出現(xiàn)先加載表格,然后一閃而過(guò)加載樹渲染頁(yè)面,造成頁(yè)面只有tree數(shù)據(jù)而沒(méi)有表格grid數(shù)據(jù)。2014-06-06jQuery快速上手:寫jQuery與直接寫JS的區(qū)別詳細(xì)解析
jQuery代碼具體的寫法和原生的Javascript寫法在執(zhí)行常見(jiàn)操作時(shí)的區(qū)別如下所示。需要的朋友可以過(guò)來(lái)參考下2013-08-08舉例講解jQuery中可見(jiàn)性過(guò)濾選擇器的使用
這篇文章主要介紹了jQuery中可見(jiàn)性過(guò)濾選擇器的使用,文中分為不可見(jiàn)性選擇器和可見(jiàn)性選擇器來(lái)分別舉例講解,需要的朋友可以參考下2016-04-04jquery+ajax實(shí)現(xiàn)跨域請(qǐng)求的方法
這篇文章主要介紹了jquery+ajax實(shí)現(xiàn)跨域請(qǐng)求的方法,詳細(xì)介紹了前臺(tái)及后臺(tái)的處理方法,是非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了jQuery自定義圖片縮放拖拽插件imageQ實(shí)現(xiàn)方法,涉及jQuery擴(kuò)展操作及頁(yè)面元素操作技巧,并附帶了完整的demo源碼供讀者下載參考,需要的朋友可以參考下2016-05-05解決jquery .ajax 在IE下卡死問(wèn)題的解決方法
為什么會(huì)出現(xiàn)在IE無(wú)效化呢,從上面的解決方案可以知道是XML文件的編碼問(wèn)題,在轉(zhuǎn)化會(huì)前臺(tái)可讀時(shí)并未符合IE的XML 對(duì)象,故需要翻譯成IE兼容的對(duì)象。2009-10-10