返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:4 s2 c) D% e% K! A9 H8 }  m; R
<script language="javascript">     
  y+ a5 L$ a  U: x3 m1 sfunction IsDigit(cCheck)     ( v! }1 i7 |/ a* {7 P
{     
+ a' B" Y" g- ?5 \! ^9 \0 y return (('0'<=cCheck) && (cCheck<='9'));     5 B" u$ b8 Q1 J2 v
}     
2 b; c- F2 Y2 P! B/ `' Q- R     
: H4 A4 l* b, z- r$ [function IsAlpha(cCheck)     
- Y6 g( {- y4 M/ B' L& ?& ~1 w {     
: L1 {6 L- F0 ?9 i return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     5 g; h6 ^# \5 g
}              / T! X* ?$ w" N
      
# @1 V* c& u. \0 Y6 T, G; [1 B7 \function IsaNull(cCheck)          6 h2 m. N4 F( w
{         
" O( m6 g% _# c& \5 p! ` return(cCheck != " ")         
$ y2 A! A& a: b  @7 Z# @. d }                               - `% S$ H, U2 f9 H: c
      
* g! \- ?  _0 v9 o% V2 ?function checkform()     
. x; q. W/ d9 v{ / J, v2 G' s( B1 E3 X  Y
  id = document.sform1.id.value;     
, }2 B! b) c. K" Z% |+ ^% M if (id == "")     ) n; D' Y) i1 ^) ]
  {     
0 {; j0 N& \4 j/ {3 `" x  alert("请输入注册名");     ' u1 H3 F! W& N5 e! @8 I
  document.sform1.id.focus();       ~! w% C: w; V5 }# t' V, @1 Y# @
  return false;     ) a& S0 m0 c, _1 Y9 U: z6 r3 H
  }     " }) m% W# _% k" M" e
     
! Z% H+ |- A+ \" f$ J for (nIndex=0; nIndex<id.length; nIndex++)     + W/ G) X3 m0 z. }- X8 V$ u% w
  {     0 _* h# f7 _0 A' Y& R, @
  cCheck = id.charAt(nIndex);     $ J$ p0 ]1 `' f' Y) |/ j+ x6 t- C
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     / I3 N4 b8 `$ }# I  b! c
   {     1 g: U# l, [5 @( B6 F  t
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ) u8 e) Y8 A" C3 n( ?. i; V9 x
   document.sform1.id.focus();     
/ Q9 q1 i" _0 v   return false;     
7 k6 i6 g7 t+ J4 H6 g1 B   }     # w, x0 c# H! A4 c$ Q" a
  }
2 a7 n* y8 S& W: v# c  q' @  chineseid = document.sform1.chineseid.value;     ' u+ }' H& Z4 s6 d, x
if (chineseid == "")     : J1 P0 y* [5 f) b# Y  L/ \
  {     3 x5 |- K* v/ h2 ?! r
  alert("请输入中文昵称");     
0 }9 ^, y/ g7 D, M  document.sform1.chineseid.focus();     . q7 {- [5 v3 V) O; u
  return false;     
2 Q: w% B1 i2 W  }    + F4 |' b9 F! G; I1 S3 W8 v
password = document.sform1.password.value;     ' y! \! F; Q" T7 X& |- d! D- ~
if (password == "")     
9 ?/ y- m4 v4 r, ?- h" v  {     
7 a0 I' y! d) c- m. m& F  alert("请输入登陆密码");     9 L& j" i* F; |- E* r2 z
  document.sform1.password.focus();     
: l, k1 V! e5 v8 C; o$ I  return false;     
% c9 i0 z) k2 U  u2 U8 x  } 4 D2 K. T# @4 ^! R7 T$ M/ p4 ~
password1 = document.sform1.password1.value;     
' G# r8 @- R* c0 E3 g# f2 Y if (password>password1)     0 Y# i# I; [" x/ D
  {2 V# v" n( @2 H) _8 M" _; n5 k
     alert("重复密码与登陆密码不相同");     % b- k3 P& T5 n! C
  document.sform1.password.focus(); ; e3 i; f: _3 s1 _. Y* T; A/ O9 ]
                                document.sform1.password1.focus();     
5 N3 O" o2 W. H6 Y: A/ n  return false;
& v8 \: A- z) @6 S  }  2 Z( l& Y+ V5 m/ b1 A( A" w6 W
if (password<password1)     / Z9 l9 X3 [0 ]6 J% q- d* F
  {
% r5 h! h) Q: n7 i     alert("重复密码与登陆密码不相同");     
+ f& Z: B* I3 O( g+ ^  document.sform1.password.focus(); 5 [) c! K+ m) ?1 j' W1 _1 I
                                document.sform1.password1.focus();     
; z* Q5 \5 h0 f. s' z1 `  return false;
5 V$ f' t# x/ d- T# t  } ' Z& M5 X3 S- U5 t
if (document.sform1.email.value == "")     
  D* b; t1 [% V4 q$ l! I# m% u  {     ( Y, B" h9 o& E8 _2 V2 V0 X
  alert("请输入您的E-MAIL地址");     
% u! i2 K3 z% }1 T9 l  document.sform1.email.focus();     
  S' ?! `3 Z: K) D6 G! L0 ?  return false;     2 D6 o3 F7 J% I4 R
  }     - P. y" L0 t. @, Q
      ( H# j/ |3 c! E) `
email=document.sform1.email.value;      
$ @2 J' x. g( S/ R emailerr=0     
. A. G1 a* H: s4 x for (i=0; i<email.length; i++)     " o# p/ H' Z6 K9 O+ s7 Y8 S
  {     
1 ?3 B/ \8 F- A3 _  if ((email.charAt(i) == "@") & (email.length > 5))     ! V/ K7 x* _6 ?, m5 y8 W; f* C4 n
   {     # A& e/ _9 C4 h5 C
     emailerr=emailerr+1     
6 t! U) f/ B% t7 i, z$ o$ r   }     
) @% w; G/ ?% l% h4 R" e, e- R. h  }     ; }6 X' T! Z7 q5 H" E7 `/ N9 I
if (emailerr != 1)     ( g7 D0 k  t' T& E0 v: n8 t
  {     ) M, B! C7 \2 J, a" r3 Q" s& Z
  alert("请输入正确的E-MAIL地址");     
5 a1 R! I& H3 o! G  document.sform1.email.focus();     
" \: e' `- j0 b  P; ]  return false;     # Z) o" y  ?1 H: ~: |( w) k
  }           
3 X2 e! Z! W( Z. j! b) H  
3 j) i  B/ s: M& ~, Z% n if (document.sform1.checkask.value=="") ! R: ^, c, M2 q+ P
  {
& @) R1 @* x# L7 k4 {   alert("密码提示问题不能为空");     & o) @+ h$ p/ I1 I  u1 K' z1 m7 \
   document.sform1.checkask.focus();     
9 G4 T' A. Z( ~# ^0 N   return false;  $ H! m4 b  |# ~* V+ X/ A
  }
8 Q5 E+ ^. k' Y  \# c4 n+ l- p if (document.sform1.checkans.value=="") ! a$ ]4 E# M" _3 t  v9 ^) e
  {
7 r  R9 D4 U. i; h% i, x   alert("您的密码提示问题答案不能为空"); 8 Q9 i! H6 I( Y$ F
   document.sform1.checkans.focus();     
$ g* p2 |+ I9 r, L$ k   return false;  
) Y. G& J" j5 C- \, |: b  }
8 [4 f( b. U3 }1 n+ d7 A return true;     1 T5 D) T; u6 H6 q% \" [4 M$ s
     * v) G- k7 [( Y
}
' W2 r; _8 l8 |2 y7 ]3 C</script>

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