h5教程H5完成用户注册自动校验的实例详解

 所属分类:web前端开发

 浏览:188次-  评论: 0次-  更新时间:2022-10-28
描述:更多教程资料进入php教程获得。 Html5实现用户注册自动校验功能实例代码05-24 10:49:46作者:zzsucai.com抽时间写了一个带有自动校验功能...
更多教程资料进入php教程获得。

Html5实现用户注册自动校验功能实例代码

05-24 10:49:46作者:zzsucai.com

抽时间写了一个带有自动校验功能的Html5用户注册Demo。使用到Handlebars模板技术和手机验证码校验。

以下是效果截图:

前端(vue)入门到精通课程:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

1.页面代码:usersRegister.hbs

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>

  2. <!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->

  3. <!--[if IE 9 ]> <html lang="en" class="ie9"> <![endif]-->

  4. <!--[if (gt IE 9)|!(IE)]><!-->

  5. <html lang="en">

  6. <!--<![endif]-->

  7. <head>

  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  9. <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  10. <title>用户注册</title>

  11. <!--[if lt IE 9]>

  12. <script src="/assets/scripts/html5shiv.js"></script>

  13. <![endif]-->

  14. <link href="/assets/styles/jquery.idealforms.min.css" rel="stylesheet" media="screen" />

  15. <style type="text/css">

  16. body {

  17. font: normal 15px/1.5 Arial, Helvetica, Free Sans, sans-serif;

  18. color: #222;

  19. overflow-y: scroll;

  20. padding: 60px 0 0 0;

  21. }

  22. .main {

  23. width: 560px;

  24. height: 480px;

  25. margin: -50px auto;

  26. }

  27. #my-form {

  28. width: 560px;

  29. height: 450px;

  30. margin: 0 auto;

  31. border: 1px solid #ccc;

  32. padding: 3em;

  33. border-radius: 3px;

  34. box-shadow: 0 0 2px rgba(0, 0, 0, .2);

  35. }

  36. </style>

  37. <script type="text/javascript" src="/assets/scripts/jquery-1.8.2.min.js"></script>

  38. <script type="text/javascript" src="/assets/scripts/jquery.idealforms.js"></script>

  39. </head>

  40. <body>

  41. <!-- style="background-image: url(static/image/bg.jpg) -->

  42. <div class="main" >

  43. <div style="height:5px;text-align:center;font-size:25px"> 欢迎您注册!</div>

  44. <!-- Begin Form -->

  45. <form id="my-form" class="myform">

  46. <div>

  47. <label>用户名:</label><input id="username" name="username" type="text" />

  48. </div>

  49. <div>

  50. <!-- <label>密码:</label><input id="pass" name="password" type="password" /> -->

  51. <label>密码:</label><input id="pass" name="password" type="text" />

  52. </div>

  53. <div>

  54. <label>邮箱:</label><input id="email" name="email"

  55. data-ideal="required email" type="email" />

  56. </div>

  57. <div>

  58. <label>电话:</label><input id="telephone" type="text" name="phone" data-ideal="phone" />

  59. </div>

  60. <div>

  61. <label>供应商V码:</label><input id="vCode" type="text" name="vCode" data-ideal="vCode" />

  62. </div>

  63. <div>

  64. <label>真实姓名:</label><input id="trueName" type="text" name="trueName" data-ideal="trueName" />

  65. </div>

  66. <div>

  67. <label>手机验证码:</label><input id="telCode" type="text" name="telCode" data-ideal="telCode" />

  68. </div>

  69. <div style="margin-bottom:5px;">

  70. <button id="getTelCode" type="button" style="margin-left:160px; margin-right:auto;" >获取手机校验码</button>

  71. <hr style="margin-top:5px; margin-bottom:5px;" />

  72. </div>

  73. <!--<div>

  74. <label>性别:</label>

  75. <select id="sex" name="sex">

  76. <option value="男"></option>

  77. <option value="女"></option>

  78. </select>

  79. </div>

  80. <div>

  81. <label>昵称:</label><input id="nickName" type="text" name="nickName" data-ideal="nickName" />

  82. </div>

  83. <div>

  84. nbsp; <label>年龄:</label><input id="age" type="text" name="age" data-ideal="age" />

  85. </div>-->

  86. <!-- <div>

  87. <label>地址:</label><input type="text" name="address" data-ideal="address" />

  88. </div>

  89. <div>

  90. <label>QQ:</label><input type="text" name="qq" data-ideal="qq" />

  91. </div>

  92. <div>

  93. <label>邮编:</label><input type="text" name="zip" data-ideal="zip" />

  94. </div>

  95. <div>

  96. <label>传真:</label><input type="text" name="fax" data-ideal="fax" />

  97. </div>

  98. <div>

  99. <label>身份证:</label><input type="text" name="creditID" data-ideal="creditID" />

  100. </div>

  101. <div>

  102. <label>出生日期:</label><input name="date" class="datepicker"

  103. data-ideal="date" type="text" placeholder="月/日/年" />

  104. </div>

  105. <div>

  106. <label>上传头像:</label><input id="file" name="file" multiple

  107. type="file" />

  108. </div>

  109. <div>

  110. <label>个人主页:</label><input name="website" data-ideal="url"

  111. type="text" />

  112. </div>

  113. <div>

  114. <label>备注:</label>

  115. <textarea id="comments" name="comments"></textarea>

  116. </div>

  117. -->

  118. <!-- <div id="languages">

  119. <label>语言:</label> <label><input type="checkbox"

  120. name="langs[]" value="English" />英文</label> <label><input

  121. type="checkbox" name="langs[]" value="Chinese" />中文</label> <label><input

  122. type="checkbox" name="langs[]" value="Spanish" />西班牙文</label> <label><input

  123. type="checkbox" name="langs[]" value="French" />法文</label>

  124. </div>

  125. <div>

  126. <label>精通几门:</label> <label><input type="radio"

  127. name="radio" checked />1</label> <label><input type="radio"

  128. name="radio" />2</label> <label><input type="radio" name="radio" />3</label>

  129. <label><input type="radio" name="radio" />4</label>

  130. </div>

  131. <div>

  132. <label>国籍:</label> <select id="states" name="states">

  133. <option value="default">&ndash; 选择国籍 &ndash;</option>

  134. <option value="AL">阿拉伯</option>

  135. <option value="AK">中国</option>

  136. <option value="AZ">美国</option>

  137. <option value="AR">法国</option>

  138. <option value="CA">英国</option>

  139. <option value="CO">德国</option>

  140. <option value="CT">西班牙</option>

  141. <option value="DE">俄罗斯</option>

  142. </select>

  143. </div> -->

  144. <div style="margin-top:10px; margin-left:100px;margin-right:100px;">

  145. <button type="button" id="submit" class="submit">提交</button>

  146. <button id="reset" type="button" >重置</button>

  147. </div>

  148. </form>

  149. <!-- End Form -->

  150. </div>

  151. <script type="text/javascript">

  152. var options = {

  153. onFail : function() {

  154. alert($myform.getInvalid().length + ' invalid fields.')

  155. },

  156. inputs : {

  157. 'password' : {

  158. filters : 'required pass'

  159. },

  160. 'username' : {

  161. filters : 'required username'

  162. },

  163. 'email' : {

  164. filters : 'required email'

  165. },

  166. 'phone' : {

  167. filters : 'required phone'

  168. },

  169. 'trueName' : {

  170. filters : 'required'

  171. },

  172. 'vCode' : {

  173. filters : 'required'

  174. },

  175. 'telCode' : {

  176. filters : 'required'

  177. }

  178. /*

  179. 'age' : {

  180. filters : 'required digits',

  181. data : {

  182. min : 16,

  183. max : 70

  184. }

  185. },

  186. 'file' : {

  187. filters : 'extension',

  188. data : {

  189. extension : [ 'jpg' ]

  190. }

  191. },

  192. 'comments' : {

  193. filters : 'min max',

  194. data : {

  195. min : 50,

  196. max : 200

  197. }

  198. },

  199. 'states' : {

  200. filters : 'exclude',

  201. data : {

  202. exclude : [ 'default' ]

  203. },

  204. errors : {

  205. exclude : '选择国籍.'

  206. }

  207. },

  208. 'langs[]' : {

  209. filters : 'min max',

  210. data : {

  211. min : 2,

  212. max : 3

  213. },

  214. errors : {

  215. min : 'Check at least <strong>2</strong> options.',

  216. max : 'No more than <strong>3</strong> options allowed.'

  217. }

  218. }

  219. */

  220. }

  221. };

  222. $('#getTelCode').click(function() {

  223. var telephone = document.getElementById("telephone").value; //手机号码

  224. if (telephone == null || telephone == ""){

  225. alert("手机号码不能为空!");

  226. }

  227. else{

  228. $.ajax({

  229. type : "GET",

  230. dataType : "json",

  231. url : "../api/getTelCode?telephone="+ telephone,

  232. success : function(msg) {

  233. },

  234. error : function(e) {

  235. alert("获取手机校验码失败!" + e);

  236. }

  237. });

  238. }

  239. });

  240. var $myform = $('#my-form').idealforms(options).data('idealforms');

  241. $('#submit').click(function() {

  242. var username = document.getElementById("username").value; //用户名

  243. var password = document.getElementById("pass").value; //密码

  244. var email = document.getElementById("email").value; //邮箱

  245. var telephone = document.getElementById("telephone").value; //手机号码

  246. var vCode = document.getElementById("vCode").value; //公司V码

  247. var telCode = document.getElementById("telCode").value; //手机校验码

  248. var trueName = document.getElementById("trueName").value; //真实姓名

  249. $.ajax({

  250. type : "GET",

  251. url : "../api/usersRegister?username="+ username +"password="+ password +"email="+ email +"telephone="+ telephone +"vCode="+ vCode +"telCode="+ telCode +"trueName="+ trueName,

  252. success : function(msg) {

  253. //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp

  254. var curWwwPath = window.document.location.href;

  255. //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp

  256. var pathName = window.document.location.pathname;

  257. var pos = curWwwPath.indexOf(pathName);

  258. //获取主机地址,如: http://localhost:8083

  259. var localhostPaht = curWwwPath.substring(0, pos);

  260. //获取带"/"的项目名,如:/uimcardprj

  261. var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);

  262. window.location.href = projectName + "/login";

  263. alert("注册成功!");

  264. },

  265. error : function(e) {

  266. alert("注册失败!" + e);

  267. }

  268. });

  269. });

  270. $('#reset').click(function() {

  271. $myform.reset().fresh().focusFirst();

  272. });

  273. </script>

  274. </body>

  275. </html>

