1.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <title>jQueryStudy</title>
  6. <style>
  7. .int{ height: 30px; text-align: left; width: 600px; }
  8. label{ width: 200px; margin-left: 20px; }
  9. .high{ color: red; }
  10. .msg{ font-size: 13px; }
  11. .onError{ color: red; }
  12. .onSuccess{ color: green; }
  13. </style>
  14. <script src="http://code.jquery.com/jquery-latest.js"></script>
  15. </head>
  16. <body>
  17. <form method="post" action="#">
  18. <div class="int">
  19. <label for="name">姓名:</label>
  20. <input type="text" id="name" class="required" />
  21. </div>
  22. <div class="int">
  23. <label for="email">邮箱:</label>
  24. <input type="text" id="email" class="required" />
  25. </div>
  26. <div class="int">
  27. <label for="address">住址:</label>
  28. <input type="text" id="address" />
  29. </div>
  30. <div class="int">
  31. <input type="submit" value="提交" id="send" style="margin-left: 70px;" />
  32. <input type="reset" value="重置" id="res" />
  33. </div>
  34. </form>
  35. <script>
  36. //为表单的必填文本框添加提示信息(选择form中的所有后代input元素)
  37. $("form :input.required").each(function () {
  38. //通过jquery api:$("HTML字符串") 创建jquery对象
  39. var $required = $("<strong class='high'>*</strong>");
  40. //添加到this对象的父级对象下
  41. $(this).parent().append($required);
  42. });
  43. //为表单元素添加失去焦点事件
  44. $("form :input").blur(function(){
  45. var $parent = $(this).parent();
  46. $parent.find(".msg").remove(); //删除以前的提醒元素(find():查找匹配元素集中元素的所有匹配元素)
  47. //验证姓名
  48. if($(this).is("#name")){
  49. var nameVal = $.trim(this.value); //原生js去空格方式:this.replace(/(^\s*)|(\s*$)/g, "")
  50. var regName = /[~#^$@%&!*()<>:;'"{}【】 ]/;
  51. if(nameVal == "" || nameVal.length < 6 || regName.test(nameVal)){
  52. var errorMsg = " 姓名非空,长度6位以上,不包含特殊字符!";
  53. //class='msg onError' 中间的空格是层叠样式的格式
  54. $parent.append("<span class='msg onError'>" + errorMsg + "</span>");
  55. }
  56. else{
  57. var okMsg=" 输入正确";
  58. $parent.find(".high").remove();
  59. $parent.append("<span class='msg onSuccess'>" + okMsg + "</span>");
  60. }
  61. }
  62. //验证邮箱
  63. if($(this).is("#email")){
  64. var emailVal = $.trim(this.value);
  65. var regEmail = /.+@.+\.[a-zA-Z]{2,4}$/;
  66. if(emailVal== "" || (emailVal != "" && !regEmail.test(emailVal))){
  67. var errorMsg = " 请输入正确的E-Mail住址!";
  68. $parent.append("<span class='msg onError'>" + errorMsg + "</span>");
  69. }
  70. else{
  71. var okMsg=" 输入正确";
  72. $parent.find(".high").remove();
  73. $parent.append("<span class='msg onSuccess'>" + okMsg + "</span>");
  74. }
  75. }
  76. }).keyup(function(){
  77. //triggerHandler 防止事件执行完后,浏览器自动为标签获得焦点
  78. $(this).triggerHandler("blur");
  79. }).focus(function(){
  80. $(this).triggerHandler("blur");
  81. });
  82. //点击重置按钮时,通过trigger()来触发文本框的失去焦点事件
  83. $("#send").click(function(){
  84. //trigger 事件执行完后,浏览器会为submit按钮获得焦点
  85. $("form .required:input").trigger("blur");
  86. var numError = $("form .onError").length;
  87. if(numError){
  88. return false;
  89. }
  90. });
  91. </script>
  92. </body>
  93. </html>