Home  >  Article  >  php教程  >  Detailed explanation of form validation based on PHP+Ajax

Detailed explanation of form validation based on PHP+Ajax

高洛峰
高洛峰Original
2016-12-23 17:42:041746browse

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(){
 $(&#39;regname&#39;).onkeydown = function (){
  name = $(&#39;regname&#39;).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".

$(&#39;regname&#39;).focus();
var cname1,cname2,cpwd1,cpwd2;  //声明了5个变量,表示要检测的5项数据chkreg()函数是每一次触发键盘事件后都要调用的,该函数判断5个变量的值,只有当所有变量都为"yes"时,注册按钮才会被激活。 
function chkreg(){
 if((cname1 == &#39;yes&#39;) && (cname2 == &#39;yes&#39;) && (cpwd1 == &#39;yes&#39;) && (cpwd2 == &#39;yes&#39;)){
  $(&#39;regbtn&#39;).disabled = false;
 }else{
  $(&#39;regbtn&#39;).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.

$(&#39;regname&#39;).onkeyup = function (){
 name = $(&#39;regname&#39;).value;  //获取注册名称
 cname2 = &#39;&#39;;
 if(name.match(/^[a-zA-Z_]*/) == &#39;&#39;){
  $(&#39;namediv&#39;).innerHTML = &#39;<font color=red>必须以字母或下划线开头</font>&#39;;
  cname1 = &#39;&#39;;
 }else if(name.length <= 3){
  $(&#39;namediv&#39;).innerHTML = &#39;<font color=red>注册名称必须大于3位</font>&#39;;
  cname1 = &#39;&#39;;
 }else{
  $(&#39;namediv&#39;).innerHTML = &#39;<font color=green>注册名称符合标准</font>&#39;;
  cname1 = &#39;yes&#39;;
 }
 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.

$(&#39;regname&#39;).onblur = function(){
 name = $(&#39;regname&#39;).value;  //获取注册名称
 if(cname1 == &#39;yes&#39;){ //当用户名称的格式输入合格后才进行这一步
  xmlhttp.open(&#39;get&#39;,&#39;chkname.php?name=&#39;+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 == &#39;1&#39;){  //chkname.php页面查找数据库,数据库没有该用户返回1
      $(&#39;namediv&#39;).innerHTML="<font color=green>恭喜您,该用户名可以使用!</font>";
      cname2 = &#39;yes&#39;;
     }else if(msg == &#39;2&#39;){ //数据库存在该用户返回0
      $(&#39;namediv&#39;).innerHTML="<font color=red>用户名被占用!</font>";
      cname2 = &#39;&#39;;
     }else{
      $(&#39;namediv&#39;).innerHTML="<font color=red>"+msg+"</font>";
      cname2 = &#39;&#39;;
     }
    }
   }
  }
  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.

$(&#39;regpwd1&#39;).onkeyup = function(){
 pwd = $(&#39;regpwd1&#39;).value;
 pwd2 = $(&#39;regpwd2&#39;).value;
 if(pwd.length < 6){
  $(&#39;pwddiv1&#39;).innerHTML = &#39;<font color=red>密码长度最少需要6位</font>&#39;;
  cpwd1 = &#39;&#39;;
 }else if(pwd.length >= 6 && pwd.length < 12){
  $(&#39;pwddiv1&#39;).innerHTML = &#39;<font color=green>密码符合要求。密码强度:弱</font>&#39;;
  cpwd1 = &#39;yes&#39;;
 }else if((pwd.match(/^[0-9]*$/)!=null) || (pwd.match(/^[a-zA-Z]*$/) != null )){
  $(&#39;pwddiv1&#39;).innerHTML = &#39;<font color=green>密码符合要求。密码强度:中</font>&#39;;
  cpwd1 = &#39;yes&#39;;
 }else{
  $(&#39;pwddiv1&#39;).innerHTML = &#39;<font color=green>密码符合要求。密码强度:高</font>&#39;;
  cpwd1 = &#39;yes&#39;;
 }
 if(pwd2 != &#39;&#39; && pwd != pwd2){
  $(&#39;pwddiv2&#39;).innerHTML = &#39;<font color=red>两次密码不一致!</font>&#39;;
  cpwd2 = &#39;&#39;;
 }else if(pwd2 != &#39;&#39; && pwd == pwd2){
  $(&#39;pwddiv2&#39;).innerHTML = &#39;<font color=green>密码输入正确</font>&#39;;
  cpwd2 = &#39;yes&#39;;
 }
 chkreg();
}

The second password judgment is relatively simple, just judge whether the second password input is equal to the first input.

$(&#39;regpwd2&#39;).onkeyup = function(){
 pwd1 = $(&#39;regpwd1&#39;).value;
 pwd2 = $(&#39;regpwd2&#39;).value;
 if(pwd1 != pwd2){
  $(&#39;pwddiv2&#39;).innerHTML = &#39;<font color=red>两次密码不一致!</font>&#39;;
  cpwd2 = &#39;&#39;;
 }else{
  $(&#39;pwddiv2&#39;).innerHTML = &#39;<font color=green>密码输入正确</font>&#39;;
  cpwd2 = &#39;yes&#39;;
 }
 chkreg();
}

The above information must be filled in. If the user wants to fill in more detailed information, he can click the "Details Button"

$(&#39;morebtn&#39;).onclick = function(){
 if($(&#39;morediv&#39;).style.display == &#39;&#39;){
  $(&#39;morediv&#39;).style.display = &#39;none&#39;;
 }else{
  $(&#39;morediv&#39;).style.display = &#39;&#39;;
 }
}

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

$(&#39;email&#39;).onkeyup = function(){
 emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
 $(&#39;email&#39;).value.match(emailreg);
 if($(&#39;email&#39;).value.match(emailreg) == null){
  $(&#39;emaildiv&#39;).innerHTML = &#39;<font color=red>错误的email格式</font>&#39;;
  cemail = &#39;&#39;;
 }else{
  $(&#39;emaildiv&#39;).innerHTML = &#39;<font color=green>输入正确</font>&#39;;
  cemail = &#39;yes&#39;;

 }
 chkreg();

Three, detect the user name (chkname.php)

<?php
session_start();
include_once "conn/conn.php";
$reback = &#39;0&#39;;
$sql = "select * from tb_member where name=&#39;".$_GET[&#39;name&#39;]."&#39;";
$num = $conne->getRowsNum($sql);
if($num == 1){
 $reback = &#39;2&#39;;
}else if($num == 0){
 $reback = &#39;1&#39;;
}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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn