返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:* M) l, @2 E  E- S2 F
<script language="javascript">     
6 h7 q6 {* T! m% y& P: e% A/ wfunction IsDigit(cCheck)     
% S, l5 p) P* C0 [& ^1 U {     - c6 b+ m3 L" H& i- J- S
return (('0'<=cCheck) && (cCheck<='9'));     
- a& K! w7 ^. |8 O2 o }     " a" O" k' R+ c6 }% I( M. O
     * E( k: [: y; V4 ?6 \" P+ k& H
function IsAlpha(cCheck)     
) K  C: B+ n# M* ^- s. @/ O; w9 q4 P {     ! l+ x9 F  a5 O$ k
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
! X9 T$ \" i: @ }              . ?9 f' P+ L1 I
      
6 x- {  J/ o# K3 N- t. Lfunction IsaNull(cCheck)          % e  W* ?) Z/ |  m
{         
6 G! e( f5 O+ ~1 x4 T  J return(cCheck != " ")         
7 ^/ H( T9 ?# v }                               ) X- x/ C) `( r* c' Y5 n" g$ ~5 y
        \. K9 p2 T- _$ s$ s8 x
function checkform()     
% ^4 t1 t4 v: v, j{
1 N' g, a. m& G( C4 d  id = document.sform1.id.value;     
4 P, q9 g  M8 K5 O- Y: d  ^) X0 N if (id == "")     
, u: _5 c7 E, E  I  {     
* G& l1 V' E5 |0 K  alert("请输入注册名");     4 N) Y1 E3 k7 t* j" ?  h& e
  document.sform1.id.focus();     0 U4 p' n: D- m" U) U) V% {$ I
  return false;     
7 S; V( f0 W3 B$ c4 E1 n/ C  }     
. b( _0 n( G; i5 V     * _, `9 s7 x( ]7 R6 F- t! [
for (nIndex=0; nIndex<id.length; nIndex++)     ' J7 K( }5 p# h
  {     ! h8 C( s* E% F2 f
  cCheck = id.charAt(nIndex);     
. V# W( J8 m# U  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
9 G9 E) r+ N# }" R4 r; T( Q2 |   {     
: ]6 z. B! c# r- h  g+ \% v" j   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     $ B8 X( A. V' t& |$ z. F
   document.sform1.id.focus();     
& K$ \' @! s* y: k2 P. Q4 d   return false;     
8 U$ w* ]/ h' D" U- s   }     8 \) g1 L2 q. M: ^. R
  }
, y1 O5 ^% }/ I" O% C$ Z2 t2 l) N  chineseid = document.sform1.chineseid.value;     
6 e' e- x9 Q- o' R" e6 B if (chineseid == "")     8 i5 l3 B7 M% e6 }$ U
  {     
0 U7 a7 [" |8 K; v9 U; u7 D0 S  alert("请输入中文昵称");     
- u/ k: e, {/ J  document.sform1.chineseid.focus();     7 f9 L" P+ s+ [# `; V" F
  return false;     - l5 \; p9 i- Q) h0 T0 O# Z+ @9 [
  }   
" @1 K. W% q6 }# {; j+ O: U* K password = document.sform1.password.value;     
; s% o. n- g% s, k, Y if (password == "")     - _. _' d# M  q" {* r
  {     
  l& Z/ s, Y" P) [1 o* h  alert("请输入登陆密码");     / o, i7 \- H/ r! W# L; k% K
  document.sform1.password.focus();     
1 c6 F, Y/ P  e8 J) M/ J$ Q  return false;     ) }8 R' d" K# Q, i( G
  }
! B0 y+ @& g% K1 k password1 = document.sform1.password1.value;     , N8 V4 @) U; }% \. i* \, P
if (password>password1)     8 P; Q0 c& A8 {% w+ b
  {
6 K, S! k7 u' V- N$ y* ~     alert("重复密码与登陆密码不相同");     
8 Q9 X" `5 _2 O' e2 \' J( C  document.sform1.password.focus();
% p0 v! d3 e4 }% O/ U; x                                document.sform1.password1.focus();     
) P, x3 u5 m% J$ Z  return false;
. @7 M% O' {6 J4 V5 m+ @# M  }  % V. P$ i, M7 V
if (password<password1)     
% W+ E" g5 M; L( U# N+ J  {
1 w) k0 A6 Z7 U$ B) H! L     alert("重复密码与登陆密码不相同");     # M$ M7 }6 i7 |% J8 j6 z+ j
  document.sform1.password.focus(); 7 N' Z+ B  D3 ]; g
                                document.sform1.password1.focus();     
; i+ [% e% [3 i" ~; Z/ N; M  return false;
% m% S8 I1 J! n  }
$ a5 ]' ]8 H7 K' _: v3 A1 S if (document.sform1.email.value == "")     : ~/ |. E8 \  C( X/ F  J0 W! _
  {     + P0 p9 m  O* Y- Z9 b" h+ S6 w% N4 u7 S
  alert("请输入您的E-MAIL地址");     
$ p, p0 v. b# u5 M" [3 b  document.sform1.email.focus();     
$ B( m. J: g( t, u) @  return false;     - a3 M/ S6 N6 d9 k! ?1 H# J+ `
  }     + W& r. o% \& ?/ Z
      
* B- Z  n: W6 X  b: q( p email=document.sform1.email.value;      ) p  C$ `  x8 A- F. `& L' U8 B: ~
emailerr=0     
, w) Q7 P3 l1 g' n% n4 C for (i=0; i<email.length; i++)     . h( p4 n; \; S$ A
  {     4 {' `& Z% x0 S$ N
  if ((email.charAt(i) == "@") & (email.length > 5))     
4 h3 s4 ~' `9 o1 I' `# z% D   {     ) R  t* \) `0 W; O: x( `
     emailerr=emailerr+1     - U/ a; O! S: \6 R1 S
   }     
* {; b% H0 z2 }- x/ b# B  }     5 P, ~/ s# \7 Y# }
if (emailerr != 1)     0 g! J2 I) C/ m5 l+ j
  {     - ~( H$ r% N, Q4 A2 ]$ n
  alert("请输入正确的E-MAIL地址");     % N$ T- x& m2 L3 f8 v5 }
  document.sform1.email.focus();     $ ^0 M  G/ K( ]2 J! B! n
  return false;     
, h2 O6 P; X5 w% Q* u, f& [8 ~. q7 k  }           
! V. e% Z, x) }    Q- M0 R8 ^1 D
if (document.sform1.checkask.value=="") 5 Q. b$ ^" `* \" e$ v% M
  {
9 f0 f0 D. s2 ?. n  \   alert("密码提示问题不能为空");     
, `2 D7 }/ D( G   document.sform1.checkask.focus();     3 ]# o6 Q2 S& b0 M
   return false;  ! F& ]" |  s1 a7 ^$ n3 U5 t5 }6 v
  } ) _7 j: M7 x* V- s; c( f  D
if (document.sform1.checkans.value=="")   @6 L5 G0 `/ C( D
  { 2 _. Q- m5 y4 f' D; h4 ]  ^
   alert("您的密码提示问题答案不能为空");
# Q  `" k1 B) t2 N   document.sform1.checkans.focus();     5 P- h0 f$ w) ?9 g4 F" i
   return false;  
- {; b. c  l( t! k! E# }  }
" A$ Z1 M, g, N4 v return true;     8 G3 |) B, {5 Y" z
     
3 H8 l! v4 t% Z}
% k  t2 @( j! n  Q: I; ]</script>

返回列表
【捌玖网络】已经运行: