返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:1 w% _. r1 T3 [: X/ w; K1 H8 _
<script language="javascript">     
" {7 T4 g& X3 b4 d* bfunction IsDigit(cCheck)     3 Z% k' P- ]7 z& T7 ]
{     + |5 j4 x8 u  \) N) J7 o/ A% H
return (('0'<=cCheck) && (cCheck<='9'));     
3 T' ]: L( q( @5 j  c8 X( T6 V7 d  S }     - f4 ~$ l) r  R5 j! Y! ]+ ]
     
, B, u) u$ V0 O$ v: \/ xfunction IsAlpha(cCheck)     $ H4 {5 \7 H/ V, L/ J/ ^6 U
{     
9 y/ k/ T# u0 `# s- `3 l  i return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
- a" k( `+ v2 \8 [) A  Y3 t3 w) f. K5 e }              4 Q& k. C) h" w+ h
      
% b; w& @- w$ Pfunction IsaNull(cCheck)         
" h, y/ ]9 L, |' E {          & l' Y# f5 u2 Y. s' s' o9 f
return(cCheck != " ")         5 b  E- x/ h3 e' L; ~
}                              
) T8 ^2 ?$ ?9 i* T$ I7 C      
) ~1 ?2 ~9 D6 E- T1 @! G2 yfunction checkform()     
& ?% n  V+ t; N" X  c+ i{
4 l. Y' f3 s( Z. N! @" A  id = document.sform1.id.value;     
) x- p; x$ p3 V) k if (id == "")     
2 A8 n- `$ L6 I" \- M$ x2 a  {     
+ W: W/ W& v$ @* r, @6 E* a  alert("请输入注册名");     0 n5 a4 [% \+ ~1 p9 K, v
  document.sform1.id.focus();     
! O( s0 O% S' \8 l5 F2 p. r# U& P  return false;     
/ f* C/ c8 M: a( V4 `% @9 S  }     
9 g% x) g8 @& h7 p2 ?0 b     " v% Q6 ~+ O6 ~
for (nIndex=0; nIndex<id.length; nIndex++)     7 W: X: j" x4 u. ?- L
  {     
5 {) ^9 M  e. @( Q  T, w6 v$ y  cCheck = id.charAt(nIndex);       r3 t8 q+ h8 Q
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
% ^$ d, v- }) W   {     3 Z: u. J# Y8 `8 A9 B0 D# o
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
6 C4 x4 P0 K* s& v. S   document.sform1.id.focus();     
, P$ L% q  V* Z   return false;     / L" g5 Y- u( ?
   }     2 R: `& ^  O: p! O$ c% H1 a
  }
: a  \9 u" t- W& C3 ^6 N  chineseid = document.sform1.chineseid.value;     
& p* A5 T/ ?- q if (chineseid == "")     
8 P% n& f$ y% b% K( `- ~) {+ I7 u7 {  {     
. D: G1 S/ Y0 ]3 T  \  alert("请输入中文昵称");     6 X( O/ d0 D* c
  document.sform1.chineseid.focus();     
" z8 l9 F' A7 ?9 I, z  return false;     & ?! e3 n) ^* c  ^& a; y2 x
  }    8 K; Z( O, T( J: ?
password = document.sform1.password.value;     
- j' ?4 b6 S& W7 {, I. x if (password == "")     
7 N3 z7 \- k, ^( ?/ G! F  {     ( r$ N0 m. F8 K/ N! w5 c, |  J
  alert("请输入登陆密码");     : H. l3 g( Y1 x- y3 h% I/ h% `# s
  document.sform1.password.focus();     # ?6 _, p( Y2 Y9 d  h  {
  return false;     ) ?3 c" g) x$ X. x$ z2 p" O7 ^
  }
3 S' y' e6 W2 [- ?( Q password1 = document.sform1.password1.value;     
- I9 s" `& n) i; ^, ^% c if (password>password1)     5 x6 h, K- _9 M) F0 h
  {6 Q3 p4 u  j3 z/ q* }1 t' i( b. m
     alert("重复密码与登陆密码不相同");     
6 h, F% k# w8 @3 V  document.sform1.password.focus();
: w! k- L5 V1 s                                document.sform1.password1.focus();     
- z4 K. E4 `5 P  return false;- h- Q% L# F+ x0 G4 R
  }  / V7 M" [* k1 l
if (password<password1)     
: G- J$ \- ?: A  {1 Q3 N! D  B; U, _9 L
     alert("重复密码与登陆密码不相同");     
2 ^8 S; L1 b6 E6 C  Q" R  document.sform1.password.focus();
0 @. a* N& a  D$ W# k6 w7 f                                document.sform1.password1.focus();     " v" B, w1 l" f" ^* |9 c
  return false;; R, {' z" B! e- \. ?
  }
2 N, N9 ?6 H/ q% W* c% L if (document.sform1.email.value == "")     * ?3 d' A* |2 G4 y: [; W
  {     * }9 M5 G+ `. X/ B" T8 @
  alert("请输入您的E-MAIL地址");     
( y5 U3 M1 P: k! ^% {( a0 i  document.sform1.email.focus();     $ q' D) C9 \9 Y
  return false;     
; f( Q- {: E% a4 E/ ~7 d& f  }     8 w3 t% g/ s7 V0 s) ^
      ; M: C; Z% `. ~4 b4 F
email=document.sform1.email.value;      
- V) y( q' U" B9 X6 v* ~; |& p emailerr=0     - F- M8 l0 d1 L* l
for (i=0; i<email.length; i++)     / ^. i5 S7 b0 U4 R
  {     
& O7 e: _$ p( |7 B9 K5 ?1 ]  if ((email.charAt(i) == "@") & (email.length > 5))     " i; ~! B3 Z. p4 x+ _3 \
   {     
. H4 C4 _9 l+ Z+ M. y     emailerr=emailerr+1     * k& G8 ~* U) n# l$ I* ?, v
   }     
  u  `8 M' L1 T  y' ^% D2 k% q/ q  }     
1 ?* B/ _& l/ ~% |0 T: T+ c( X  d if (emailerr != 1)     4 e" C, @* J7 p+ v7 x
  {     - t0 y- ^! _/ ~4 n* |; P
  alert("请输入正确的E-MAIL地址");     ' L3 N4 ~* O2 [+ g' y1 R" k( N
  document.sform1.email.focus();     
, y. F5 l( F8 z5 `; u" f$ F) v- F4 u  return false;     
% W9 A6 F5 g2 m* U4 H3 _+ M  }           4 N# k7 w, H/ S) p
  
$ W9 C0 e! j" k3 S; L3 g/ }, f if (document.sform1.checkask.value=="")
, x8 X, u+ K5 w6 U2 U& e: ^  {
2 p& n9 H5 h' g   alert("密码提示问题不能为空");     4 _! Y' \( r# ]
   document.sform1.checkask.focus();     % W  [8 K  B# L' D! Z/ b
   return false;  8 I, X6 f3 o6 X( h7 R9 j
  }
3 s6 @3 K+ Z0 D  h if (document.sform1.checkans.value=="") ' x" q0 k2 j. e6 h" K
  { ( ^& ^5 O' D: p9 d8 f
   alert("您的密码提示问题答案不能为空");
: O7 @" C' @5 w( q8 Z5 u9 B/ {- S% t   document.sform1.checkans.focus();     6 g" \6 f8 V% e8 ~4 |2 s0 s
   return false;  
; _6 A; c) N  G  }/ e! t  G% B: w8 y
return true;     
3 s- }8 Z/ c9 q+ d5 }) V8 f     
2 O9 }# b/ q( S1 Q3 s) X7 ]& @, S} ' j+ |* O$ X0 m" R: |
</script>

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