批改狀態(tài):未批改
老師批語:
html代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP表單驗證</title> <style> form{ width: 500px; height: 600px; background-color: lightsalmon; margin: auto; } table{ margin: auto; } table tr td{ font-weight: bold; color: #222222; padding: 10px; } button{ width: 100px; height: 30px; margin-left: 100px; background-color: #cccccc; font-weight: bolder; } button:hover{ cursor: pointer; background-color: sandybrown; border: 1px solid red; border-radius: 20%; } </style> </head> <body> <form> <table> <caption><h2>用戶注冊</h2></caption> <tr> <td> <label for="email">郵箱:</label> </td> <td> <input type="text" name="email" id="email"> </td> </tr> <tr> <td><label for="password1">密碼:</label></td> <td><input type="password" name="password1" id="password1"></td> </tr> <tr> <td><label for="password2">確認(rèn):</label></td> <td><input type="password" name="password2" id="password2"></td> </tr> <tr> <td><label for="secret">性別:</label></td> <td> <input type="radio" name="sex" value="man">男 <input type="radio" name="sex" value="woman">女 <input type="radio" name="sex" value="secret" id="secret" checked>保密 </td> </tr> <tr> <td><label>級別:</label></td> <td> <select name="level"> <option value="0" selected>菜鳥程序猿</option> <option value="1">程序猿</option> <option value="2">攻城獅</option> </select> </td> </tr> <tr> <td><label>語言:</label></td> <td> <input type="checkbox" name="language" value="php" checked>php <input type="checkbox" name="language" value="php">java <input type="checkbox" name="language" value="php">android <input type="checkbox" name="language" value="php">ios </td> </tr> <tr> <td><label for="introduction">簡介:</label></td> <td> <textarea name="introduction" id="introduction" cols="30" rows="10"></textarea> </td> </tr> <tr> <td colspan="2"> <button>提交</button> </td> </tr> </table> </form> </body> <script src="../jquery/jquery-3.2.1.min.js"></script> <script> $('#email').blur(function () { // $.post(url,data,success) $.post( 'form.php?checked=email', 'email='+$('#email').val(), function (data) { //郵箱驗證 switch (data.status){ case 0: $('td').find('span').remove() $('#email').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 1: $('td').find('span').remove() $('#email').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 2: $('td').find('span').remove() $('#email').after('<span>').next().text(data.msg).css('color','green').prev() break } },'json') }) //密碼驗證 $('#password1').blur(function () { //檢查郵箱輸入 if ($('#email').val().length == 0) { return false; } $.post( 'form.php?checked=password1', 'password1='+$('#password1').val(), function (data) { if(data.status ==0){ $('#password1').after('<span>').next().text(data.msg).css('color','red').prev().focus() } },'json') }) //確認(rèn)密碼驗證 $('#password2').blur(function () { //檢查郵箱輸入 if ($('#email').val().length == 0) { return false; } if ($('#password1').val().length == 0) { return false; } $.post( 'form.php?checked=password2', { password1:$('#password1').val(), password2:$('#password2').val() }, function (data) { switch (data.status){ case 0: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 1: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 2: $('td').find('span').remove() $('#password2').after('<span>').next().text(data.msg).css('color','green') break } },'json') }) //簡介 $('#introduction').blur(function () { if($('#email').val().length ==0 || $('#password1').val().length ==0 ||$('#password1').val()!=$('#password2').val()){ return false } $.post( 'form.php?checked=introduction', { "introduction":$('#introduction').val() }, function (data) { switch(data.status) { case 0: $('td').find('span').remove() $('#introduction').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 1: $('td').find('span').remove() $('#introduction').after('<span>').next().text(data.msg).css('color','red').prev().focus() break case 2: $('td').find('span').remove() $('#introduction').after('<span>').next().text(data.msg).css('color','green') break } },'json') }) </script> </html>
點擊 "運行實例" 按鈕查看在線實例
php代碼:
<?php switch ($_GET['checked']){ //驗證郵箱 case 'email': $email = $_POST['email']; if(empty($email)){ exit(json_encode(['status'=>0,'msg'=>'郵箱不能為空'])); }else if(in_array($email,['admin@php.com','php@php.cn'])){ exit(json_encode(['status'=>1,'msg'=>'郵箱已經(jīng)被使用'])); }else { exit(json_encode(['status'=>2,'msg'=>'郵箱可用'])); } break; //驗證密碼 case 'password1': $password1 = $_POST['password1']; if(empty($password1)){ exit(json_encode(['status'=>0,'msg'=>'密碼不為空'])); } break; //驗證確認(rèn)密碼 case 'password2': $password1=$_POST['password1']; $password2=$_POST['password2']; if(empty($password2)){ exit(json_encode(['status'=>0,'msg'=>'確認(rèn)密碼不能為空'])); }else if($password1 !=$password2){ exit(json_encode(['status'=>1,'msg'=>'兩次密碼不一致'])); }else{ exit(json_encode(['status'=>2,'msg'=>'驗證通過'])); } break; //簡介 case 'introduction': $introduction=$_POST['introduction']; if(empty($introduction)){ exit(json_encode(['status'=>0,'msg'=>'簡介不能為空'])); }else if(strlen($introduction)<20){ exit(json_encode(['status'=>1,'msg'=>'簡介字?jǐn)?shù)不能小于20個字'])); }else{ exit(json_encode(['status'=>2,'msg'=>'簡介說明已完成'])); } }
點擊 "運行實例" 按鈕查看在線實例
運行效果圖:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號