index.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Total Form Validation</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no">
  7. <link rel="stylesheet" href="fv.css" type="text/css" />
  8. <!--[if IE]>
  9. <style>
  10. .item .tooltip .content{ display:none; opacity:1; }
  11. .item .tooltip:hover .content{ display:block; }
  12. </style>
  13. <![endif]-->
  14. </head>
  15. <body>
  16. <a class='btn github' href='https://github.com/yairEO/validator'>Github</a>
  17. <div id='wrap'>
  18. <div class='options'>
  19. <label>
  20. <input type='checkbox' id='vfields' />
  21. Vertical orientation
  22. </label>
  23. <label>
  24. <input type='checkbox' id='alerts' />
  25. Disable alerts
  26. </label>
  27. </div>
  28. <h1 title='how forms should be done.'>Forms: validation, styling &amp; semantics</h1>
  29. <section class='form'>
  30. <form action="" method="post" novalidate>
  31. <fieldset>
  32. <div class="item">
  33. <label>
  34. <span>Name</span>
  35. <input data-validate-length-range="6" data-validate-words="2" name="name" placeholder="ex. John f. Kennedy" required="required" />
  36. </label>
  37. <div class='tooltip help'>
  38. <span>?</span>
  39. <div class='content'>
  40. <b></b>
  41. <p>Name must be at least 2 words</p>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="item">
  46. <label>
  47. <span>Occupation</span>
  48. <input class='optional' name="occupation" data-validate-length-range="5,20" type="text" />
  49. </label>
  50. <div class='tooltip help'>
  51. <span>?</span>
  52. <div class='content'>
  53. <b></b>
  54. <p>An optional field. This field is only validated when it has a value.<br /><em>Minimum 5 chars for this example.</em></p>
  55. </div>
  56. </div>
  57. <span class='extra'>(optional)</span>
  58. </div>
  59. <div class="item">
  60. <label>
  61. <span>HTML5 Regex</span>
  62. <!--<input required="required" type="text" pattern='\d+' />-->
  63. <input type="text" data-validate-length-range="2,6" required="required" placeholder="eg. 123456" pattern="alphanumeric" />
  64. </label>
  65. <div class='tooltip help'>
  66. <span>?</span>
  67. <div class='content'>
  68. <b></b>
  69. <p>This field uses HTML5 "pattern" attribute to be validated.<br /><em>"<strong>\d+</strong>" - only digits are allowed</em></p>
  70. </div>
  71. </div>
  72. </div>
  73. <div class="item">
  74. <label>
  75. <span>email</span>
  76. <input name="email" class='email' required="required" type="email" />
  77. </label>
  78. </div>
  79. <div class="item">
  80. <label>
  81. <span>Confirm email address</span>
  82. <input type="email" class='email' name="confirm_email" data-validate-linked='email' required='required'>
  83. </label>
  84. </div>
  85. <div class="item">
  86. <label>
  87. <span>Number</span>
  88. <input type="number" class='number' name="number" data-validate-minmax="10,100" required='required'>
  89. </label>
  90. <div class='tooltip help'>
  91. <span>?</span>
  92. <div class='content'>
  93. <b></b>
  94. <p>Number must be between 10 and 100</p>
  95. </div>
  96. </div>
  97. </div>
  98. <div class="item">
  99. <label>
  100. <span>Date</span>
  101. <input class='date' type="date" name="date" required='required'>
  102. </label>
  103. </div>
  104. <div class="item">
  105. <label>
  106. <span>Password</span>
  107. <input type="password" name="password" data-validate-length="6,8" required='required'>
  108. </label>
  109. <div class='tooltip help'>
  110. <span>?</span>
  111. <div class='content'>
  112. <b></b>
  113. <p>Should be of length 6 OR 8 characters</p>
  114. </div>
  115. </div>
  116. </div>
  117. <div class="item">
  118. <label>
  119. <span>Repeat password</span>
  120. <input type="password" name="password2" data-validate-linked='password' required='required'>
  121. </label>
  122. </div>
  123. <div class="item">
  124. <label>
  125. <span>Telephone</span>
  126. <input type="tel" class='tel' name="phone" required='required' data-validate-length-range="8,20">
  127. </label>
  128. <div class='tooltip help'>
  129. <span>?</span>
  130. <div class='content'>
  131. <b></b>
  132. <p>Notice that for a phone number user can input a '+' sign, a dash '-' or a space ' '</p>
  133. </div>
  134. </div>
  135. </div>
  136. <div class="item">
  137. <label>
  138. <span>Drop down selection</span>
  139. <select class="required" name="dropdown">
  140. <option value="">-- none --</option>
  141. <option value="o1">Option 1</option>
  142. <option value="o2">Option 2</option>
  143. <option value="o3">Option 3</option>
  144. </select>
  145. </label>
  146. <div class='tooltip help'>
  147. <span>?</span>
  148. <div class='content'>
  149. <b></b>
  150. <p>Choose something or choose not. what shall it be?</p>
  151. </div>
  152. </div>
  153. </div>
  154. <div class="item">
  155. <label>
  156. <span>url</span>
  157. <input name="url" placeholder="http://www.website.com" required="required" type="url" />
  158. </label>
  159. </div>
  160. <div class="item">
  161. <label>
  162. <span>Checkboxes</span>
  163. <label><input required="required" type="checkbox" />I agree</label>
  164. </label>
  165. </div>
  166. <div class="item multi required">
  167. <label for='multi_first'>
  168. <span>Multifield</span>
  169. </label>
  170. <div class='input'>
  171. <input type="text" name="multi1" maxlength='4' id='multi_first'>
  172. <input type="text" name="multi2" maxlength='4'>
  173. <input type="text" name="multi3" maxlength='4'>
  174. <input type="text" name="multi4" maxlength='4'>
  175. <input type="text" name="multi5" maxlength='4'>
  176. <input type="text" name="multi6" maxlength='4'>
  177. <input data-validate-length-range="24" data-validate-pattern="alphanumeric" id="serial" name="serial" type="hidden" required='required' />
  178. </div>
  179. <div class='tooltip help'>
  180. <span>?</span>
  181. <div class='content'>
  182. <b></b>
  183. <p>This is a multifield, which let the user input a serial number or credit card number for example, and the trick is to validate the combined result, which is stored in a hidden field</p>
  184. </div>
  185. </div>
  186. </div>
  187. <div class="item">
  188. <label>
  189. <span>File upload</span>
  190. <input type='file' required>
  191. </label>
  192. </div>
  193. <div class="item">
  194. <label>
  195. <span>message</span>
  196. <textarea required="required" name='message'></textarea>
  197. </label>
  198. </div>
  199. </fieldset>
  200. <fieldset>
  201. <p>There is a hidden "Required" form field below, notice it will not be validated due to its lack of visibility. The reason for this is that sometimes there is a section in a form that is not visible until some action is taken, but you do not want to change all those fields' "required" attributes on-the-fly, so that is why.</p>
  202. <input name="somethingHidden" required="required" type="text" style='display:none' />
  203. </fieldset>
  204. <button id='send' type='submit'>Submit</button>
  205. </form>
  206. </section>
  207. </div>
  208. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  209. <script src="multifield.js"></script>
  210. <script src="validator.js"></script>
  211. <script>
  212. // initialize the validator function
  213. validator.message['date'] = 'not a real date';
  214. // validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
  215. $('form')
  216. .on('blur', 'input[required], input.optional, select.required', validator.checkField)
  217. .on('change', 'select.required', validator.checkField)
  218. .on('keypress', 'input[required][pattern]', validator.keypress);
  219. $('.multi.required')
  220. .on('keyup blur', 'input', function(){
  221. validator.checkField.apply( $(this).siblings().last()[0] );
  222. });
  223. // bind the validation to the form submit event
  224. //$('#send').click('submit');//.prop('disabled', true);
  225. $('form').submit(function(e){
  226. e.preventDefault();
  227. var submit = true;
  228. // Validate the form using generic validaing
  229. if( !validator.checkAll( $(this) ) ){
  230. submit = false;
  231. }
  232. if( submit )
  233. this.submit();
  234. return false;
  235. });
  236. /* FOR DEMO ONLY */
  237. $('#vfields').change(function(){
  238. $('form').toggleClass('mode2');
  239. }).prop('checked',false);
  240. $('#alerts').change(function(){
  241. validator.defaults.alerts = (this.checked) ? false : true;
  242. if( this.checked )
  243. $('form .alert').remove();
  244. }).prop('checked',false);
  245. </script>
  246. </body>
  247. </html>