获得本站免费赞助空间请点这里
返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:7 a! Y5 X" E3 J. W1 o3 P
<script language="javascript">     
- C; X! l' C1 a: n6 N/ mfunction IsDigit(cCheck)     2 S4 m7 _9 W0 A9 J7 D' n' {" r3 j
{     
( m) E! W( F+ f, ` return (('0'<=cCheck) && (cCheck<='9'));     ( n. ], f3 m& V. ~+ j1 o0 n' f
}     
5 D* e$ E5 J- m+ p/ G3 g, x% E     % W0 ?: C- T/ ?: x' t
function IsAlpha(cCheck)     
! i5 M) ^5 X, V& J/ a) y, x* M- p {     % i. I- ^; B& V' j+ [
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
- J; F* |# z8 U% b9 ` }              
* d6 H; E; v' ~2 N; T# B; ?" D* j! M      
  J% _, O$ O# hfunction IsaNull(cCheck)         
& _+ K2 B$ I" O {          5 D; O8 c" f$ I/ P, D1 W( p
return(cCheck != " ")         % i$ j: z  {% d: s6 A, `8 l
}                              
9 A  c1 l  B: d! E' p2 Z      
6 Z6 t# t/ t  ^* Cfunction checkform()     - X9 l" P5 C8 H: X
{ + o! ]- v2 m: l- x
  id = document.sform1.id.value;     9 u, P7 @, d$ R: ?
if (id == "")     1 a6 A# o* p' @
  {     
2 q7 o3 ^' A- h, O  alert("请输入注册名");     1 `6 N  ~; v3 m) A; `
  document.sform1.id.focus();     8 g* s) V1 R, A( i3 s
  return false;     0 w# l" c# P! |" n- F
  }     # H( \, C% S& m
     + h: q, o. t6 a- Z" J) Q
for (nIndex=0; nIndex<id.length; nIndex++)     
; V+ ~0 b" n" a: t  V* ]2 u8 ?  {     4 r' f% o% d4 S  L8 s
  cCheck = id.charAt(nIndex);     
# K+ c/ S: E8 l1 q) E* ^  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     9 b4 d2 I5 u+ A/ e
   {     
9 l% I4 `+ k" u6 s( R2 r! x   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
# o( j8 V0 ~+ G! o8 J  I9 ?   document.sform1.id.focus();     
1 J: j4 a% L$ Z: x6 g# J  Y) a   return false;     
+ v; j, `6 a1 x   }     2 k6 w: c& h4 g# Q9 O
  } 5 N  z6 o1 _- \2 L4 B
  chineseid = document.sform1.chineseid.value;     ( ]3 D5 T9 P- j% \0 ^
if (chineseid == "")     
7 k" e0 g  U7 ~" A7 t+ _+ n; m. g$ P  {     * ]% u# Q6 ^1 m1 \" ?( R9 f1 a# \! y; I
  alert("请输入中文昵称");     4 o6 G" l( G6 B4 h8 M  r+ I) l
  document.sform1.chineseid.focus();     
7 v# X+ x4 b5 m2 I& O  return false;     3 F% F3 V1 K3 `' Z
  }   
: P# {% ?: F% Z password = document.sform1.password.value;     
# s& g% `  G3 P: [/ Z" U/ f% r! o if (password == "")     8 x5 X$ m$ z' \& k9 ?
  {     
1 S' ~4 e6 d% V" u  alert("请输入登陆密码");     
( S) J- J' y# a9 K  document.sform1.password.focus();     6 G1 b8 f6 ~4 l; i
  return false;     
' Z) Q* W( C. }+ Q& u, s, {  } 1 s6 |# o0 V  Q0 K' g
password1 = document.sform1.password1.value;     , }( z4 c4 {% v" q3 i/ H  D
if (password>password1)     + q' \, o/ S  A0 X9 r
  {6 e# U9 [: B; M" U( M6 {+ w
     alert("重复密码与登陆密码不相同");     
7 s! }" `+ n! U5 e: h# @' a4 z  document.sform1.password.focus();
' W  O9 \" R9 i& n                                document.sform1.password1.focus();     
  W% [) d: U9 ]7 ]  return false;
& t; u; l& w* b  }  " f  z  |! ]+ R  f; f$ H7 j# @. V) b
if (password<password1)     
, h% P" V) y1 V4 v6 G3 F, H. R7 y  {* b/ m( F7 t2 k& b8 O+ ~' c5 e
     alert("重复密码与登陆密码不相同");       m+ }4 X" W) o1 _' E! C
  document.sform1.password.focus();
0 @! H5 K* r8 P+ S& R                                document.sform1.password1.focus();     6 Y3 U4 x2 x- E! u: l1 k; x
  return false;0 e' Z8 q; |$ c4 A  J- G
  }
0 f3 K  s+ M# [, `+ S6 t- s if (document.sform1.email.value == "")     - z5 U1 i1 u# r4 r( ?# H) K
  {     
' s/ v% l- t) ?  `/ g  alert("请输入您的E-MAIL地址");     
- M% w) G1 T4 ]1 g, B* ~( ]+ E  document.sform1.email.focus();     
2 V; R( N- w; O6 P9 W  return false;     
8 M2 g  _9 i. J3 D  }     
2 v. w: }" t# |      
/ @+ s* G$ M# c$ U email=document.sform1.email.value;      
+ n; J* U, Q* K0 n2 x7 P# B emailerr=0     
  X. |% Q# t$ e0 m" V- S for (i=0; i<email.length; i++)     / j& o; z8 d& o& x$ c; f
  {     
" V$ d- o( J7 l7 W  if ((email.charAt(i) == "@") & (email.length > 5))     7 \6 h* k) r& e1 _5 @2 t
   {     4 f: m& m7 K7 h& \, t
     emailerr=emailerr+1     
  A  Q& J% I) _2 V+ |   }     / n- t) s! F8 p4 x: f8 z
  }     
. C! o: l- n  ~ if (emailerr != 1)     2 T7 t( Q. B% l2 U) n# E
  {     & d' E& u# H; g% z3 @5 P; ?2 j
  alert("请输入正确的E-MAIL地址");     $ {: b  S, X7 G. y* b
  document.sform1.email.focus();     2 D  L) v$ O# L: J) w$ X+ ^
  return false;     
% ]; Q" V9 _0 F3 S/ n3 d& K  }           
- F7 y9 ]; a, Y0 M+ P: a  / Y% y0 |9 Y( b. P- e! S
if (document.sform1.checkask.value=="")
1 V6 Z9 Z# Y  N2 I6 O2 e" e" C/ G" ^  { . N- U1 H2 ?- k6 R; l1 M' n. T# V
   alert("密码提示问题不能为空");     
3 t9 a' C1 y$ D: e   document.sform1.checkask.focus();     7 L, G1 ?2 _* w( `: C
   return false;  $ [: C* n. k) f8 y8 S
  }
6 X+ M& o+ B6 n! m if (document.sform1.checkans.value=="")
% E3 V% W2 N( U7 F$ q) V  { . s' Z0 L$ |- |% B4 N- O
   alert("您的密码提示问题答案不能为空");
/ s/ y4 ~. D$ s6 q   document.sform1.checkans.focus();     5 z; W( w" z. x) O0 n
   return false;  6 `) E# x! h2 I/ y; T7 P% S
  }4 r/ l0 i$ x1 i2 q% r
return true;     
' b) r, h& t. A6 u+ P1 f     
( h9 M% k( p/ k7 ~5 g7 e3 |1 Q  M}
4 M# X  @% o/ V- Y1 T</script>

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