2.jq输入校验:jquery.idealforms.js

该js校验初始版本来自Cedric Ruiz,我略有修改。

部分校验的规则如下:

required: '此处是必填的.'

number: '必须是数字.',

digits: '必须是唯一的数字.'

name: '必须至少有3个字符长,并且只能包含字母.'

username: '用户名最短5位,最长30位,请使用英文字母、数字、中文和下划线. 用户名首字符必须为字母、数字、中文,不能为全数字.中文最长21个字.'

pass: '密码的位数必须的在6-15位之间,并且至少包含一个数字,一个大写字母和一个小写字母.'

strongpass: '必须至少为8个字符长,至少包含一个大写字母和一个小写字母和一个数字或特殊字符.'

email: '必须是一个有效的email地址. <em>(例: '">user@gmail.com)</em>'

phone: '必须是一个有效的手机号码. <em>(例: 18723101212)</em>'

以下是整个代码文件:

XML/HTML Code复制内容到剪贴板
  1. /*--------------------------------------------------------------------------

  2. jq-idealforms 2.1

  3. * Author: Cedric Ruiz

  4. * License: GPL or MIT

  5. * Demo: http://elclanrs.github.com/jq-idealforms/

  6. *

  7. --------------------------------------------------------------------------*/

  8. ;(function ( $, window, document, undefined ) {

  9. 'use strict';

  10. // Global Ideal Forms namespace

  11. $.idealforms = {}

  12. $.idealforms.filters = {}

  13. $.idealforms.errors = {}

  14. $.idealforms.flags = {}

  15. $.idealforms.ajaxRequests = {}

  16. /*--------------------------------------------------------------------------*/

  17. /**

  18. * @namespace A chest for various Utils

  19. */

  20. var Utils = {

  21. /**

  22. * Get width of widest element in the collection.

  23. * @memberOf Utils

  24. * @param {jQuery object} $elms

  25. * @returns {number}

  26. */

  27. getMaxWidth: function( $elms ) {

  28. var maxWidth = 0

  29. $elms.each(function() {

  30. var width = $(this).outerWidth()

  31. if ( width > maxWidth ) {

  32. maxWidth = width

  33. }

  34. })

  35. return maxWidth

  36. },

  37. /**

  38. * Hacky way of getting LESS variables

  39. * @memberOf Utils

  40. * @param {string} name The name of the LESS class.

  41. * @param {string} prop The css property where the data is stored.

  42. * @returns {number, string}

  43. */

  44. getLessVar: function( name, prop ) {

  45. var value = $('<p class="' + name + '"></p>').hide().appendTo('body').css( prop )

  46. $('.' + name).remove()

  47. return ( /^\d+/.test( value ) ? parseInt( value, 10 ) : value )

  48. },

  49. /**

  50. * Like ES5 Object.keys

  51. */

  52. getKeys: function( obj ) {

  53. var keys = []

  54. for(var key in obj) {

  55. if ( obj.hasOwnProperty( key ) ) {

  56. keys.push( key )

  57. }

  58. }

  59. return keys

  60. },

  61. // Get lenght of an object

  62. getObjSize: function( obj ) {

  63. var size = 0, key;

  64. for ( key in obj ) {

  65. if ( obj.hasOwnProperty( key ) ) {

  66. size++;

  67. }

  68. }

  69. return size;

  70. },

  71. isFunction: function( obj ) {

  72. return typeof obj ===&nbspnbsp;'function'

  73. },

  74. isRegex: function( obj ) {

  75. return obj instanceof RegExp

  76. },

  77. isString: function( obj ) {

  78. return typeof obj === 'string'

  79. },

  80. getByNameOrId: function( str ) {

  81. var $el = $('[name="'+ str +'"]').length

  82. ? $('[name="'+ str +'"]') // by name

  83. : $('#'+ str) // by id

  84. return $el.length

  85. ? $el

  86. : $.error('The field "'+ str + '" doesn\'t exist.')

  87. },

  88. getFieldsFromArray: function( fields ) {

  89. var f = []

  90. for ( var i = 0, l = fields.length; i < l; i++ ) {

  91. f.push( Utils.getByNameOrId( fields[i] ).get(0) )

  92. }

  93. return $( f )

  94. },

  95. convertToArray: function( obj ) {

  96. return Object.prototype.toString.call( obj ) === '[object Array]'

  97. ? obj : [ obj ]

  98. },

  99. /**

  100. * Determine type of any Ideal Forms element

  101. * @param $input jQuery $input object

  102. */

  103. getIdealType: function( $el ) {

  104. var type = $el.attr('type') || $el[0].tagName.toLowerCase()

  105. return (

  106. /(text|password|email|number|search|url|tel|textarea)/.test( type ) && 'text' ||

  107. /file/.test( type ) && 'file' ||

  108. /select/.test( type ) && 'select' ||

  109. /(radio|checkbox)/.test( type ) && 'radiocheck' ||

  110. /(button|submit|reset)/.test( type ) && 'button' ||

  111. /h\d/.test( type ) && 'heading' ||

  112. /hr/.test( type ) && 'separator' ||

  113. /hidden/.test( type ) && 'hidden'

  114. )

  115. },

  116. /**

  117. * Generates an input

  118. * @param name `name` attribute of the input

  119. * @param type `type` or `tagName` of the input

  120. */

  121. makeInput: function( name, value, type, list, placeholder ) {

  122. var markup, items = [], item, i, len

  123. function splitValue( str ) {

  124. var item, value, arr

  125. if ( /::/.test( str ) ) {

  126. arr = str.split('::')

  127. item = arr[ 0 ]

  128. value = arr[ 1 ]

  129. } else {

  130. item = value = str

  131. }

  132. return { item: item, value: value }

  133. }

  134. // Text & file

  135. if ( /^(text|password|email|number|search|url|tel|file|hidden)$/.test(type) )

  136. markup = '<input '+

  137. 'type="'+ type +'" '+

  138. 'id="'+ name +'" '+

  139. 'name="'+ name +'" '+

  140. 'value="'+ value +'" '+

  141. (placeholder && 'placeholder="'+ placeholder +'"') +

  142. '/>'

  143. // Textarea

  144. if ( /textarea/.test( type ) ) {

  145. markup = '<textarea id="'+ name +'" name="'+ name +'" value="'+ value +'"></textarea>'

  146. }

  147. // Select

  148. if ( /select/.test( type ) ) {

  149. items = []

  150. for ( i = 0, len = list.length; i < len; i++ ) {

  151. item = splitValue( list[ i ] ).item

  152. value = splitValue( list[ i ] ).value

  153. items.push('<option value="'+ value +'">'+ item +'</option>')

  154. }

  155. markup =

  156. '<select id="'+ name +'" name="'+ name +'">'+

  157. items.join('') +

  158. '</select>'

  159. }

  160. // Radiocheck

  161. if ( /(radio|checkbox)/.test( type ) ) {

  162. items = []

  163. for ( i = 0, len = list.length; i < len; i++ ) {

  164. item = splitValue( list[ i ] ).item

  165. value = splitValue( list[ i ] ).value

  166. items.push(

  167. '<label>'+

  168. '<input type="'+ type +'" name="'+ name +'" value="'+ value +'" />'+

  169. item +

  170. '</label>'

  171. )

  172. }

  173. markup = items.join('')

  174. }

  175. return markup

  176. }

  177. }

  178. /**

  179. * Custom tabs for Ideal Forms

  180. */

  181. $.fn.idealTabs = function (container) {

  182. var

  183. // Elements

  184. $contents = this,

  185. $containercontainer = container,

  186. $wrapper = $('<ul class="ideal-tabs-wrap"/>'),

  187. $tabs = (function () {

  188. var tabs = []

  189. $contents.each(function () {

  190. var name = $(this).attr('name')

  191. var html =

  192. '<li class="ideal-tabs-tab">'+

  193. '<span>' + name + '</span>'+

  194. '<i class="ideal-tabs-tab-counter ideal-tabs-tab-counter-zero">0</i>'+

  195. '</li>'

  196. tabs.push(html)

  197. })

  198. return $(tabs.join(''))

  199. }()),

  200. Actions = {

  201. getCurIdx: function () {

  202. return $tabs

  203. .filter('.ideal-tabs-tab-active')

  204. .index()

  205. },

  206. getTabIdxByName: function (name) {

  207. var re = new RegExp(name, 'i')

  208. var $tab = $tabs.filter(function () {

  209. return re.test($(this).text())

  210. })

  211. return $tab.index()

  212. }

  213. },

  214. /**

  215. * Public methods

  216. */

  217. Methods = {

  218. /**

  219. * Switch tab

  220. */

  221. switchTab: function (nameOrIdx) {

  222. var idx = Utils.isString(nameOrIdx)

  223. ? Actions.getTabIdxBy

积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!