<span>/*</span><span> ******* 环境: Apache2.2.8 + PHP5.2.6 + MySQL5.0.51b + jQuery-1.8.3.min.js *******<br />******* 其他组件:Zend_mail( Zend_framework 1.11.11 )<br />******* Date:2014-09-25<br />******* Author:小dee<br />******* Blog:http://www.cnblogs.com/dee0912/<br /></span><span>*/</span>
写了一个简单的PHP+jQuery注册模块,需要填写的栏目包括用户名、邮箱、密码、重复密码和验证码,其中每个栏目需要具备的功能和要求如下图:
在做这个模块的时候,很大程度上借鉴了网易注册( http://reg.163.com/reg/reg.jsp?product=urs )的功能和样式。但网易对于每个栏目的判断的做法是:在输入文字时,并不给出任何实时的检测结果,而在这个栏目失去焦点时,才把检测的结果展示出来,这种做法我认为会使用户在输入时视觉上比较统一,看到的是关于该栏目要求的提示,不会出现其他信息的打扰,但同时也不会得到正在输入的字符的检测提示。所以在做这个功能的时候,我把我自认为需要实时提示的一些信息做了相应的加强,比如用户名长度超过限制和密码的长度以及强弱,并且给邮箱的大写锁定做了简单的判断。
注:表单的提交按钮type为button而不是submit,因此所有栏目的回车( keydown )都统一设置为将焦点移至下一个栏目,除了最后一个栏目验证码,在验证码栏目使用回车( keydown )会触发提交事件。
功能分析
用户名栏目:
流程
①页面加载完即获得焦点,获得焦点时出现初始说明文字;
②鼠标点击用户名输入框,出现初始说明文字;
③输入字符,即时提示是否符合长度要求;
④失去焦点时首先判断是否为空,为空时提示不能为空;非空且长度满足要求时,开始检测用户名是否被注册;
⑤用户名已被注册,给出提示,如果没有注册,则提示可以注册;
⑥再次获得焦点时,不论输入框中是否有输入,或是否输入符合规定,都出现初始说明文字
⑦回车时将焦点移至邮箱栏目
如图:
细节
可以使用任意字符,并且字数限制为:中文长度不超过7个汉字,英文、数字或符号长度不超过14个字母、数字或符号(类似豆瓣注册https://www.douban.com/accounts/register),即不超过14个字符
关于占位符(字符长度),一个汉字的占位符是2,一个英文(数字)的占位符是1,可以用php语句来计算字符的长度
<span>1</span> <?<span>php </span><span>2</span> <span>//</span><span>php.ini开启了php_mbstring.dll扩展</span> <span>3</span> <span>$str</span>="博客园小dee"<span>; </span><span>4</span> <span>5</span> <span>echo</span> (<span>strlen</span>(<span>$str</span>)+mb_strlen(<span>$str</span>,'utf-8'))/2;
输出:11
而strlen($str) 输出的是15:4*3+3,汉字在utf-8编码下占3个字节,英文占1个,
mb_strlen($str,'utf-8') 输出的是7:一个汉字的长度是1,
如果用jquery的length来输出这个字符串,alert($("#uname").val().length),则会得到长度7,
这点要注意。
同时用户名两端不能含有空格,在检测以及注册时,程序会自动过滤用户名两端的空格。
register.html 用户名栏目的HTML代码片段:
<span>1</span> <span><!--</span><span> 用户名 </span><span>--></span> <span>2</span> <span><</span><span>div </span><span>class</span><span>="ipt fipt"</span><span>></span> <span>3</span> <span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="uname"</span><span> id</span><span>="uname"</span><span> value</span><span>=""</span><span> placeholder</span><span>="输入用户名"</span><span> autocomplete</span><span>="off"</span> <span>/></span> <span>4</span> <span><!--</span><span>提示文字</span><span>--></span> <span>5</span> <span><</span><span>span </span><span>id</span><span>="unamechk"</span><span>></</span><span>span</span><span>></span> <span>6</span> <span></</span><span>div</span><span>></span>
register.js公用部分的js代码:
<span> 1</span> $(<span>function</span><span>(){ </span><span> 2</span> <span> 3</span> <span>//</span><span>说明文字</span> <span> 4</span> <span>function</span><span> notice(showMsg,noticeMsg){ </span><span> 5</span> showMsg.html(noticeMsg).attr("class","notice"<span>); </span><span> 6</span> <span> } </span><span> 7</span> <span>//</span><span>显示错误信息</span> <span> 8</span> <span>function</span><span> error(showMsg,errorMsg){ </span><span> 9</span> showMsg.html(errorMsg).attr("class","error"<span>); </span><span>10</span> <span> } </span><span>11</span> <span>//</span><span>显示正确信息</span> <span>12</span> <span>function</span><span> success(showMsg,successMsg){ </span><span>13</span> <span> showMsg.html(successMsg) </span><span>14</span> .css("height","20px"<span>) </span><span>15</span> .attr("class","success"<span>); </span><span>16</span> <span> } </span><span>17</span> <span>18</span> <span>//</span><span>计算字符长度</span> <span>19</span> <span>function</span><span> countLen(value){ </span><span>20</span> <span>21</span> <span>var</span> len = 0<span>; </span><span>22</span> <span>for</span> (<span>var</span> i = 0; i < value.length; i++<span>) { </span><span>23</span> <span>if</span> (value[i].match(/[^\x00-\xff]/ig) != <span>null</span><span>) </span><span>24</span> len += 2<span>; </span><span>25</span> <span>else</span> <span>26</span> len += 1<span>; </span><span>27</span> <span> } </span><span>28</span> <span>return</span><span> len; </span><span>29</span> <span>} </span><span>30</span> <span>31</span> <span>//</span><span>......</span> <span>32</span> )};
register.js用户名部分的js代码:
<span> 1</span> <span>//</span><span>检测用户名长度</span> <span> 2</span> <span>function</span><span> unameLen(value){ </span><span> 3</span> <span> 4</span> <span>var</span> showMsg = $("#unamechk"<span>); </span><span> 5</span> <span> 6</span> <span>/*</span><span> (strlen($str)+mb_strlen($str))/2 可得出限制字符长度的上限, </span><span> 7</span> <span> * 例如:$str为7个汉字:"博客园记录生活",利用上面的语句可得出14, </span><span> 8</span> <span> * 同样,14个英文,利用上面的语句同样能得出字符长度为14 </span><span> 9</span> <span>*/</span> <span>10</span> <span>if</span>(countLen(value) > 14<span>){ </span><span>11</span> <span>12</span> <span>var</span> errorMsg = '用户名长度不能超过14个英文或7个汉字'<span>; </span><span>13</span> <span> error(showMsg,errorMsg); </span><span>14</span> }<span>else</span> <span>if</span>(countLen(value) == 0<span>){ </span><span>15</span> <span>16</span> <span>var</span> noticeMsg = '用户名不能为空'<span>; </span><span>17</span> <span> notice(showMsg,noticeMsg); </span><span>18</span> }<span>else</span><span>{ </span><span>19</span> <span>20</span> <span>var</span> successMsg = '长度符合要求'<span>; </span><span>21</span> <span> success(showMsg,successMsg); </span><span>22</span> <span> } </span><span>23</span> <span>24</span> <span>return</span><span> countLen(value); </span><span>25</span> <span> } </span><span>26</span> <span>27</span> <span>//</span><span>用户名</span> <span>28</span> unameLen($("#uname"<span>).val()); </span><span>29</span> <span>30</span> $("#uname").focus(<span>function</span><span>(){ </span><span>31</span> <span>32</span> <span>var</span> noticeMsg = '中英文均可,最长为14个英文或7个汉字'<span>; </span><span>33</span> notice($("#unamechk"<span>),noticeMsg); </span><span>34</span> <span> }) </span><span>35</span> .click(<span>function</span><span>(){ </span><span>36</span> <span>37</span> <span>var</span> noticeMsg = '中英文均可,最长为14个英文或7个汉字'<span>; </span><span>38</span> notice($("#unamechk"<span>),noticeMsg); </span><span>39</span> <span> }) </span><span>40</span> .keyup(<span>function</span><span>(){ </span><span>41</span> <span>42</span> unameLen(<span>this</span><span>.value); </span><span>43</span> }).keydown(<span>function</span><span>(){ </span><span>44</span> <span>45</span> <span>//</span><span>把焦点移至邮箱栏目</span> <span>46</span> <span>if</span>(event.keyCode == 13<span>){ </span><span>47</span> <span>48</span> $("#uemail"<span>).focus(); </span><span>49</span> <span> } </span><span>50</span> <span> }) </span><span>51</span> .blur(<span>function</span><span>(){ </span><span>52</span> <span>53</span> <span>if</span>($("#uname").val()!="" && unameLen(<span>this</span>.value)<=14 && unameLen(<span>this</span>.value)>0<span>){ </span><span>54</span> <span>//</span><span>检测中</span> <span>55</span> $("#unamechk").html("检测中...").attr("class","loading"<span>); </span><span>56</span> <span>//</span><span>ajax查询用户名是否被注册</span> <span>57</span> $.post("./../chkname.php"<span>,{ </span><span>58</span> <span>59</span> <span>//</span><span>要传递的数据</span> <span>60</span> uname : $("#uname"<span>).val() </span><span>61</span> },<span>function</span><span>(data,textStatus){ </span><span>62</span> <span>63</span> <span>if</span>(data == 0<span>){ </span><span>64</span> <span>65</span> <span>var</span> successMsg = '恭喜,该用户名可以注册'<span>; </span><span>66</span> $("#unamechk").html(successMsg).attr("class","success"<span>); </span><span>67</span> <span>68</span> <span>//</span><span>设置参数</span> <span>69</span> nameval = <span>true</span><span>; </span><span>70</span> }<span>else</span> <span>if</span>(data == 1<span>){ </span><span>71</span> <span>72</span> <span>var</span> errorMsg = '该用户名已被注册'<span>; </span><span>73</span> error($("#unamechk"<span>),errorMsg); </span><span>74</span> }<span>else</span><span>{ </span><span>75</span> <span>76</span> <span>var</span> errorMsg = '查询出错,请联系网站管理员'<span>; </span><span>77</span> error($("#unamechk"<span>),errorMsg); </span><span>78</span> <span> } </span><span>79</span> <span> }); </span><span>80</span> }<span>else</span> <span>if</span>(unameLen(<span>this</span>.value)>14<span>){ </span><span>81</span> <span>82</span> <span>var</span> errorMsg = '用户名长度不能超过14个英文或7个汉字'<span>; </span><span>83</span> error($("#unamechk"<span>),errorMsg); </span><span>84</span> }<span>else</span><span>{ </span><span>85</span> <span>86</span> <span>var</span> errorMsg = '用户名不能为空'<span>; </span><span>87</span> error($("#unamechk"<span>),errorMsg); </span><span>88</span> <span> } </span><span>89</span> <span>}); </span><span>90</span> <span>91</span> <span>//</span><span>加载后即获得焦点</span> <span>92</span> $("#uname").focus();
checkname.php代码:
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>header</span>("charset=utf-8"<span>); </span><span> 4</span> <span> 5</span> <span>require_once</span>("conn/conn.php"<span>); </span><span> 6</span> <span> 7</span> <span>if</span>(<span>isset</span>(<span>$_POST</span>['uname']) && <span>$_POST</span>['uname']!=""<span>){ </span><span> 8</span> <span> 9</span> <span>$uname</span> = <span>trim</span>(<span>addslashes</span>(<span>$_POST</span>['uname'<span>])); </span><span>10</span> <span> } </span><span>11</span> <span>12</span> <span>$sql</span> = "select uname from user where uname='".<span>$uname</span>."'"<span>; </span><span>13</span> <span>14</span> <span>if</span>(<span>$conne</span>->getRowsNum(<span>$sql</span>) == 1<span>){ </span><span>15</span> <span>16</span> <span>$state</span> = 1<span>; </span><span>17</span> }<span>else</span> <span>if</span>(<span>$conne</span>->getRowsNum(<span>$sql</span>) == 0<span>){ </span><span>18</span> <span>19</span> <span>$state</span> = 0<span>; </span><span>20</span> }<span>else</span><span>{ </span><span>21</span> <span>22</span> <span>echo</span> <span>$conne</span>-><span>msg_error(); </span><span>23</span> <span> } </span><span>24</span> <span>25</span> <span>echo</span> <span>$state</span>;
提示文字( Chrome下 )
①初始获得焦点、再次获得焦点或点击时
②输入时实时检测长度
③删除至空且未失去焦点时,使用蓝色图标提示不能为空——用户在输入时看起来不突兀
④失去焦点且不为空,检测是否被注册( 非常短暂,一闪而过 )
⑤失去焦点时为空、可以注册、已被注册时
用户名分析至此完毕。
邮箱栏目:
流程
①当栏目获得焦点或者点击时不论栏目为空、填写正确或者填写错误时都出现说明文字;
②用户输入时出现下拉菜单显示多种邮件后缀供用户选择;
③失去焦点时首先判断邮箱格式是否正确,如果正确则检测邮箱是否被注册 ;
④在使用回车选择下拉菜单时,将自动填充邮箱栏目;没有出现下拉菜单时,将焦点移至密码栏目
如图:
register.html 邮箱栏目HTML代码片段:
<span>1</span> <span><!--</span><span> email </span><span>--></span> <span>2</span> <span><</span><span>div </span><span>class</span><span>="ipt"</span><span>></span> <span>3</span> <span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="uemail"</span><span> id</span><span>="uemail"</span><span> value</span><span>=""</span><span> placeholder</span><span>="常用邮箱地址"</span> <span>/></span> <span>4</span> <span><</span><span>span </span><span>id</span><span>="uemailchk"</span><span>></</span><span>span</span><span>></span> <span>5</span> <span><</span><span>ul </span><span>class</span><span>="autoul"</span><span>></</span><span>ul</span><span>></span> <span>6</span> <span></</span><span>div</span><span>></span>
下拉功能emailup.js同之前的博文《jQuery实现下拉提示且自动填充的邮箱》,略有修改,注意用回车( keydown和keyup事件 )在不同情况下触发的不同动作:
1 $(function(){ 2 3 //初始化邮箱列表 4 var mail = new Array("sina.com","126.com","163.com","gmail.com","qq.com","hotmail.com","sohu.com","139.com","189.cn","sina.cn"); 5 6 //把邮箱列表加入下拉 7 for(var i=0;iregister.js邮箱代码片段:
<span>//</span><span>邮箱下拉js单独引用emailup.js</span> $("#uemail").focus(<span>function</span><span>(){ </span><span>var</span> noticeMsg = '用来登陆网站,接收到激活邮件才能完成注册'<span>; notice($(</span>"#uemailchk"<span>),noticeMsg); }) .click(</span><span>function</span><span>(){ </span><span>var</span> noticeMsg = '用来登陆网站,接收到激活邮件才能完成注册'<span>; notice($(</span>"#uemailchk"<span>),noticeMsg); }) .blur(</span><span>function</span><span>(){ </span><span>if</span>(<span>this</span>.value!="" && <span>this</span>.value.match(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/)!=<span>null</span><span>){ </span><span>//</span><span>检测是否被注册</span> $("#uemailchk").html("检测中...").attr("class","loading"<span>); </span><span>//</span><span>ajax查询用户名是否被注册</span> $.post("./../chkemail.php"<span>,{ </span><span>//</span><span>要传递的数据</span> uemail : $("#uemail"<span>).val() },</span><span>function</span><span>(data,textStatus){ </span><span>if</span>(data == 0<span>){ </span><span>var</span> successMsg = '恭喜,该邮箱可以注册'<span>; $(</span>"#uemailchk").html(successMsg).attr("class","success"<span>); emailval </span>= <span>true</span><span>; }</span><span>else</span> <span>if</span>(data == 1<span>){ </span><span>var</span> errorMsg = '该邮箱已被注册'<span>; error($(</span>"#uemailchk"<span>),errorMsg); }</span><span>else</span><span>{ </span><span>var</span> errorMsg = '查询出错,请联系网站管理员'<span>; error($(</span>"#uemailchk"<span>),errorMsg); } }); }</span><span>else</span> <span>if</span>(<span>this</span>.value == ""<span>){ </span><span>var</span> errorMsg = '邮箱不能为空'<span>; error($(</span>"#uemailchk"<span>),errorMsg); }</span><span>else</span><span>{ </span><span>var</span> errorMsg = '请填写正确的邮箱地址'<span>; $(</span>"#uemailchk").html(errorMsg).attr("class","error"<span>); } });</span>
提示文字( Chrome下 )
①获得焦点时、点击时
②输入时
③失去焦点为空、格式错误、已被注册、可以注册时分别为
邮箱功能至此结束。
密码栏目:
要求
①6-16个个字符,区分大小写(参考豆瓣和网易)
②密码不能为同一字符
③实时提示是否符合要求以及判断并显示密码强度,:
1.输入时如果为空(删除时)则用蓝色符号提示不能为空,超过长度时用红色符号
2.密码满足长度但是为相同字符的组合时:密码太简单,请尝试数字、字母和下划线的组合
3.密码强度判断有多种规则,有直接依据长度和组合规则作出判断,也有给每种长度和组合设置分数,通过验证实际密码的情况计算出最后分数来判断强弱。在这个模块中采用比较简单的一种形式,也是网易注册采用的方法:
密码满足长度且全部为不同字母、全部为不同数字或全部为不同符号时为弱:弱:试试字母、数字、符号混搭
密码满足长度且为数字、字母和符号任意两种组合时为中
密码满足长度且为数字、字母和符号三种组合时为强
④输入时大写提示
如图:
register.html 密码栏目HTML代码片段:
<span>1</span> <span><</span><span>div </span><span>class</span><span>="ipt"</span><span>></span> <span>2</span> <span><</span><span>input </span><span>type</span><span>="password"</span><span> name</span><span>="upwd"</span><span> id</span><span>="upwd"</span><span> value</span><span>=""</span><span> placeholder</span><span>="设置密码"</span> <span>/></span> <span>3</span> <span><</span><span>div </span><span>class</span><span>="upwdpic"</span><span>></span> <span>4</span> <span><</span><span>span </span><span>id</span><span>="upwdchk"</span><span>></</span><span>span</span><span>></span> <span>5</span> <span><</span><span>img </span><span>id</span><span>="pictie"</span> <span>/></span> <span>6</span> <span></</span><span>div</span><span>></span> <span>7</span> <span></</span><span>div</span><span>></span>
register.js密码代码片段:
<span> 1</span> <span>function</span><span> noticeEasy(){ </span><span> 2</span> <span> 3</span> <span>//</span><span>密码全部为相同字符或者为123456,用于keyup时的notice</span> <span> 4</span> <span>var</span> noticeMsg = '密码太简单,请尝试数字、字母和下划线的组合'<span>; </span><span> 5</span> <span>return</span> notice($("#upwdchk"<span>),noticeMsg); </span><span> 6</span> <span> } </span><span> 7</span> <span> 8</span> <span>function</span><span> errorEasy(){ </span><span> 9</span> <span> 10</span> <span>//</span><span>密码全部为相同字符或者为123456,用于blur时的error</span> <span> 11</span> <span>var</span> errorMsg = '密码太简单,请尝试数字、字母和下划线的组合'<span>; </span><span> 12</span> <span>return</span> error($("#upwdchk"<span>),errorMsg); </span><span> 13</span> <span> } </span><span> 14</span> <span> 15</span> <span>//</span><span>检测密码长度函数</span> <span> 16</span> <span>//</span><span>检测密码长度</span> <span> 17</span> <span>function</span><span> upwdLen(value,func){ </span><span> 18</span> <span> 19</span> <span>var</span> showMsg = $("#upwdchk"<span>); </span><span> 20</span> <span> 21</span> <span>if</span>(countLen(value) > 16<span>){ </span><span> 22</span> <span> 23</span> <span>var</span> errorMsg = '密码不能超过16个字符'<span>; </span><span> 24</span> <span> error(showMsg,errorMsg); </span><span> 25</span> <span> 26</span> $("#pictie"<span>).hide(); </span><span> 27</span> }<span>else</span> <span>if</span>(countLen(value) < 6<span>){ </span><span> 28</span> <span> 29</span> <span>//</span><span>使用notice更加友好</span> <span> 30</span> <span>var</span> noticeMsg = '密码不能少于6个字符'<span>; </span><span> 31</span> <span> notice(showMsg,noticeMsg); </span><span> 32</span> <span> 33</span> $("#pictie"<span>).hide(); </span><span> 34</span> }<span>else</span> <span>if</span>(countLen(value) == 0<span>){ </span><span> 35</span> <span> 36</span> <span>//</span><span>使用notice更加友好</span> <span> 37</span> <span>var</span> noticeMsg = '密码不能为空'<span>; </span><span> 38</span> <span> notice(showMsg,noticeMsg); </span><span> 39</span> <span> 40</span> $("#pictie"<span>).hide(); </span><span> 41</span> }<span>else</span><span>{ </span><span> 42</span> <span> 43</span> upwdStrong(value,func);<span>//</span><span>如果长度不成问题,则调用检测密码强弱</span> <span> 44</span> <span> } </span><span> 45</span> <span> 46</span> <span>return</span> countLen(value);<span>//</span><span>返回字符长度</span> <span> 47</span> <span> } </span><span> 48</span> <span> 49</span> <span>//</span><span>检测密码强弱</span> <span> 50</span> <span>function</span><span> upwdStrong(value,func){ </span><span> 51</span> <span> 52</span> <span>var</span> showMsg = $("#upwdchk"<span>); </span><span> 53</span> <span> 54</span> <span>if</span>(value.match(/^(.)\1*$/)!=<span>null</span> || value.match(/^123456$/<span>)){ </span><span> 55</span> <span> 56</span> <span>//</span><span>密码全部为相同字符或者为123456,调用函数noticeEasy或errorEasy</span> <span> 57</span> <span> func; </span><span> 58</span> }<span>else</span> <span>if</span>(value.match(/^[A-Za-z]+$/)!=<span>null</span> || value.match(/^\d+$/)!=<span>null</span> || value.match(/^[^A-Za-z0-9]+$/)!=<span>null</span><span>){ </span><span> 59</span> <span> 60</span> <span>//</span><span>全部为相同类型的字符为弱</span> <span> 61</span> <span>var</span> successMsg = '弱:试试字母、数字、符号混搭'<span>; </span><span> 62</span> <span> success(showMsg,successMsg); </span><span> 63</span> <span> 64</span> <span>//</span><span>插入强弱条</span> <span> 65</span> $("#pictie").show().attr("src","images/weak.jpg"<span>); </span><span> 66</span> <span> 67</span> pwdval = <span>true</span><span>; </span><span> 68</span> <span> 69</span> }<span>else</span> <span>if</span>(value.match(/^[^A-Za-z]+$/)!=<span>null</span> || value.match(/^[^0-9]+$/)!=<span>null</span> || value.match(/^[a-zA-Z0-9]+$/)!=<span>null</span><span>){ </span><span> 70</span> <span> 71</span> <span>//</span><span>任意两种不同类型字符组合为中强( 数字+符号,字母+符号,数字+字母 )</span> <span> 72</span> <span>var</span> successMsg = '中强:试试字母、数字、符号混搭'<span>; </span><span> 73</span> <span> success(showMsg,successMsg); </span><span> 74</span> <span> 75</span> $("#pictie").show().attr("src","images/normal.jpg"<span>); </span><span> 76</span> <span> 77</span> pwdval = <span>true</span><span>; </span><span> 78</span> }<span>else</span><span>{ </span><span> 79</span> <span> 80</span> <span>//</span><span>数字、字母和符号混合</span> <span> 81</span> <span>var</span> successMsg = '强:请牢记您的密码'<span>; </span><span> 82</span> <span> success(showMsg,successMsg); </span><span> 83</span> <span> 84</span> $("#pictie").show().attr("src","images/strong.jpg"<span>); </span><span> 85</span> <span> 86</span> pwdval = <span>true</span><span>; </span><span> 87</span> <span> } </span><span> 88</span> <span> } </span><span> 89</span> <span> 90</span> $upper = $("<div id=\"upper\">大写锁定已打开</div>"<span>); </span><span> 91</span> <span> 92</span> $("#upwd").focus(<span>function</span><span>(){ </span><span> 93</span> <span> 94</span> <span>var</span> noticeMsg = '6到16个字符,区分大小写'<span>; </span><span> 95</span> notice($("#upwdchk"<span>),noticeMsg); </span><span> 96</span> <span> 97</span> $("#pictie"<span>).hide(); </span><span> 98</span> <span> }) </span><span> 99</span> .click(<span>function</span><span>(){ </span><span>100</span> <span>101</span> <span>var</span> noticeMsg = '6到16个字符,区分大小写'<span>; </span><span>102</span> notice($("#upwdchk"<span>),noticeMsg); </span><span>103</span> <span>104</span> $("#pictie"<span>).hide(); </span><span>105</span> }).keydown(<span>function</span><span>(){ </span><span>106</span> <span>107</span> <span>//</span><span>把焦点移至邮箱栏目</span> <span>108</span> <span>if</span>(event.keyCode == 13<span>){ </span><span>109</span> <span>110</span> $("#rupwd"<span>).focus(); </span><span>111</span> <span> } </span><span>112</span> <span> }) </span><span>113</span> .keyup(<span>function</span><span>(){ </span><span>114</span> <span>115</span> <span>//</span><span>判断大写是否开启</span> <span>116</span> <span>//</span><span>输入密码的长度</span> <span>117</span> <span>var</span> len = <span>this</span><span>.value.length; </span><span>118</span> <span>if</span>(len!=0<span>){ </span><span>119</span> <span>120</span> <span>//</span><span>当输入的最新以为含有大写字母时说明开启了大写锁定</span> <span>121</span> <span>if</span>(<span>this</span>.value[len-1].match(/[A-Z]/)!=<span>null</span><span>){ </span><span>122</span> <span>123</span> <span>//</span><span>给出提示</span> <span>124</span> $upper.insertAfter($(".upwdpic"<span>)); </span><span>125</span> }<span>else</span><span>{ </span><span>126</span> <span>127</span> <span>//</span><span>移除提示</span> <span>128</span> <span> $upper.remove(); </span><span>129</span> <span> } </span><span>130</span> }<span>else</span><span>{ </span><span>131</span> <span>132</span> <span>//</span><span>当密码框为空时移除提示</span> <span>133</span> <span>if</span><span>($upper){ </span><span>134</span> <span>135</span> <span> $upper.remove(); </span><span>136</span> <span> } </span><span>137</span> }<span>//</span><span>判断大写开启结束</span> <span>138</span> <span>139</span> <span>//</span><span>判断长度及强弱</span> <span>140</span> upwdLen(<span>this</span><span>.value,noticeEasy()); </span><span>141</span> <span> }) </span><span>142</span> <span>//</span><span>keyup事件结束</span> <span>143</span> .blur(<span>function</span><span>(){ </span><span>144</span> <span>145</span> upwdLen(<span>this</span><span>.value,errorEasy()); </span><span>146</span> <span>//</span><span>upwdLen函数中部分提示使用notice是为了keyup事件中不出现红色提示,而blur事件中则需使用error标红</span> <span>147</span> <span>if</span>(<span>this</span>.value == ""<span>){ </span><span>148</span> <span>149</span> <span>var</span> errorMsg = '密码不能为空'<span>; </span><span>150</span> error($("#upwdchk"<span>),errorMsg); </span><span>151</span> <span>152</span> $("#pictie"<span>).hide(); </span><span>153</span> }<span>else</span> <span>if</span>(countLen(<span>this</span>.value)<6<span>){ </span><span>154</span> <span>155</span> <span>var</span> errorMsg = '密码不能少于6个字符'<span>; </span><span>156</span> error($("#upwdchk"<span>),errorMsg); </span><span>157</span> <span>158</span> $("#pictie"<span>).hide(); </span><span>159</span> <span> } </span><span>160</span> });
大写锁定的思路是:判断输入的字符的最新一位是否是大写字母,如果是大写字母,则提示大写锁定键打开。这种方法并不十分准确,网上有一些插件能判断大写锁定,在这里只是简单地做了一下判断。
提示文字( Chrome下 )
①获得焦点、点击时
②输入时
失去焦点时与此效果相同
失去焦点时与此效果相同
失去焦点时与此效果相同
失去焦点时与此效果相同
③失去焦点为空时
④出现大写时
密码栏目至此结束。
重复密码:失去焦点时判断是否和密码一致
reister.html代码片段:
<span><</span><span>div </span><span>class</span><span>="ipt"</span><span>></span> <span><</span><span>input </span><span>type</span><span>="password"</span><span> name</span><span>="rupwd"</span><span> id</span><span>="rupwd"</span><span> value</span><span>=""</span><span> placeholder</span><span>="确认密码"</span> <span>/></span> <span><</span><span>span </span><span>id</span><span>="rupwdchk"</span><span>></</span><span>span</span><span>></span> <span></</span><span>div</span><span>></span>
register.js代码片段:
<span> 1</span> $("#rupwd").focus(<span>function</span><span>(){ </span><span> 2</span> <span> 3</span> <span>var</span> noticeMsg = '再次输入你设置的密码'<span>; </span><span> 4</span> notice($("#rupwdchk"<span>),noticeMsg); </span><span> 5</span> <span> }) </span><span> 6</span> .click(<span>function</span><span>(){ </span><span> 7</span> <span> 8</span> <span>var</span> noticeMsg = '再次输入你设置的密码'<span>; </span><span> 9</span> notice($("#rupwdchk"<span>),noticeMsg); </span><span>10</span> }).keydown(<span>function</span><span>(){ </span><span>11</span> <span>12</span> <span>//</span><span>把焦点移至邮箱栏目</span> <span>13</span> <span>if</span>(event.keyCode == 13<span>){ </span><span>14</span> <span>15</span> $("#yzm"<span>).focus(); </span><span>16</span> <span> } </span><span>17</span> <span> }) </span><span>18</span> .blur(<span>function</span><span>(){ </span><span>19</span> <span>20</span> <span>if</span>(<span>this</span>.value == $("#upwd").val() && <span>this</span>.value!=""<span>){ </span><span>21</span> <span>22</span> success($("#rupwdchk"),""<span>); </span><span>23</span> <span>24</span> rpwdval = <span>true</span><span>; </span><span>25</span> }<span>else</span> <span>if</span>(<span>this</span>.value == ""<span>){ </span><span>26</span> <span>27</span> $("#rupwdchk").html(""<span>); </span><span>28</span> }<span>else</span><span>{ </span><span>29</span> <span>30</span> <span>var</span> errorMsg = '两次输入的密码不一致'<span>; </span><span>31</span> error($("#rupwdchk"<span>),errorMsg); </span><span>32</span> <span> } </span><span>33</span> });
提示文字:
①获得焦点、点击时
②失去焦点时和密码不一致、一致时分别为
至此重复密码结束。
验证码:不区分大小写
验证码采用4位,可以包含的字符为数字1-9,字母a-f
点击验证码和刷新按钮都能刷新验证码
register.html验证码代码部分:
<span>1</span> <span><</span><span>div </span><span>class</span><span>="ipt iptend"</span><span>></span> <span>2</span> <span><</span><span>input </span><span>type</span><span>='text' </span><span>id</span><span>='yzm' </span><span>name</span><span>='yzm' </span><span>placeholder</span><span>="验证码"</span><span>></span> <span>3</span> <span><</span><span>img </span><span>id</span><span>='yzmpic' </span><span>src</span><span>='' </span><span>style</span><span>="cursor:pointer"</span><span>><span> <!-- 验证码图片 --></span></span> <span>4</span> <span><</span><span>a </span><span>style</span><span>="cursor:pointer"</span><span> id</span><span>='changea'</span><span>></span> <span>5</span> <span><</span><span>img </span><span>id</span><span>="refpic"</span><span> src</span><span>="images/ref.jpg"</span><span> alt</span><span>="验证码"</span><span>> <span><!-- 验证码刷新按钮图片 --></span></span> <span>6</span> <span></</span><span>a</span><span>></span> <span>7</span> <span><</span><span>span </span><span>id</span><span>='yzmchk'</span><span>></</span><span>span</span><span>></span> <span>8</span> <span><</span><span>input </span><span>type</span><span>='hidden' </span><span>id</span><span>='yzmHiddenNum' </span><span>name</span><span>='yzmHiddenNum' </span><span>value</span><span>=''</span><span>> <span><!-- 隐藏域,内容是验证码输出的数字,用户输入的字符与其进行对比 --></span></span> <span>9</span> <span></</span><span>div</span><span>></span>
register.js验证码部分:
<span> 1</span> <span>//</span><span>验证码按钮</span> <span> 2</span> $("#refpic").hover(<span>function</span><span>(){ </span><span> 3</span> <span> 4</span> $(<span>this</span>).attr("src","images/refhover.jpg"<span>); </span><span> 5</span> },<span>function</span><span>(){ </span><span> 6</span> <span> 7</span> $(<span>this</span>).attr("src","images/ref.jpg"<span>); </span><span> 8</span> }).mousedown(<span>function</span><span>(){ </span><span> 9</span> <span> 10</span> $(<span>this</span>).attr("src","images/refclick.jpg"<span>); </span><span> 11</span> }).mouseup(<span>function</span><span>(){ </span><span> 12</span> <span> 13</span> $(<span>this</span>).attr("src","images/ref.jpg"<span>); </span><span> 14</span> <span> }); </span><span> 15</span> <span> 16</span> <span>//</span><span>生成验证码函数</span> <span> 17</span> <span>function</span><span> showval() { </span><span> 18</span> <span> 19</span> num = ''<span>; </span><span> 20</span> <span>for</span> (i = 0; i < 4; i++<span>) { </span><span> 21</span> <span> 22</span> tmp = Math.ceil(Math.random() * 15);<span>//</span><span>Math.ceil上取整;Math.random取0-1之间的随机数</span> <span> 23</span> <span>if</span> (tmp > 9<span>) { </span><span> 24</span> <span>switch</span><span> (tmp) { </span><span> 25</span> <span>case</span>(10<span>): </span><span> 26</span> num += 'a'<span>; </span><span> 27</span> <span>break</span><span>; </span><span> 28</span> <span>case</span>(11<span>): </span><span> 29</span> num += 'b'<span>; </span><span> 30</span> <span>break</span><span>; </span><span> 31</span> <span>case</span>(12<span>): </span><span> 32</span> num += 'c'<span>; </span><span> 33</span> <span>break</span><span>; </span><span> 34</span> <span>case</span>(13<span>): </span><span> 35</span> num += 'd'<span>; </span><span> 36</span> <span>break</span><span>; </span><span> 37</span> <span>case</span>(14<span>): </span><span> 38</span> num += 'e'<span>; </span><span> 39</span> <span>break</span><span>; </span><span> 40</span> <span>case</span>(15<span>): </span><span> 41</span> num += 'f'<span>; </span><span> 42</span> <span>break</span><span>; </span><span> 43</span> <span> } </span><span> 44</span> } <span>else</span><span> { </span><span> 45</span> num +=<span> tmp; </span><span> 46</span> <span> } </span><span> 47</span> <span> 48</span> $('#yzmpic').attr("src","../valcode.php?num="+<span>num); </span><span> 49</span> <span> } </span><span> 50</span> $('#yzmHiddenNum'<span>).val(num); </span><span> 51</span> <span> } </span><span> 52</span> <span> 53</span> <span>//</span><span>生成验证码以及刷新验证码</span> <span> 54</span> <span> showval(); </span><span> 55</span> $('#yzmpic').click(<span>function</span><span>(){ </span><span> 56</span> <span> 57</span> <span> showval(); </span><span> 58</span> <span> }); </span><span> 59</span> $('#changea').click(<span>function</span><span>(){ </span><span> 60</span> <span> 61</span> <span> showval(); </span><span> 62</span> <span> }); </span><span> 63</span> <span> 64</span> <span>//</span><span>验证码检验</span> <span> 65</span> <span>function</span><span> yzmchk(){ </span><span> 66</span> <span> 67</span> <span>if</span>($("#yzm").val() == ""<span>){ </span><span> 68</span> <span> 69</span> <span>var</span> errorMsg = '验证码不能为空'<span>; </span><span> 70</span> error($("#yzmchk"<span>),errorMsg); </span><span> 71</span> }<span>else</span> <span>if</span>($("#yzm").val().toLowerCase()!=$("#yzmHiddenNum"<span>).val()){ </span><span> 72</span> <span> 73</span> <span>//</span><span>不区分大小写</span> <span> 74</span> <span>var</span> errorMsg = '请输入正确的验证码'<span>; </span><span> 75</span> error($("#yzmchk"<span>),errorMsg); </span><span> 76</span> }<span>else</span><span>{ </span><span> 77</span> <span> 78</span> success($("#yzmchk"),""<span>); </span><span> 79</span> <span> 80</span> yzmval = <span>true</span><span>; </span><span> 81</span> <span> } </span><span> 82</span> <span> } </span><span> 83</span> <span> 84</span> <span>//</span><span>验证码的blur事件</span> <span> 85</span> $("#yzm").focus(<span>function</span><span>(){ </span><span> 86</span> <span> 87</span> <span>var</span> noticeMsg = '不区分大小写'<span>; </span><span> 88</span> notice($("#yzmchk"<span>),noticeMsg); </span><span> 89</span> }).click(<span>function</span><span>(){ </span><span> 90</span> <span> 91</span> <span>var</span> noticeMsg = '不区分大小写'<span>; </span><span> 92</span> notice($("yzmdchk"<span>),noticeMsg); </span><span> 93</span> }).keydown(<span>function</span><span>(){ </span><span> 94</span> <span> 95</span> <span>//</span><span>提交</span> <span> 96</span> <span>if</span>(event.keyCode == 13<span>){ </span><span> 97</span> <span> 98</span> <span>//</span><span>先检验后提交</span> <span> 99</span> <span> yzmchk(); </span><span>100</span> <span> formsub(); </span><span>101</span> <span> } </span><span>102</span> }).blur(<span>function</span><span>(){ </span><span>103</span> <span>104</span> <span> yzmchk(); </span><span>105</span> });
valcode.php验证码生成php代码:
<span> 1</span> <span>php </span><span> 2</span> <span> 3</span> <span>header</span>("content-type:image/png"<span>); </span><span> 4</span> <span>$num</span> = <span>$_GET</span>['num'<span>]; </span><span> 5</span> <span>$imagewidth</span> = 150<span>; </span><span> 6</span> <span>$imageheight</span> = 54<span>; </span><span> 7</span> <span> 8</span> <span>//</span><span>创建图像</span> <span> 9</span> <span>$numimage</span> = imagecreate(<span>$imagewidth</span>, <span>$imageheight</span><span>); </span><span>10</span> <span>11</span> <span>//</span><span>为图像分配颜色</span> <span>12</span> imagecolorallocate(<span>$num</span>