返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:/ l. A; G/ o) q" m
<script language="javascript">     
" |7 M$ p; [0 [/ C, p0 mfunction IsDigit(cCheck)     
  @5 j9 F2 ?; x9 R. r9 a- T {     1 u9 ~+ f% X7 U
return (('0'<=cCheck) && (cCheck<='9'));     7 D0 \7 u8 X3 n  I' u
}     
- x. b* y$ t9 P* \% a     / a9 A% K9 k$ M
function IsAlpha(cCheck)     
! n8 q% C. N  { {     ! H& w5 C( Y' W; n1 J& h) ?/ [
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     2 P6 ^- J3 v  [1 i" T4 _- ^
}              
; D) f& M4 V0 ?) s      
4 j2 l) n3 Z0 U& @  E/ Lfunction IsaNull(cCheck)          4 p' p1 b* l* B- }) L6 g
{          5 k1 H0 q+ x9 b" G) T
return(cCheck != " ")         8 Z; l. o7 u/ O. c6 I' b
}                              
. b+ Y5 k8 t. H7 ]3 l0 {      
- V% W% |- ~" u- A! b& ofunction checkform()     
- N+ T: M5 ~) [% t$ q( w) l{ ! Q( e( J2 j; A/ {
  id = document.sform1.id.value;     + E' g& H* M* s. }% X% u. H  v/ O3 Y
if (id == "")     1 m' X" d: u  h$ N, Y8 m! {0 l
  {     
2 |" s4 q1 D* `; U, ]! j& J  alert("请输入注册名");     1 ~" H4 s* c7 }( t8 v& k5 a
  document.sform1.id.focus();     0 _) w( P' k& A7 q
  return false;     
, n6 M* l8 g. j' H: @' u1 M: ?  }     7 t$ G+ x9 P5 \% n
     
7 o* B6 I. s- L: e  j/ P3 Y2 M6 q3 r for (nIndex=0; nIndex<id.length; nIndex++)     
3 ?5 z% m( T' I+ ]+ Y# L  {     
& x& e# P5 O/ q6 {0 }3 J  cCheck = id.charAt(nIndex);     # _9 @2 S" W* T. T) N# f( J7 @" W
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
5 G6 q  \& C9 {9 t7 Q; }8 ~   {     ) L* G  K5 K: _  `2 r# ^0 \4 l- M8 L9 g
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
  _% O4 U  I8 U9 |# p   document.sform1.id.focus();     : t- i# w& A+ z' u
   return false;     & [( f9 u2 x* W$ q
   }     
% D, m8 I1 E0 r$ f0 X, Q  }
4 y1 W, ?& B/ l" d& Y- s, }+ w  chineseid = document.sform1.chineseid.value;     
# o* v/ @& F" r4 ~8 `" _ if (chineseid == "")     
0 _+ M5 |/ D: ?" [4 n  {     * A9 i* H$ {; v( i; M! T, V
  alert("请输入中文昵称");     0 k+ r3 O7 x% d3 e) s! d
  document.sform1.chineseid.focus();     ' ?# O/ W# o7 w; S8 P- M# V
  return false;     
! D: z( @1 [3 |, Q2 C3 U9 R% S( l  }    & J' P: ^4 _( Z
password = document.sform1.password.value;     - J8 v% `: t' W/ y: K+ G
if (password == "")     
3 u8 k, Y$ q: {. S, j% [9 h  {     ! ~/ O2 S) ~# b& ~% r) o( x
  alert("请输入登陆密码");     ( k2 a7 d+ h. f* |" ~4 W1 O+ y
  document.sform1.password.focus();     . e) _2 u8 A9 Y  T
  return false;     + L, M. {+ ]0 Y8 }; Q: m
  } 3 n& ~$ Z; s; z+ ]" N2 z
password1 = document.sform1.password1.value;     : R& S6 H7 l% z" b$ Z$ k& e$ g# V7 n
if (password>password1)     , N4 t! [5 e5 L0 X
  {
. T7 Q2 l7 h4 t/ N2 M  n" l+ Q: A     alert("重复密码与登陆密码不相同");     
# C$ k/ t5 t) N7 c) m  document.sform1.password.focus(); 3 t9 x5 n( [! y; ?- Z' W6 A
                                document.sform1.password1.focus();     
! l" |& z5 W( o* b; v3 ]$ i9 C  return false;
  c, y  m% @/ P* i1 `  }  
5 r# K0 W$ ~. W% g1 Aif (password<password1)     
. `! j( t& G5 o9 H  {
7 H0 _5 D. D) p5 Q% S, A     alert("重复密码与登陆密码不相同");     + J. l8 i9 d8 D: ^
  document.sform1.password.focus();
; U* [0 o( h0 b, E                                document.sform1.password1.focus();     0 O  v; ~& w' F+ M3 ]
  return false;, ~7 i' L7 D, D* ~3 p
  } . k& ~3 G% l. W2 q( p
if (document.sform1.email.value == "")     
; f- C( u; ?2 f/ h  {     
  y$ k" k# G/ {7 p" R  alert("请输入您的E-MAIL地址");     % p. e2 N, N' Z. r, ~8 N& ]! U
  document.sform1.email.focus();     
" w  n9 H& e% }5 ~+ L, G- ^  return false;     # D# L3 K3 |* s0 m7 y- e7 E7 A
  }     * }& F# o. p2 |5 c
      / Y: _9 W6 M2 k+ u) v7 M* ^( T% ]
email=document.sform1.email.value;      + Z$ z% N. {3 j" G
emailerr=0     
1 g5 o" h7 C8 f0 D for (i=0; i<email.length; i++)     9 A6 |+ `' j( h4 @& o
  {     8 l0 `* ^( @& P; x
  if ((email.charAt(i) == "@") & (email.length > 5))     ' v# ~5 ]( O7 Q
   {     9 B. R6 b9 V& k& v* K
     emailerr=emailerr+1     0 H+ y5 n) Z% s! B
   }     
; K" Q/ Y% }; ^6 \& O$ q8 k6 I5 ^  }     $ P1 R% z; o* Q8 t: u
if (emailerr != 1)     5 q0 Y8 Y- g/ m6 K" E$ X8 o0 M
  {     
) w6 |7 m( C# y* ^5 F" g+ \  alert("请输入正确的E-MAIL地址");     7 i9 G! @0 x. Q9 C/ ~9 \9 v; x
  document.sform1.email.focus();     
/ D3 i% x, c* I# S  return false;     
* L$ n% K; Q" b  }           ; Q: V( H& @, w  P  Q
  
/ ]! Q; f6 ~  |- T! e5 M% c. K# f if (document.sform1.checkask.value=="") 4 }8 Z: \+ m8 O2 ]
  { # D) U- \7 e9 O0 h
   alert("密码提示问题不能为空");     
0 h& ^. M. M4 \9 [1 ?, A   document.sform1.checkask.focus();     
6 q; h( f9 N" k" G- Q   return false;    u4 V' x8 N) j6 y
  }
) Y0 s$ ?7 S( _  P# I- n if (document.sform1.checkans.value=="")
9 e* C% s3 `2 q* y3 i0 r# J6 Z  { ( s/ V" j. a0 R+ c
   alert("您的密码提示问题答案不能为空"); 1 x# V+ z* J% K" E1 `* V4 l
   document.sform1.checkans.focus();     
! ~0 O! Z9 U' @   return false;  
; H; N2 ]" t! J' R  }
1 `$ [" _1 a/ ? return true;     
8 k6 N" z) S. \7 k     
3 X5 c* S/ n# a  E0 j} 1 D9 A' c8 s% r
</script>

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