返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
: a+ K: Z, v  S# m2 Z, s& x<script language="javascript">     
9 L) _3 p) c, Y% A- i0 t7 Q+ t6 bfunction IsDigit(cCheck)     
  V3 c8 V2 Q& [) V {     ( {& n% y* v  B, }
return (('0'<=cCheck) && (cCheck<='9'));     
3 u/ s" P, t, G }     
# v. N* s* F% Q, H     
4 |6 a# E, `6 [' v3 p; P: _) g6 yfunction IsAlpha(cCheck)     " E" f8 L# s! m+ G3 B; L# s" d" O
{     ' P2 y  G/ ]- w! m5 o/ c; i
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
7 [- E& o' X* D7 | }              1 `5 m" n" ~' d; b' {. a
      1 }6 O  y; l2 [' W( a
function IsaNull(cCheck)          / o+ r% T6 ~" |  V% y
{         
9 Z) J! _( A. c8 K) n- g* p/ b; y return(cCheck != " ")         7 N* S2 `: c  m
}                              
* l% L9 W- T) ?' Z0 C# h      2 u+ M1 o" H4 {
function checkform()     
8 `; n' ?- G7 w) d/ x  Y{ 8 s0 F- \0 [" c& E( w
  id = document.sform1.id.value;     . w! x3 H5 S/ z
if (id == "")     0 V' y, H# {; X  V8 D$ n4 M
  {     
2 b/ @2 D# F/ A% d3 W  alert("请输入注册名");     4 ^' ~* h( L7 ~. L
  document.sform1.id.focus();     / _# J* W( m7 r) L" ?% ~
  return false;     
8 F! l# N& l  q6 v4 Z/ D( Z4 g* E  }     ( M- [' V- b. F8 g
     
# g' p8 K% n* \6 }; ~ for (nIndex=0; nIndex<id.length; nIndex++)     3 S+ r  K! g, X+ E: f% z
  {     + W0 O6 \3 Q6 y1 {
  cCheck = id.charAt(nIndex);     
1 O+ [  ~  \- a$ V5 G& K  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     3 I2 j8 O' \+ t
   {     # r- `! i' R! ~3 ]0 L: H+ J" p
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ' S6 K" L: A3 y8 z7 k+ m
   document.sform1.id.focus();     
( T; O2 o* h. E4 o   return false;     
$ i0 w" N' R8 h2 i) {   }     ) M: m# J" v/ {) y
  }
1 r1 |; \9 `) H! W0 n  chineseid = document.sform1.chineseid.value;       R' {3 B5 L# S$ S
if (chineseid == "")     
& R, S8 I% i$ ^& G2 w4 [2 E5 H  {     " x7 f, }7 u  U4 v% S9 m; E( b
  alert("请输入中文昵称");     
$ {7 ?) c# l$ t2 K; v% y  document.sform1.chineseid.focus();     
8 S5 p) ^3 W, [6 T9 [+ \  return false;     
# C5 d' W* x$ l, K" S  }   
$ F. }) B% q* U( C4 C password = document.sform1.password.value;     , S. h, K3 Z0 l2 c' E/ L
if (password == "")     
- `$ q  q: O  K9 U3 Y  {     - Z" r0 ?) p' Q$ `, l
  alert("请输入登陆密码");     
0 T6 l1 d6 w) U  document.sform1.password.focus();     7 A5 L/ Z. n5 ~. G
  return false;     : i! i5 v" l& g0 o
  }
; \% V. ?  U& _2 |  s! z3 I, \ password1 = document.sform1.password1.value;     6 M( y: a" L0 j2 [" v
if (password>password1)     
( c* L) W  f( L5 L1 {  {) X2 M# i: V  q1 v
     alert("重复密码与登陆密码不相同");     
3 o, Z" n% h/ L1 f% p. |  document.sform1.password.focus();
* v& |: n& a# [: o                                document.sform1.password1.focus();     % \9 F" I' _# U4 F8 w
  return false;% y/ R- p5 w5 [4 T4 k
  }  
- \- M% j" o' @. t2 f( y4 o  _if (password<password1)     # \& ]- G* G# e* @& p
  {6 z" f) N! E- o/ y, d2 \
     alert("重复密码与登陆密码不相同");     % [0 r6 C3 @- R; q( n, Y
  document.sform1.password.focus();
7 [3 O# K. ?1 ^! _0 o                                document.sform1.password1.focus();     - U% e: v. e% N
  return false;
. g- f! L0 S3 ]4 {& `  }
+ B5 s" `$ ?4 x% U+ a# i: k( { if (document.sform1.email.value == "")     5 Y7 s5 C8 L7 j* F) `, R5 W
  {     
2 n0 r6 ^4 s7 I+ k7 A* Y, \, `  alert("请输入您的E-MAIL地址");     
/ P% C) U6 A! ?# p$ b4 X% q  document.sform1.email.focus();     
+ v9 d6 s) ]" U8 P7 t/ ?( u$ _2 k  return false;     
4 K; C7 N. r3 w4 [  }     , |$ F2 z4 p; c  \' w
      
  y/ z+ o0 ^. {* Q2 o email=document.sform1.email.value;      
% U6 X' P$ e* v: `5 ^ emailerr=0     : S  m7 X2 G9 A9 c; B
for (i=0; i<email.length; i++)     6 Y" b  `2 P- s
  {     4 u2 O& W$ Q. ^) K( i6 A
  if ((email.charAt(i) == "@") & (email.length > 5))     
0 H6 R7 H2 d3 w9 a   {     
! H) n4 F  d" ^: [     emailerr=emailerr+1     
& f$ k8 w# D& n) Y   }     
& ~6 q2 j1 Q& N  }     
" f/ T* k6 E6 _9 @% I if (emailerr != 1)     - l6 V$ |* X1 |$ D3 A
  {     5 V* x: i# X9 M! T" A0 \
  alert("请输入正确的E-MAIL地址");     - g' |2 Q* i3 v5 x6 m% d* h8 @% G
  document.sform1.email.focus();     
) t6 B3 b0 M. f) o/ O  return false;     1 W1 i+ b) Y: T7 l9 |$ Q$ I
  }           
" @% P3 p, l$ `0 B! e, B. V  
8 S: P, G0 `; g1 T8 T if (document.sform1.checkask.value=="") + ~! a8 V' C5 q  {" |5 x6 s
  { 4 B% r' o, H+ E: f# o  x1 W
   alert("密码提示问题不能为空");     
' a  H( t5 ?9 a   document.sform1.checkask.focus();     ) }+ F7 `: u" H& w( j: J# p
   return false;  3 q5 c# L8 U' V  |3 Y! H
  }   {& G6 {7 h% q# [4 p5 V7 c
if (document.sform1.checkans.value=="") , _. I; X2 p3 Y8 H$ Y3 C
  {   o6 b( R& E" x( h7 n! ]
   alert("您的密码提示问题答案不能为空");
: w' B' Y+ _% n   document.sform1.checkans.focus();     
) h; R% ~! _, l. Y. j: x) d   return false;  1 Q0 p1 c' }4 Z; `. O$ e
  }3 T* H# ~: J2 Y- F0 x9 S- I; i1 S
return true;     
* t3 u6 E0 m) D* W  w( q+ F     ' p8 d! _+ o2 h6 y: S) P; Z
}
/ ~3 y9 ?6 u$ z. U</script>

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