返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:/ c8 K# i4 C# v$ A" y
<script language="javascript">     
# h. a8 r0 \. x- Wfunction IsDigit(cCheck)     , J( L$ I; x& N' m
{     
3 u8 e0 n- }- ~1 d, P return (('0'<=cCheck) && (cCheck<='9'));     - I. x1 x5 n* }7 o4 y
}     
/ E& O: ]$ t" B7 ]- K5 h     
* N: w. n3 o- C' E$ mfunction IsAlpha(cCheck)     8 _2 d7 L) S, }/ R" `
{     
' I0 S9 O& O, G. m% W& z1 P return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
2 n) U- d$ z0 r2 f# r2 p, z! p8 | }              
. C. d6 F2 l* X. p* u, Y- k+ p      
: X6 b( j; H4 {0 f$ S, H9 Xfunction IsaNull(cCheck)          5 w( t+ m0 v9 j
{          3 E2 H9 ^2 Y3 k9 q4 }
return(cCheck != " ")         
4 k1 B7 u8 D3 m* v8 R8 r. H }                              
  n8 M6 ^9 O' X  V6 J4 p/ j      
' }5 N( X. d; a% B3 N/ ]  jfunction checkform()     4 ]2 |; K* p7 [3 P/ d
{ # J+ I2 [' b' f4 D6 }* `4 E
  id = document.sform1.id.value;     0 g+ f! B+ N# _( X+ q
if (id == "")     8 b; U  L5 e- C4 j+ s% O/ s
  {     
' _/ G8 L7 ^; a/ a  alert("请输入注册名");     
; M% Y/ ?' Q+ ~5 I3 p$ _  document.sform1.id.focus();     
9 Q' O3 S6 J, C) ^  L$ O) P* K  return false;     4 D" r& Q! b1 T, J6 s8 q# N, J
  }     
9 v7 Y- {  G# s! T8 t     ( }4 f* o9 n& c4 i! Y4 {; V
for (nIndex=0; nIndex<id.length; nIndex++)     
+ |  o( M" }$ ~/ A, f8 Y  O. `1 v  {     . l6 j) ~$ B& c. k; ~) B0 i
  cCheck = id.charAt(nIndex);     
% v* f, ]& Z/ Q0 ?) p  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
- U+ B! @' X3 w. q   {     
  o) r4 x( w( R" U   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     % i7 d5 T2 e& w5 y, D
   document.sform1.id.focus();     ; g8 P4 f) [/ M" W9 @
   return false;     
3 z6 e8 X5 e9 Y  ], [   }     , d  L+ ^4 C8 m8 X" T* q0 N% [
  } 7 h3 k) I9 ]1 n7 f
  chineseid = document.sform1.chineseid.value;     
/ P% u% w& f  i: K% h5 H; y) C if (chineseid == "")     
) Q( V9 N9 ~0 }4 {- r  {     - c1 f% ?; c4 D# d2 |& Y( |
  alert("请输入中文昵称");       X' Z- N. C; |& R- d9 g, q
  document.sform1.chineseid.focus();     2 V  B4 H" O' x5 P4 p
  return false;     + X: w6 F. ~/ o1 h+ _
  }   
+ b" U$ }, G, r6 ?# f2 k password = document.sform1.password.value;     
, B3 l. S- x2 D) a1 |, } if (password == "")     ) R6 |  B$ r9 w, e7 ^7 L, t/ Y# {
  {     - e' R" }( ?  m' X" u, f
  alert("请输入登陆密码");     - c0 C- \; _1 U0 G
  document.sform1.password.focus();     . w- y9 J, x  P5 ]5 F
  return false;     " W- S( m. F7 E9 q9 R1 l
  }
1 r* H) k, M7 [, y password1 = document.sform1.password1.value;     
8 W* H6 C+ l) N6 F6 ]: s' m if (password>password1)     + T5 G, B2 Y+ x. e: _& F5 q
  {. `3 E* \' E9 t  q4 K0 z
     alert("重复密码与登陆密码不相同");     
, A) T2 G* _7 M: q7 P7 Y; L7 L  document.sform1.password.focus();
* q- E/ C9 E1 g7 v$ [7 l                                document.sform1.password1.focus();     
( c4 R, R0 G" C  w- F7 V& n  return false;! [; w% D2 X4 h8 a1 ?/ O4 L' W
  }  9 g, s# N$ |. U" U7 V5 |) B
if (password<password1)     
' a/ t& U; F: u& o0 n  {
4 g" b+ U1 D2 P     alert("重复密码与登陆密码不相同");     
1 J. q, v3 Y0 [& |+ l! k4 [  document.sform1.password.focus();
' }4 T& B4 `$ L3 k) E  l2 W                                document.sform1.password1.focus();     1 {7 r* o6 [: }) [+ v$ T+ p
  return false;
8 s% v1 Z$ ^" H& [' ~  }
$ r, `( U1 c+ [; ]* ^ if (document.sform1.email.value == "")     
- U! Y& [1 w& W! i) x% P  {     
6 h1 z4 ?: V* D% z6 |/ T  alert("请输入您的E-MAIL地址");     * H6 R; t8 O6 h
  document.sform1.email.focus();     & [- S1 L) `- A
  return false;       B% J, t8 J( D  Y, m% u
  }     
* W, y" O. x' \, p" P8 U& W) |      
; ~! _3 E$ Z; C- _- J email=document.sform1.email.value;      9 _: ]' V, ~' h& l) B1 P, |1 u
emailerr=0     
! e- x0 l3 R. X6 w7 K for (i=0; i<email.length; i++)     1 D# F0 i2 G( x6 a& L
  {     
  b! J$ j5 h& E  H  if ((email.charAt(i) == "@") & (email.length > 5))     + W. X; F1 }7 q7 p* W
   {     0 g8 R! ?- X& r8 w2 _
     emailerr=emailerr+1     ' O9 ^; P; w" V: y! ^: u
   }     
0 O6 V6 ?2 I' ]  }     
; M" Z% l" R5 l. S" x7 a; L if (emailerr != 1)     
* e+ u( k1 r7 e' ]' a  {     . S5 F8 ~/ O- V' a
  alert("请输入正确的E-MAIL地址");     ( ?6 a/ R* U" I! ?, s' p
  document.sform1.email.focus();     
( Q' n+ Q& z: }8 `; [% S  return false;     
" W$ i. h5 n- q2 [% C  [  }           ) c# }; P4 j5 G9 b
  : \' ^9 L) s# K) y( U5 x- L, F/ b0 Z
if (document.sform1.checkask.value=="") 4 d* D# n: f3 \  B$ q5 U. z
  { 0 S2 l. j! E% D2 Z
   alert("密码提示问题不能为空");     
5 k0 n  ]1 p' E# h5 n   document.sform1.checkask.focus();     
- }6 E  W! }3 f  e4 q   return false;  ( c6 {5 W" @* d9 j! S
  }
+ M- W) b, y  `, Y; K9 X- J if (document.sform1.checkans.value=="")
7 ?6 u% B) d0 O  {
! Y8 c1 n1 K1 W1 r0 f' k/ i   alert("您的密码提示问题答案不能为空");
6 h# A4 @( y4 ~! |  J" k$ q/ Y+ T   document.sform1.checkans.focus();     $ ]( U7 h  V; B( U: m
   return false;  . Q' a  I8 ]/ ~2 x7 t
  }
, T- w1 o9 w+ o( R" I' t3 @3 K5 \ return true;     
  e8 U$ t) R  U# n' |' T9 D     % `, g, U. F7 ~; H/ C/ C* P2 K
} 3 m: W& P3 K: `' j9 H
</script>

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