js注冊表單驗證
⑴ 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>
⑵ 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()"/>
自己補全。
⑶ 求份完整的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>
自己寫著玩的
最好自己再修改下,看不懂我就沒辦法了。
⑷ 我用JS做了一個彈出注冊的表單,這種表單要如何驗證其中的用戶名和密碼,謝謝!
你要驗證用戶名和密碼的輸入格式么?
可以用正則匹配來判斷
⑸ JavaScript HTML表單問題 在用js寫用戶端的驗證 注冊的表單 要兩次密碼相同 已經
您好,這樣的:
表單文件formtest.asp
<%
Randomize'初始代隨機數種子
num1=rnd() '產生隨機數num1
num1=int(26*num1)+65'修改num1的范圍以使其是A-Z范圍的Ascii碼,以防表單名出錯
session("antry")="test"&chr(num1)'產生隨機字元串
%>
<form name="test" action="testact.asp" method="post">
你的名字:<input type='text' name='' size=30> '注意本行中使用了隨機表單項名
<input type='submit' value='提交'>
</form>
表單處理程序testact.asp
<%
teststr=request.form(session("antry"))
if teststr="" then
response.write "沒有填寫姓名或重復提交"
'由於用戶沒有填寫名字,或表單被重復提交(標志為session("antry")為空)引起
else
response.write teststr
session("antry")=""'提交成功,清空session("antry"),以防重復提交!!
end if
%>
在這里,你只需隨機化一個必填項目的表單項名即可,不必隨機化所有的表單項目。
⑹ JS實現表單驗證功能(驗證手機號是否存在,驗證
獲取文本框內容,進行驗證是否為空,驗證手機格式時候正確
⑺ JS表單驗證,按鈕驗證,驗證成功怎麼實現跳轉
<form action="reg.html" name="loginForm" id="login" onsubmit="retrun checkdata()">
return寫錯了,應該是return,你寫的是retrun。檢查一下…版…
另外把權
if(checkName()==false)
return false;
else
return true;
if(checkPassword()==false)
return false;
else
return true;
改成
if(checkName()==false)
return false;
if(checkPassword()==false)
return false;
else
return true;
⑻ js表單驗證代碼怎麼寫
我來舉個例子,很簡單的
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function formCheck(){//表單驗證
var userForm=document.forms.userForm;
if(userForm.username.value==null||userForm.username.value.length<=0){
alert("用戶名不能為空");
return false;
}else if(userForm.username.value.length<6||userForm.username.value.length>20){
alert("用戶名必須為6-20位");
return false;
}else if(userForm.password.value==null||userForm.password.value.length<=0){
alert("密碼不能為空");
return false;;
}else if(userForm.password.value.length<6||userForm.password.value.length>20){
alert("密碼必須為6-20位");
return false;
}else{
userForm.submit();
}
}
</script>
</head>
<body>
<form action="" name="userForm">
userName:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="button" value="提交" onclick=" formCheck()">
</form>
</body>
</html>
⑼ JSP中用js實現注冊表單的驗證
jquery的框架 有自帶的驗證 都是別人幫你寫好的
看能不能符合你的
⑽ js怎麼寫表單驗證
1. 長度限制
<script>
function test()
{
if(document.a.b.value.length>50)
{
alert(」不能超過50個字元!」);
document.a.b.focus();
return false;
}
}
</script>
<form name=a onsubmit=」return test()」>
<textarea name=」b」 cols=」40〃 wrap=」VIRTUAL」 rows=」6〃></textarea>
<input type=」submit」 name=」Submit」 value=」check」>
</form>
2. 只能是漢字
<input onkeyup=」value=」/oblog/value.replace(/[^\u4E00-\u9FA5]/g,」)」>
3.」 只能是英文
<script language=javascript>
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>
<input onkeydown=」onlyEng();」>
4. 只能是數字
<script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//考慮小鍵盤上的數字鍵
event.returnvalue=false;
}
</script>
<input onkeydown=」onlyNum();」>
5. 只能是英文字元和數字
<input onkeyup=」value=」/oblog/value.replace(/[\W]/g,」」) 「onbeforepaste=」clipboardData.setData(』text』,clipboardData.getData(』text』).replace(/[^\d]/g,」))」>
6. 驗證郵箱格式
<SCRIPT LANGUAGE=javascript RUNAT=Server>
function isEmail(strEmail) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
return true;
else
alert(」oh」);
}
</SCRIPT>
<input type=text onblur=isEmail(this.value)>
7. 屏蔽關鍵字(這里屏蔽***和****)
<script language=」javascript1.2〃>
function test() {
if((a.b.value.indexOf (」***」) == 0)||(a.b.value.indexOf (」****」) == 0)){
alert(」:)」);
a.b.focus();
return false;}
}
</script>
<form name=a onsubmit=」return test()」>
<input type=text name=b>
<input type=」submit」 name=」Submit」 value=」check」>
</form>
8. 兩次輸入密碼是否相同
<FORM METHOD=POST ACTION=」">
<input type=」password」 id=」input1〃>
<input type=」password」 id=」input2〃>
<input type=」button」 value=」test」 onclick=」check()」>
</FORM>
<script>
function check()
{
with(document.all){
if(input1.value!=input2.value)
{
alert(」false」)
input1.value = 「」;
input2.value = 「」;
}
else document.forms[0].submit();
}
}
</script>
夠了吧 :)
屏蔽右鍵 很酷
oncontextmenu=」return false」 ondragstart=」return false」 onselectstart=」return false」
加在body中
二
2.1 表單項不能為空
<script language=」javascript」>
<!–
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert(」請輸入您姓名!」);
document.form.name.focus();
return false;
}
return true;
}
–>
</script>
2.2 比較兩個表單項的值是否相同
<script language=」javascript」>
<!–
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) {
alert(」您兩次輸入的密碼不一樣!請重新輸入.」);
document.ADDUser.PWD.focus();
return false;
}
return true;
}
–>
</script>
2.3 表單項只能為數字和」_」,用於電話/銀行帳號驗證上,可擴展到域名注冊等
<script language=」javascript」>
<!–
function isNumber(String)
{
var Letters = 」1234567890-」; //可以自己增加可輸入值
var i;
var c;
if(String.charAt( 0 )==』-')
return false;
if( String.charAt( String.length - 1 ) == 』-』 )
return false;
for( i = 0; i < String.length; i ++ )
{
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()
{
if(! isNumber(document.form.TEL.value)) {
alert(」您的電話號碼不合法!」);
document.form.TEL.focus();
return false;
}
return true;
}
–>
</script>
2.4 表單項輸入數值/長度限定
<script language=」javascript」>
<!–
function CheckForm()
{
if (document.form.count.value > 100 || document.form.count.value < 1)
{
alert(」輸入數值不能小於零大於100!」);
document.form.count.focus();
return false;
}
if (document.form.MESSAGE.value.length<10)
{
alert(」輸入文字小於10!」);
document.form.MESSAGE.focus();
return false;
}
return true;
}
//–>
</script>
2.5 中文/英文/數字/郵件地址合法性判斷
<SCRIPT LANGUAGE=」javascript」>
<!–
function isEnglish(name) //英文值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return false;
}
return true;
}
function isChinese(name) //中文值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return true;
}
return false;
}
function isMail(name) // E-mail值檢測
{
if(! isEnglish(name))
return false;
i = name.indexOf(」 at 」);
j = name dot lastIndexOf(」 at 」);
if(i == -1)
return false;
if(i != j)
return false;
if(i == name dot length)
return false;
return true;
}
function isNumber(name) //數值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charAt(i) < 」0〃 || name.charAt(i) > 」9〃)
return false;
}
return true;
}
function CheckForm()
{
if(! isMail(form.Email.value)) {
alert(」您的電子郵件不合法!」);
form.Email.focus();
return false;
}
if(! isEnglish(form.name.value)) {
alert(」英文名不合法!」);
form.name.focus();
return false;
}
if(! isChinese(form.cnname.value)) {
alert(」中文名不合法!」);
form.cnname.focus();
return false;
}
if(! isNumber(form.PublicZipCode.value)) {
alert(」郵政編碼不合法!」);
form.PublicZipCode.focus();
return false;
}
return true;
}
//–>
</SCRIPT>
2.6 限定表單項不能輸入的字元
<script language=」javascript」>
<!–
function contain(str,charset)// 字元串包含測試函數
{
var i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
}
function CheckForm()
{
if ((contain(document.form.NAME.value, 」%\(\)><」)) || (contain(document.form.MESSAGE.value, 」%\(\)><」)))
{
alert(」輸入了非法字元」);
document.form.NAME.focus();
return false;
}
return true;
}
//–>
</script>
1. 檢查一段字元串是否全由數字組成
—————————————
<script language=」Javascript」><!–
function checkNum(str){return str.match(/\D/)==null}
alert(checkNum(」1232142141〃))
alert(checkNum(」123214214a1〃))
// –></script>
2. 怎麼判斷是否是字元
—————————————
if (/[^\x00-\xff]/g.test(s)) alert(」含有漢字」);
else alert(」全是字元」);
3. 怎麼判斷是否含有漢字
—————————————
if (escape(str).indexOf(」%u」)!=-1) alert(」含有漢字」);
else alert(」全是字元」);
4. 郵箱格式驗證
—————————————
//函數名:chkemail
//功能介紹:檢查是否為Email Address
//參數說明:要檢查的字元串
//返回值:0:不是 1:是
function chkemail(a)
{ var i=a.length;
var temp = a.indexOf(』@');
var tempd = a.indexOf(』.');
if (temp > 1) {
if ((i-temp) > 3){
if ((i-tempd)>0){
return 1;
}
}
}
return 0;
}
5. 數字格式驗證
—————————————
//函數名:fucCheckNUM
//功能介紹:檢查是否為數字
//參數說明:要檢查的數字
//返回值:1為是數字,0為不是數字
function fucCheckNUM(NUM)
{
var i,j,strTemp;
strTemp=」0123456789〃;
if ( NUM.length== 0)
return 0
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//說明有字元不是數字
return 0;
}
}
//說明是數字
return 1;
}
6. 電話號碼格式驗證
—————————————
//函數名:fucCheckTEL
//功能介紹:檢查是否為電話號碼
//參數說明:要檢查的字元串
//返回值:1為是合法,0為不合法
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp=」0123456789-()# 「;
for (i=0;i<TEL.length;i++)
{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
//說明有字元不合法
return 0;
}
}
//說明合法
return 1;
}
7. 判斷輸入是否為中文的函數
—————————————
function ischinese(s){
var ret=true;
for(var i=0;i<s.length;i++)
ret=ret && (s.charCodeAt(i)>=10000);
return ret;
}
8. 綜合的判斷用戶輸入的合法性的函數
—————————————
<script language=」javascript」>
//限制輸入字元的位數開始
//m是用戶輸入,n是要限制的位數
function issmall(m,n)
{
if ((m<n) && (m>0))
{
return(false);
}
else
{return(true);}
}
9. 判斷密碼是否輸入一致
—————————————
function issame(str1,str2)
{
if (str1==str2)
{return(true);}
else
{return(false);}
}
10. 判斷用戶名是否為數字字母下滑線
—————————————
function notchinese(str){
var reg=/[^A-Za-z0-9_]/g
if (reg.test(str)){
return (false);
}else{
return(true); }
}
2.8. form文本域的通用校驗函數
—————————————
作用:檢測所有必須非空的input文本,比如姓名,賬號,郵件地址等等。
該校驗現在只針對文本域,如果要針對form裡面的其他域對象,可以改變判斷條件。
使用方法:在要檢測的文本域中加入title文字。文字是在提示信息,你要提示給用戶的該欄位的中文名。比如要檢測用戶名
html如下<input name=」txt_1〃 title=」姓名」>,當然,最好用可視化工具比如dreamweaver什麼的來編輯域。
如果要檢測數字類型數據的話,再把域的id統一為sz.
javascript判斷日期類型比較麻煩,所以就沒有做日期類型校驗的程序了.高手可以補充。
程序比較草,只是提供一個思路。拋磚引玉! :)
哦,對了,函數調用方法:
< form onsubmit=」return dovalidate()」>
function dovalidate()
{
fm=document.forms[0] //只檢測一個form,如果是多個可以改變判斷條件
for(i=0;i<fm.length;i++)
{
//檢測判斷條件,根據類型不同可以修改
if(fm[i].tagName.toUpperCase()==」INPUT」 &&fm[i].type.toUpperCase()==」TEXT」 && (fm[i].title!=」"))
if(fm[i].value=」/blog/=」")//
{
str_warn1=fm[i].title+」不能為空!」;
alert(str_warn1);
fm[i].focus();
return false;
}
if(fm[i].id.toUpperCase()==」SZ」)//數字校驗
{
if(isNaN(fm[i].value))
{ str_warn2=fm[i].title+」格式不對」;
alert(str_warn2);
fm[i].focus();
return false;
}
}
}
return true;
}