返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
6 ^/ B$ U8 q, t/ _<script language="javascript">     - b7 ?: o9 Z$ n: @+ V
function IsDigit(cCheck)     
) a$ \( g% R. ?; `7 ^) @: k {     0 v$ I/ F" K9 Z5 k1 l0 M, G
return (('0'<=cCheck) && (cCheck<='9'));     
, b" g" D" H- T. `6 n% _8 U }     - [5 }( h2 a: z! k5 y& ~9 h+ S5 a( D
     
; _( S4 c+ u- yfunction IsAlpha(cCheck)     
, w8 n- |$ T. F1 e {     ( L* A0 W  {, J! }3 U
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
$ Y8 z1 f( A" |1 }) b+ o }              
! t5 }/ [: H* {7 u4 F      7 ~( b4 k$ c+ C& J# e
function IsaNull(cCheck)         
$ Z# l3 y$ |+ o! b/ [ {          ' W( [8 ?# ?( t& G& ]5 t
return(cCheck != " ")         $ S0 E% r  P( ]0 e
}                               9 Y3 P4 |* A( Z' Z9 c4 Y/ e  g
      
% b* r( ?5 z# e' }function checkform()     
, b: a4 o8 r! W: ]" e{ 0 X7 l. Q5 y$ I! Y6 P
  id = document.sform1.id.value;     
1 Q1 ?0 P' A0 C, j! B' Q if (id == "")     - _. J, C8 Y% P5 x! m$ Q" C
  {     
! w" q2 s" i" d5 C5 \: K  alert("请输入注册名");     
# y, F" b/ ]9 P% v! {8 p2 w; E  document.sform1.id.focus();     
! k! [$ C, K5 K3 c: R  D/ O  return false;     4 H: K! k/ ^5 M+ S$ e
  }     # b1 ~0 N, f3 G# n
     . _$ w% ^4 M0 h, I* x( @8 T
for (nIndex=0; nIndex<id.length; nIndex++)     8 k6 S6 _7 c( L3 f6 T( i7 r% @' e) o
  {     - ^: ?  j1 n: v: p, ^3 \2 _
  cCheck = id.charAt(nIndex);     
, {- H2 [& @8 j" A/ o; C# B% ^  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
  B* ]6 c. b8 Y8 C: S- S( o   {     6 e! j: h; x5 m$ H
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
9 ?* l8 [. x, `( X2 @: w   document.sform1.id.focus();     9 @  g! L$ |# _) @2 K  I
   return false;     
$ |: m9 Y2 \' u   }     
! B- g, M) U% X+ g/ |  }  r9 ~  } * |' B) b& U1 ~2 i9 `/ R- y# D
  chineseid = document.sform1.chineseid.value;     
) N1 f1 `( \  [/ v  C  m if (chineseid == "")     
6 w5 |& A7 G) H; a, P  {     $ ~) v5 z9 L$ _( G# s+ S0 K
  alert("请输入中文昵称");     ! y. q4 Q3 Y$ A* r: d; i
  document.sform1.chineseid.focus();     , N0 B0 o( Q' Y1 |* r. @0 m
  return false;     
6 P+ f" F( b& ^3 L% d. A. W  }    + _( ]& x" t" C/ s" S
password = document.sform1.password.value;     9 `. V/ ]9 c3 s- ?! y
if (password == "")     
* s% c5 J) P3 J& _, p6 G0 B  {     
( c( O& Q. T4 }* ]: y  alert("请输入登陆密码");     5 x0 Z  Q+ ?2 d0 n
  document.sform1.password.focus();     , h" w4 {! o- r! H! h
  return false;     
" f6 D' A7 M& u3 _" c# o/ O  }
' D! Y0 h$ ?' ]: M password1 = document.sform1.password1.value;     
9 e8 t7 d" r1 E8 v! L. Z5 m if (password>password1)     
5 h( Q" e/ ^  {8 M4 e' X; r) C  {) o& F7 r( t% k- i6 ^% S' j1 X, \
     alert("重复密码与登陆密码不相同");     
; x! G0 ^/ \& U: z0 j* S  \# x  document.sform1.password.focus(); & y5 @8 x8 [- X4 n4 e6 I
                                document.sform1.password1.focus();     
) Z; u; r! U7 }% l  return false;% t4 b9 p. T* Z/ J
  }  $ l% H: \! H! H7 o
if (password<password1)     ! f! k0 @2 {2 M% n
  {/ N) f) Y2 s! W& G- }
     alert("重复密码与登陆密码不相同");     
# L/ q8 L& H+ ?, Y% }: }- F  document.sform1.password.focus();
) }) S; M) r( x; y+ i/ ?                                document.sform1.password1.focus();     
- D- a: D- r% Q( _1 a% a6 i  return false;$ O* r2 j2 G' q' m# e
  } + O1 s9 W# u# x; l
if (document.sform1.email.value == "")     
* B% {8 y7 o. V; h! t5 a! k  {     1 m- U) M- B+ Y% o& ?4 o" F0 M
  alert("请输入您的E-MAIL地址");     0 j( K% \( w: S/ ~4 G
  document.sform1.email.focus();     
- V: P1 h4 O& c+ B  return false;     
& H% E+ M' R& R( W, S# e( s6 g+ n  }     / K) @: g" ~# T! L; v& r4 B
      
% w' z% g% _/ t2 C( a2 N# D email=document.sform1.email.value;      . D5 Y5 U0 ^7 N8 y
emailerr=0     
' ^  u3 r& U- u6 D for (i=0; i<email.length; i++)     
) l3 f1 P$ `8 P7 d  {     
' ]1 t- Q! _$ n5 h; D  if ((email.charAt(i) == "@") & (email.length > 5))     
& M+ L4 _% `2 S   {     
" _0 e5 m7 `4 }     emailerr=emailerr+1     
% c  X+ ]0 L: `6 z5 c   }     ( Q3 }5 ]1 E7 O6 s: ~  k
  }     " D$ i% N! P  }; F1 I' m3 Q. Q
if (emailerr != 1)     
- o0 e' b! s& L1 }; l  {     
; Q6 E- {2 S) L4 W1 }% h( u' v  alert("请输入正确的E-MAIL地址");     
( @5 T( q2 m6 b6 s  \( P  document.sform1.email.focus();     
/ |5 V8 G! c% R4 k- C' l  return false;     
( ^' N+ \0 v7 ]2 A, `' _+ e9 e# l7 U  }           
3 w; r6 |/ w+ e  
4 V+ a/ _4 B; ^( j. O if (document.sform1.checkask.value=="")
0 f, [7 }/ ?" u7 Y( h! m  { ! S; _0 v7 O, k2 V; Q
   alert("密码提示问题不能为空");     # F  T$ @, n/ _
   document.sform1.checkask.focus();     - S# y6 W* Z+ _3 d8 K: W" M
   return false;  : T- a4 r& o0 y  |
  }
7 C7 o/ V( w) X; s% o- V if (document.sform1.checkans.value=="")
: _2 H3 b# t' @3 X  {
1 Y0 Z' t# k( |8 B' [   alert("您的密码提示问题答案不能为空"); ; H3 X- y3 b' h! Q/ O- E
   document.sform1.checkans.focus();     
0 I0 u" ?) m1 C, A3 ?   return false;  % p! A0 T0 v$ R+ C5 [
  }8 e# r) R, M1 R$ i( _/ x- e
return true;     2 O# u4 q! o: l
     
, v& [# F, a/ H6 q- {} 0 Q; J  H& J$ j  s8 U& x; \
</script>

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