First, use keyboard response to verify whether the form input is legal without refreshing this page
Users trigger response events through onkeydown and onkeyup events. The usage method is similar to the onclick event. onkeydown means that it is triggered when a key on the keyboard is pressed, and onkeyup is just the opposite, triggering when a key on the keyboard is pressed and then lifted.
Two commonly used calling methods:
(1) Add the event to the page element. When the user completes inputting the information and clicks any key, the onkeydown event is triggered and the refer() function is called.
This method is the simplest and most direct, with the following format:
<script type="text/javascript"> ... function refer(){ ... } </script> <input type="text" onkeydown="refer()"/>
(2) Loading through window.onload, when the page is loaded, the event is loaded. When the user enters information, this event will be triggered every time a letter is entered. In the function called by the event, the user's input information is judged.
window.onload = function(){ $('regname').onkeydown = function (){ name = $('regname').value; } }
Using the onkeydown event can also control specific keys, including the 148abe006775c52e28d07bbe171fd752 key (event.keyCode==13), the space bar (event.keyCode==32), ca92b3c4521180d6a70d03d3dfb34279 key,
Second, registration information verification
General function, returns the triggered id element object
function $(id){ return document.getElementById(id); } window.onload事件,表示当前窗口被载入时触发。function(){...}表示当前页面被载入时所要进行的操作。 window.onload = function(){ ... }
function() function analysis;
First, focus on the user name text box to facilitate user operations. Next, 5 variables are declared, which represent the results of the 5 data to be detected. When the detection data is qualified, set the variable value to "yes".
$('regname').focus(); var cname1,cname2,cpwd1,cpwd2; //声明了5个变量,表示要检测的5项数据chkreg()函数是每一次触发键盘事件后都要调用的,该函数判断5个变量的值,只有当所有变量都为"yes"时,注册按钮才会被激活。 function chkreg(){ if((cname1 == 'yes') && (cname2 == 'yes') && (cpwd1 == 'yes') && (cpwd2 == 'yes')){ $('regbtn').disabled = false; }else{ $('regbtn').disabled = true; } }
Next, verify the user name. When the user enters the registration name, the function will make a regular judgment on each input of the user and set it according to the result. Different cname1 values.
$('regname').onkeyup = function (){ name = $('regname').value; //获取注册名称 cname2 = ''; if(name.match(/^[a-zA-Z_]*/) == ''){ $('namediv').innerHTML = '<font color=red>必须以字母或下划线开头</font>'; cname1 = ''; }else if(name.length <= 3){ $('namediv').innerHTML = '<font color=red>注册名称必须大于3位</font>'; cname1 = ''; }else{ $('namediv').innerHTML = '<font color=green>注册名称符合标准</font>'; cname1 = 'yes'; } chkreg(); //调用chkreg()函数,判断5个变量是否正确 }
When the username text box loses focus, that is, when the user completes input and moves to other elements on the page, it will detect whether the username is repeated. The user name judgment uses Ajax technology to call chkname.php (the user name verification code of this page will be posted later) and the judgment result is displayed in the div tag based on the return value of chkname.php.
$('regname').onblur = function(){ name = $('regname').value; //获取注册名称 if(cname1 == 'yes'){ //当用户名称的格式输入合格后才进行这一步 xmlhttp.open('get','chkname.php?name='+name,true); //open()创建XMLHttpRequest初始化连接,Ajax创建新的请求 xmlhttp.onreadystatechange = function(){ //当指定XMLHttpRequest为异步传输时(false),发生任何状态的变化,该对象都会调用onreadystatechange所指定的函数 if(xmlhttp.readyState == 4){ //XMLHttpRequest处理状态,4表示处理完毕 if(xmlhttp.status == 200){ //服务器响应的HTTP代码,200表示正常 var msg = xmlhttp.responseText; //获取响应页的内容 if(msg == '1'){ //chkname.php页面查找数据库,数据库没有该用户返回1 $('namediv').innerHTML="<font color=green>恭喜您,该用户名可以使用!</font>"; cname2 = 'yes'; }else if(msg == '2'){ //数据库存在该用户返回0 $('namediv').innerHTML="<font color=red>用户名被占用!</font>"; cname2 = ''; }else{ $('namediv').innerHTML="<font color=red>"+msg+"</font>"; cname2 = ''; } } } } xmlhttp.send(null); chkreg(); //检测是否激活注册按钮 } }
Verify the password. When verifying the password, in addition to limiting the length of the password, you can also judge the strength of the password.
$('regpwd1').onkeyup = function(){ pwd = $('regpwd1').value; pwd2 = $('regpwd2').value; if(pwd.length < 6){ $('pwddiv1').innerHTML = '<font color=red>密码长度最少需要6位</font>'; cpwd1 = ''; }else if(pwd.length >= 6 && pwd.length < 12){ $('pwddiv1').innerHTML = '<font color=green>密码符合要求。密码强度:弱</font>'; cpwd1 = 'yes'; }else if((pwd.match(/^[0-9]*$/)!=null) || (pwd.match(/^[a-zA-Z]*$/) != null )){ $('pwddiv1').innerHTML = '<font color=green>密码符合要求。密码强度:中</font>'; cpwd1 = 'yes'; }else{ $('pwddiv1').innerHTML = '<font color=green>密码符合要求。密码强度:高</font>'; cpwd1 = 'yes'; } if(pwd2 != '' && pwd != pwd2){ $('pwddiv2').innerHTML = '<font color=red>两次密码不一致!</font>'; cpwd2 = ''; }else if(pwd2 != '' && pwd == pwd2){ $('pwddiv2').innerHTML = '<font color=green>密码输入正确</font>'; cpwd2 = 'yes'; } chkreg(); }
The second password judgment is relatively simple, just judge whether the second password input is equal to the first input.
$('regpwd2').onkeyup = function(){ pwd1 = $('regpwd1').value; pwd2 = $('regpwd2').value; if(pwd1 != pwd2){ $('pwddiv2').innerHTML = '<font color=red>两次密码不一致!</font>'; cpwd2 = ''; }else{ $('pwddiv2').innerHTML = '<font color=green>密码输入正确</font>'; cpwd2 = 'yes'; } chkreg(); }
The above information must be filled in. If the user wants to fill in more detailed information, he can click the "Details Button"
$('morebtn').onclick = function(){ if($('morediv').style.display == ''){ $('morediv').style.display = 'none'; }else{ $('morediv').style.display = ''; } }
E-mail format verification, the input string must contain @ and., and these two characters at the same time The position of the string cannot be at the beginning or end or connected together
$('email').onkeyup = function(){ emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; $('email').value.match(emailreg); if($('email').value.match(emailreg) == null){ $('emaildiv').innerHTML = '<font color=red>错误的email格式</font>'; cemail = ''; }else{ $('emaildiv').innerHTML = '<font color=green>输入正确</font>'; cemail = 'yes'; } chkreg();
Three, detect the user name (chkname.php)
<?php session_start(); include_once "conn/conn.php"; $reback = '0'; $sql = "select * from tb_member where name='".$_GET['name']."'"; $num = $conne->getRowsNum($sql); if($num == 1){ $reback = '2'; }else if($num == 0){ $reback = '1'; }else{ $reback = $conne->msg_error(); } echo $reback; ?>
Four, initialize the XMLHttpRequest function
// JavaScript Document var xmlhttp = false; if (window.XMLHttpRequest) { //Mozilla、Safari等浏览器 xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { //IE浏览器 try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } }
More details on implementing form validation based on PHP+Ajax For related articles, please pay attention to the PHP Chinese website!