注冊js驗證
A. JavaScript注冊表單的驗證
function isCharsNotInBag (s, bag)
{
var i,c;
for (i = 0; i < s.length; i++)
{
c = s.charAt(i);
if (bag.indexOf(c) < 0)
return true;
}
return false;
} function InValidChar(s)
{
var haserrorChar;
var CorrectChar = ""
haserrorChar = isCharsNotInBag(s, CorrectChar);
return haserrorChar;
}
function orderuser(strs)
{
if (InValidChar(strs))
{
document.getElementById("uname").innerHTML ="用戶名只能使用英文加數字組合";
document.getElementById("txtName").focus();
return;
}
if(strs.length<5)
{
document.getElementById("uname").innerHTML ="用戶名不能低於5位";
document.getElementById("txtName").focus();
return;
}
var xmlHttp = new XMLHttpRequest();
var url="reg_ajax.aspx";
url= addparam(url,"users",strs);
url= addparam(url,"types","select");
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById("uname").innerHTML = xmlHttp.responseText;
if(xmlHttp.responseText=="該用戶名可以使用")
{
document.getElementById("txtPwd").focus();
document.getElementById("txtName").readOnly=true;
}
else
{
document.getElementById("txtName").focus();
}
}
}
}
function checkpwd()
{
var pwd = document.getElementById("txtPwd").value;
if (pwd.length<6)
{
document.getElementById("pwd").innerHTML="密碼不能低於6位";
document.getElementById("txtPwd").focus();
return;
}
else
{
if (document.getElementById("txtPwd").value==document.getElementById("txtPwd2").value)
{
document.getElementById("pwd").innerHTML="兩次密碼相同";
document.getElementById("txtPwd").readOnly=true;
document.getElementById("txtPwd2").readOnly=true;
document.getElementById("Button1").disabled = "";
}
else
{
document.getElementById("pwd").innerHTML="兩次密碼不同";
document.getElementById("txtPwd").focus();
}
}
}
function checkform()
{
if (document.getElementById("yanzhengma").value.length<4)
{
alert("請輸入驗證碼");
return;
}
else
{
var urlstr="regditaccept.aspx?uname=" + document.getElementById("txtName").value + "&upwd=" + document.getElementById("txtPwd").value + "&yzm=" + document.getElementById("yanzhengma").value +"&email="+ document.getElementById("txtMailbox").value ;
window.location.href=urlstr;
}
} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml" >
<head>
<title>無標題頁</title>
<script type="text/javascript" src="../js/public_ajax.js"></script>
<script type="text/javascript" src="../js/reg_other.js">
</script>
</head>
<body onload="javascript:document.getElementById('txtName').focus();">
<form>
<table bgcolor="#cccccc" border="0" cellpadding="2" cellspacing="1" style="width: 477px;
height: 156px">
<tr>
<td align="center" bgcolor="#f7f7f7" width="80">
<span style="font-size: 10pt">用戶名稱:</span></td>
<td align="left" bgcolor="#f7f7f7" width="427">
<p>
<input id="txtName" name="txtName" onblur="orderuser(this.value)"
style="width: 185px" type="text" />
<span style="font-size: 10pt;color: #990000"><span class="hong1">*</span></span><span
id="uname" style="font-size: 10pt; color: #cc0000"></span></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#ffffff">
<span style="font-size: 10pt">您的密碼:</span></td>
<td align="left" bgcolor="#ffffff">
<input id="txtPwd" maxlength="20" name="txtPwd" style="width: 185px"
type="password" />
<span class="hong1"><span style="font-size: 10pt; color: #990000">*</span> <span
id="rfv_Pwd" style="display: none; color: red"></span> </span>
</td>
</tr>
<tr>
<td align="center" bgcolor="#f7f7f7">
<span style="font-size: 10pt">確認密碼:</span></td>
<td align="left" bgcolor="#f7f7f7">
<input id="txtPwd2" maxlength="20" name="txtPwd2" onblur="checkpwd()" style="width: 185px"
type="password" />
<span class="hong1"><span style="font-size: 10pt; color: #990000">* </span><span
id="pwd" style="font-size: 10pt; color: #cc0000"></span></span></td>
</tr>
<tr>
<td align="center" bgcolor="#f7f7f7">
<span style="font-size: 10pt">電子郵箱:</span></td>
<td align="left" bgcolor="#f7f7f7">
<input id="txtMailbox" class="text1" name="txtMailbox" style="width: 185px"
type="text" /></td>
</tr>
<tr>
<td align="center" bgcolor="#ffffff" style="height: 28px">
<span style="font-size: 10pt">驗證碼:</span></td>
<td align="left" bgcolor="#ffffff" style="height: 28px">
<input id="yanzhengma" type="text" width="110px" style="width: 97px" /><iframe frameborder="0"
scrolling="no" src="yanzhengma.aspx" style="width: 68px;
height: 22px"></iframe>
<span style="font-size: 10pt; color: #990000">(點擊圖片刷新驗證碼)</span></td>
</tr>
<tr>
<td align="center" bgcolor="#f7f7f7" colspan="2" nowrap="nowrap">
<input id="Button1" onclick="checkform()" disabled="disabled" style="border-right: #000000 1px solid; border-top: #000000 1px solid;
border-left: #000000 1px solid; width: 64px; border-bottom: #000000 1px solid;
height: 26px; background-color: #ff9900" type="button" value="注冊" />
<input id="Button2" style="border-right: #000000 1px solid; border-top: #000000 1px solid;
border-left: #000000 1px solid; width: 64px; border-bottom: #000000 1px solid;
height: 26px; background-color: #ff9900" type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
B. 求份完整的JS注冊表單驗證
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
form.input,div{margin:0; padding:0;}
body{font-size:12px; margin:0; padding:0;}
#login{width:1151; height:624px;}
.parent{width:100%; height:27px; line-height:27px;}
.parent label{display:inline-block;width:333px; height:27px;line-height:27px; text-align:right;}
.bor{padding-left:333px; color:#999;}
.three{width:308px; height:25px; border:1px solid #999;}
#email{width:196px; height:25px; border:1px solid #999;}
.wrong{background:url(images/wrong.png) no-repeat; padding-left:25px; color:#F00; display:inline-block; height:25px; line-height:25px;}
.right{background:url(images/right.png) no-repeat; padding-left:25px; color:#00F; display:inline-block; height:25px;line-height:25px;}
</style>
<title>注冊界面</title>
</head>
<body>
<div id="login">
<form action="form" method="get" name="login" style="margin-top:24px">
<div class="parent"><label>郵箱地址:</label><input type="text" id="email" /> @
<select style="width:86px; height:20px;">
<option value="163.com">163.com</option>
<option value="126.com">126.com</option>
<option value="yeah.net">yeah.net</option>
</select>
</div>
<div class="bor">6-18個字元,區分大小寫</div>
<div class="parent"><label>密碼:</label><input type="password" id="pwd" class="three" /></div>
<div class="bor">6-16個字元,區分大小寫</div>
<div class="parent"><label>確認密碼:</label><input type="password" id="dpwd" class="three" /></div>
<div class="bor">請再次輸入密碼</div>
<div class="parent"><label>手機號碼:</label><input type="text" id="phoneNum" class="three" /></div>
<div class="bor">密碼遺忘或被盜時,可通過手機簡訊取回密碼</div>
<div class="parent"><label>驗證碼:</label><input type="text"/></div>
<input type="button" id="dad" value="注冊" style="position:relative;left:333px;" />
</form>
</div>
<script type="text/javascript">
for(i=0;i<4;i++){
document.getElementsByTagName("input")[i].onfocus=function(){
}
document.getElementsByTagName("input")[i].onblur=function(){
var spanLength=this.parentNode.getElementsByTagName("span").length;
if(spanLength>0){
this.parentNode.removeChild(this.nextSibling)
}
var id=this.getAttribute("id");
if(id=="email"){
emailReg=/^\w{6,18}$/
if(!emailReg.test(this.value)){
span=document.createElement("span")
spanTxt=document.createTextNode("您輸入的格式有錯誤")
span.className="wrong"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}else{
span=document.createElement("span")
spanTxt=document.createTextNode("恭喜您輸入正確")
span.className="right"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}
}
if(id=="pwd"){
pwdReg=/^\w{6,16}$/
if(!pwdReg.test(this.value)){
span=document.createElement("span")
spanTxt=document.createTextNode("您輸入的格式有錯誤")
span.className="wrong"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}else{
span=document.createElement("span")
spanTxt=document.createTextNode("恭喜您輸入正確")
span.className="right"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}
}
if(id=="dpwd"){
if(this.value!=document.getElementById("pwd").value||this.length<=0){
span=document.createElement("span")
spanTxt=document.createTextNode("您輸入的格式有錯誤")
span.className="wrong"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}else{
span=document.createElement("span")
spanTxt=document.createTextNode("恭喜您輸入正確")
span.className="right"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}
}
if(id=="phoneNum"){
phoneReg=/^1[3|4|5|8]\d{9}$/
if(!phoneReg.test(this.value)){
span=document.createElement("span")
spanTxt=document.createTextNode("您輸入的格式有錯誤")
span.className="wrong"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}else{
span=document.createElement("span")
spanTxt=document.createTextNode("恭喜您輸入正確")
span.className="right"
span.appendChild(spanTxt)
this.parentNode.appendChild(span)
}
}
}
document.login.getElementsByTagName("input")[i].onkeyup=function(){
this.blur();
this.focus();
}
}
document.getElementById("dad").onmousedown=function(){//onmousedown按下滑鼠鍵
for(i=0;i<4;i++){
document.login.getElementsByTagName("input")[i].focus();//對所有的文本框進行驗證
document.login.getElementsByTagName("input")[i].blur();//取消聚集
}
}
document.getElementById("dad").onmouseup=function(){//onmouseup彈起滑鼠鍵
var errorNum=0;//
for(i=0;i<document.login.getElementsByTagName("span").length;i++){
if(document.login.getElementsByTagName("span")[i].className=="wrong"){
errorNum++;//加一次
}
}
if(errorNum==0){
document.login.submit()
}else{
alert("填寫錯誤,請重新輸入正確的內容。")
}
}
</script>
</body>
</html>
自己寫著玩的
最好自己再修改下,看不懂我就沒辦法了。
C. js注冊驗證用戶名首字母英文 登錄密碼和確認密碼相同 email格式正確 *號項必填
我只給你提示,具體要麼查資料,要麼去網路搜了,然後多看看(希版望你好好學習,權只能說這個很簡單很簡單):
1、3的問題可以使用正則表達式完成(正則不會的,網路基本能得到答案)
2、這個我不知道你的作業會不會涉及後台,涉及後台的話使用AJAX非同步+資料庫查詢+結果響應。不涉及後台,直接在js函數里定義變數,然後比較輸入的內容是否相同就OK了
4、這個也是個內容的判斷,你要是會了2,4這個也就沒問題。
整個題目就是個怎麼獲取元素及內容,對比判斷內容的信息,input標簽里可以使用onchange作為觸發方法事件,具體邏輯提示什麼的就寫在觸發的函數里,這樣就可以產生信息不對的提示了!
注意(純手打,請不要追問具體代碼,這個真的很簡單,好好學學努力點,不會查查資料多看看對你有好處)
D. 請問下,在用html編寫一個用戶注冊界面時,怎麼加入js驗證。
在Form中加一個onsubmit事件再在事件中寫驗證代碼
E. js實現登錄,注冊,驗證怎麼寫
js實現登錄,注冊,驗證
不是簡單的幾句代碼就可以實現的
它要結合後面,且一般還要搭建資料庫環境
當然,用現成的框架可能更簡單些(但你要搭建框架環境,並會使用它)
這個網上有很多現成的參考的,你可以自己查下
F. js如何實現注冊時的智能判斷
輸入框後面加個 <span id="aaa"></span>
在你的判斷函數中 加上如下代碼
如果回成功答 document.getElementById("aaa").innerHTML="成功";
反之 document.getElementById("aaa").innerHTML="失敗";
G. JS表單注冊驗證
把以下代碼加到來body里試源一試 本應寫成function 就是做個示意給你看看原理
昵稱:
<inputtype="text"onclick="this.style.border='1px#cd1b1bsolid';document.getElementById('a').innerHTML='';"onblur="this.style.border='1px#000000solid';if(this.value==''){document.getElementById('a').innerHTML='昵稱不能為空!';}"/><spanid="a"></span><br/><br/>
密碼:
<inputtype="text"onclick="this.style.border='1px#cd1b1bsolid';document.getElementById('b').innerHTML='';"onblur="this.style.border='1px#000000solid';if(this.value==''){document.getElementById('b').innerHTML='密碼不能為空!';}"/><spanid="b"></span>
H. 注冊時 驗證用的js代碼
下面的代碼測試通過,不過你說的密碼不能重復是什麼意思?
<script language="javaScript">
function checkReg(){
if (document.regForm.username.value == ""){
("提示:\n\n必須輸入用戶名!");
document.regForm.username.focus();
return false;
}
if (document.regForm.password.value == document.regForm.username.value){
alert ("提示:\n\n密碼不能與用戶名相同!");
document.regForm.password.focus();
return false;
}else if (document.regForm.password.value.length < 6){
alert ("提示:\n\n密碼至少6位數!");
document.regForm.password.focus();
return false;
}else if (!isNaN(document.regForm.password.value)){
alert ("提示:\n\n密碼不能全是數字!");
document.regForm.password.focus();
return false;
}
if(!(document.regForm.tel.value.match(/^(\d{3,4}\-)?\d{7,8}$/))){
alert ("提示:\n\n固定電話格式為:XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX!");
document.regForm.tel.focus();
return false;
}
if(!(document.regForm.mobile.value.match(/^(\d{3})(\-)?(\d{8})$/))){
alert ("提示:\n\n11位手機號碼格式為:XXXXXXXXXXX,XXX-XXXXXXXX!");
document.regForm.mobile.focus();
return false;
}
if(!(document.regForm.email.value.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/))){
alert ("提示:\n\nEmail地址錯誤!");
document.regForm.email.focus();
return false;
}
return true;
}
</script>
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center">
<form method="post" action="reg.asp" name="regForm" onSubmit="return checkReg()">
<tr>
<td width="120" align="right">用戶名:</td>
<td width="280"><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td align="right">密碼:</td>
<td><input type="text" name="password" size="30"></td>
</tr>
<tr>
<td align="right">電話:</td>
<td><input type="text" name="tel" size="30"></td>
</tr>
<tr>
<td align="right">手機:</td>
<td><input type="text" name="mobile" size="30"></td>
</tr>
<tr>
<td align="right">郵箱: </td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" name="ok" value="注 冊">
<input type="reset" name="reset" value="重 填">
</td>
</tr>
</form>
</table>
I. 用javascript做注冊驗證
給你寫個簡單的,一個文本框,一個密碼框,驗證兩個都不能為空的。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script>
function checkForm()
{
var f = document.forms[0];
if(f.username.value=='')
{
document.getElementById("userNameInfo").style.display="block";
return false;
}
if(f.password.value=='')
{
document.getElementById("passwordInfo").style.display="block";
return false;
}
return true;
}
</script>
</head>
<body>
<form action="#" method="post" onsubmit="return checkForm()">
用戶名:<input type="text" name="username" /><div id="userNameInfo" style="display:none;"><font color="red">用戶名不能為空</font></div><br/>
密碼:<input type="password" name="password" /><div id="passwordInfo" style="display:none;"><font color="red">密碼不能為空</font></div><br/>
<input type="submit" value="登錄" />
</form>
</body>
</html>
J. js注冊表單驗證代碼
||JS函數:
functioncheck(){
varusername=document.getElementById("username").value;
varpass1=document.getElementById("password").value;
varpass2=document.getElementById("confirm_password").value;
if(username==""||username==NULL||pass1==""||pass1==NULL||pass2==NULL||pass2==""){
alert("用戶名或密碼不得為空!");
returnfalse;
}
}
表單提交版的時候驗證:權
<inputname="reg"type="submit"value="提交"onclick="returncheck()"/>
自己補全。