国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery表單驗證功能實例

2019-11-20 11:39:34
字體:
來源:轉載
供稿:網友

本文實例講述了jQuery表單驗證功能。分享給大家供大家參考。具體如下:

這里使用jquery實現的表單驗證效果,以Ajax方式驗證你的表單是否填寫正確,如果驗證不通過,會將表單元素背景變成紅色,并給出提示信息,簡單實用,jquery表單驗證功能已經有很多了,本款表單驗證特效看上去更簡單,不懂Ajax的朋友,或許直接套用即可實現無刷新表單驗證功能。

運行效果截圖如下:

在線演示地址如下:

http://demo.VeVB.COm/js/2015/jquery-table-form-check-codes/

具體代碼如下:

<!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>jQuery表單驗證</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">表單中有錯誤信息!</p></form></body></html>

希望本文所述對大家的jquery程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉城县| 屯门区| 阿巴嘎旗| 轮台县| 马关县| 福鼎市| 军事| 安宁市| 抚宁县| 太谷县| 都匀市| 离岛区| 延边| 久治县| 酒泉市| 扎囊县| 马龙县| 万年县| 巴林右旗| 河北省| 株洲市| 青冈县| 甘孜县| 嘉荫县| 宝清县| 名山县| 图片| 瑞金市| 绥江县| 通道| 迁安市| 广平县| 山西省| 武安市| 五家渠市| 老河口市| 阳东县| 富锦市| 华宁县| 突泉县| 中牟县|