返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
8 V" Q, S) F# f* h* D& c+ t9 L<script language="javascript">     0 J4 t$ G6 h, t. Z
function IsDigit(cCheck)     1 Z/ V& L- e" S+ [* y0 f
{     
$ n7 g% ]; ~6 G" Q: X return (('0'<=cCheck) && (cCheck<='9'));     4 ^5 S* K+ I" Q- B& o& S  Y
}     9 u. m" y3 r4 e9 x0 u7 t; U
     
6 p" R  j9 ~% w2 x! c. ^( `function IsAlpha(cCheck)     
, _2 a4 V6 a+ H9 o5 b$ U {     
+ u! d4 J% j/ d+ [0 |! W% i return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     ' w4 e' h. q% m% K3 I
}              ( q1 [' m, C/ P/ e* i  t
      
0 s% T$ n$ T/ g! D1 ]5 K% \% sfunction IsaNull(cCheck)          2 u1 i, s' M( W0 A4 ]
{         
* Z2 x$ s+ R; s: v8 f& R return(cCheck != " ")         ( Q0 }' ]' g% }+ T$ w1 [+ e
}                              
1 M4 H, n* q3 v+ u      
" c: r; w: w% _function checkform()       b' G8 g6 W( b' {) O1 c! [. }$ y2 F
{
( O6 i$ s) B( J8 p5 s1 j  id = document.sform1.id.value;     
6 J# U  r, z/ Q% Y: y if (id == "")     
5 R) U. ]% ~, r  {     & G2 w1 v; X  n$ `3 y, P7 ]
  alert("请输入注册名");     
- v7 ^& v. T3 ?+ U7 T  document.sform1.id.focus();     
5 @9 Y" ]) M- Y$ J9 G/ O2 v7 u. w  return false;     ' n2 n( D) G4 t& H) _% a3 m# v
  }     
! D/ L) j+ B  q1 V- w     
7 q& h; i1 V4 r5 a! v for (nIndex=0; nIndex<id.length; nIndex++)     * O1 J& g3 L2 d0 L" u
  {     
( j( O7 r$ ~( r: Y1 ^+ T: H; w  cCheck = id.charAt(nIndex);     
0 O  L) W' b. u3 `( k( ^  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))       k# W4 ?8 G- E: t, l
   {     / f; f9 Q! i( s- C/ p  S" K: Z
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
' E2 B9 K( u! y( u6 S( z* V   document.sform1.id.focus();     
4 g' U- f9 b6 x9 m2 K   return false;       g7 B* w8 D. A1 q: {* Z
   }     - l4 K0 |  G, F# ]9 g$ E
  }
; i/ @" q! J* ~# S' n  chineseid = document.sform1.chineseid.value;     , z3 g' }7 W  J1 B" Q
if (chineseid == "")       U5 E  f# S, b7 K; U: O, m2 v# `
  {     
% G" f; k+ m8 T6 l/ g' ^" K+ @. }  alert("请输入中文昵称");     
" d5 l: F; W- z' ]( q. f) u- }  document.sform1.chineseid.focus();     
; J% N! K# ]& N( @$ Y2 K  return false;     
8 |4 R3 T8 g3 i4 T1 d: X/ x; n: M  }    & ?. }* W+ {* H. f. z% [
password = document.sform1.password.value;     
3 @9 [6 F4 d9 ^: O if (password == "")     
( L. D& p4 S% u( I  {     
4 J7 _$ y: x. u: A8 D) y7 U/ e  alert("请输入登陆密码");     
3 w" u7 {3 `: x( `0 ?3 q( o8 F  document.sform1.password.focus();     
" L, q( ]( ?& W1 U  return false;     
8 y4 y% P' N4 W4 d  }
$ q: Q) l. K5 d  Z7 r) _0 s password1 = document.sform1.password1.value;     
5 b: b3 L* w( a( Z) k if (password>password1)     
& M) B( t! C1 b& W1 V  {
- G  t9 T8 ]* `3 P     alert("重复密码与登陆密码不相同");     . u/ _/ \3 c8 n# b, X& L' U, U" r# c
  document.sform1.password.focus(); / |9 U* G5 X; ~) s
                                document.sform1.password1.focus();     + P: S/ A8 f+ p2 u( _
  return false;. A3 x5 o( K: y0 ?/ B5 z  S, [% ^) I
  }  
2 R* Q4 [8 |" E  ~8 Z4 y8 D+ P& Nif (password<password1)     $ h7 t0 L/ F/ W: j
  {
- c  p7 \8 d) L- P  w/ `8 S* n) k     alert("重复密码与登陆密码不相同");     : L1 X# \! Y' m% _2 g2 p5 j2 M
  document.sform1.password.focus();
  ~  c8 y: C+ ]& H4 P* o" X0 U                                document.sform1.password1.focus();     
4 v8 j6 G" x0 ~7 f& ~" Q( ~/ @  return false;
$ v  \% `; r! P3 ]" n. m  }
$ [0 a; R+ i' n if (document.sform1.email.value == "")     
2 B% e. y2 z- A7 c2 u+ G  {     
5 Q- q  e/ h! E( ]+ K5 x  alert("请输入您的E-MAIL地址");     
& H! E. Z+ R# \  document.sform1.email.focus();     
% Z) V$ K% B4 H( Q  T% S! m' [  return false;     5 @# W: g7 M$ H
  }     , k# w9 C, X; X: k
      - o9 U, h$ p+ ~/ @- K
email=document.sform1.email.value;      
& h1 W3 Q$ _$ g1 l1 Z" x2 z' ^; ~ emailerr=0     
" G9 t! W) ]$ J6 ~ for (i=0; i<email.length; i++)     : T+ D0 N& s8 K$ p0 @2 }( M  o8 m
  {     
! E$ N/ A. G% a8 U# V& i9 H. z  if ((email.charAt(i) == "@") & (email.length > 5))     + P& X0 S( i+ T+ @
   {     
' i/ f0 M. m/ L* d! u     emailerr=emailerr+1     & ?1 W2 @3 U& M! j3 V
   }     / ^# I( R) @& X3 m
  }     
& S9 t1 _! c( x. D. V6 I$ f if (emailerr != 1)     1 ^, n8 t  t5 `7 x4 H3 e$ q) \
  {     
/ ^) ~! e4 Y3 P: O7 s6 D3 h  alert("请输入正确的E-MAIL地址");     8 p2 \: ?, d: q: o
  document.sform1.email.focus();     " k4 k# `! t% z% g* e, @, ^1 E
  return false;     
* P1 T& s; F# }( a. {! L9 j" @  }           ' e) V6 w0 ~3 G* t- {( e2 B/ J
  9 _+ P; l+ v  }5 l; F
if (document.sform1.checkask.value=="") . N5 y) K# N) l1 b2 Z+ S4 u
  { % a+ T2 A+ t0 a
   alert("密码提示问题不能为空");     
4 g) Z1 V/ G% k4 F. W   document.sform1.checkask.focus();     ; r4 T! x+ E# r( q3 o$ [
   return false;  
5 I, A+ h; }5 I6 L3 N( r  }
6 t5 j4 n6 i, z; ]6 c! ?& L! @" O; x9 ` if (document.sform1.checkans.value=="")
9 r! m2 G8 E# Q  { 8 O; g) C  o2 p! L0 M+ R, q3 O; t7 q
   alert("您的密码提示问题答案不能为空");
& w, y( s2 w( E2 n$ X" g; d' I" c   document.sform1.checkans.focus();     
. A; q8 Z7 t/ u# j6 Q0 C  q   return false;  
7 ?3 N: k) b. X. ]9 s4 D! @  }
5 R; S  x+ `5 G, v return true;     
: D5 w+ o8 \3 Q% H1 O6 ^+ U1 q! d     
5 G$ r3 x8 t0 W: L0 K}
( u- K/ |# |5 z# D5 d+ A' F</script>

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