返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:+ Z1 |2 [! j; u6 e
<script language="javascript">     : X/ Y5 q5 g6 @1 ]% s, k
function IsDigit(cCheck)     
+ `7 V+ S+ a( c) o3 h) x+ W {     , ~5 A, U! o+ _# [5 R5 t. A
return (('0'<=cCheck) && (cCheck<='9'));     
* Q% |" r4 z' w! B }     ' H: M" s* t  N& D4 ?
     / p1 e: I3 l8 Z& {8 e
function IsAlpha(cCheck)     
( U2 x4 m" H$ W# I* R7 }% O {     
2 o. w+ q! c$ b, w8 U3 i return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
9 D" D" L, k. T2 I. ? }              $ G" i$ @' g1 |+ o
      
$ G& F8 i; N+ t3 ~* h+ xfunction IsaNull(cCheck)          6 ]* o  j( S; ^
{         
9 }: {. \2 ~& h. a/ u. P4 n return(cCheck != " ")         
4 ^" m% ]: I% e4 \$ m3 G% n }                              
2 e2 s& }7 Q) K* |) h      * n; T" _1 o5 Y# R, c
function checkform()     
4 y8 _) l. ^# y/ H{ 0 `- w& |8 _" c! i2 \7 a2 Y: d
  id = document.sform1.id.value;     
7 P8 h) J: S3 } if (id == "")     
2 `& D3 E: W. p" C2 O+ o  {     
! T, h9 T, }; m$ }9 x; i$ i  alert("请输入注册名");     
9 N6 n$ B4 ^1 s) l2 ]$ L  document.sform1.id.focus();     5 D% I5 u1 n: N; Z; L8 e) W
  return false;     % B9 k% _1 f, @3 n+ ?
  }     
3 m: k: @/ `+ S+ E3 M, W     
: H7 D0 V% t( M5 g; F& j: o for (nIndex=0; nIndex<id.length; nIndex++)     # ^, M! L5 o+ U5 x( b( c
  {     
1 l4 w  J. p2 d$ L/ a% i+ o  cCheck = id.charAt(nIndex);     6 {& O3 c% Q6 O
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
6 f5 h9 ]6 L2 _# r- ~+ J   {     
4 a, }# ^2 H6 Z8 q1 Y  r8 N" b3 x   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     * ~, d3 _- H$ P  w: ~0 F7 b3 `0 P
   document.sform1.id.focus();     ' G2 `& C% B! Q
   return false;     2 L* R5 t$ i1 f/ w, K
   }     
/ y, I1 x6 K1 @' q' _8 {! U  }
( x! i" L9 k, `' P  ~  chineseid = document.sform1.chineseid.value;     8 V* Y5 d# u+ A) |" Q
if (chineseid == "")     3 C0 F6 `  m7 a5 `7 ]8 e4 g
  {     1 N$ f; Q$ R! @/ Q- C
  alert("请输入中文昵称");     
: Y; k& W& X% n2 G; ^  document.sform1.chineseid.focus();     9 j& P) q) o4 H& i% ^* X- Q
  return false;     0 A+ L5 T( r# s& f* p8 C  ?& n
  }   
  j: C  z. k( _8 h6 H0 E password = document.sform1.password.value;     7 H% \6 q3 O( b3 Z" X/ n5 S7 }
if (password == "")     + C/ H$ B3 r8 u# z: K1 Q$ [
  {     / d! \! C; ]8 N# x9 }
  alert("请输入登陆密码");     
8 X4 `; y9 \( @* B9 x  document.sform1.password.focus();     2 e4 O4 `9 f8 }+ E7 R' v; ~9 ~5 F
  return false;     
2 ]# y1 i; h/ W9 g/ h. ]0 t; t  } 0 c4 O8 J" A) W- A
password1 = document.sform1.password1.value;     
9 O4 T5 G7 l+ \' d0 J3 F: E7 F if (password>password1)     / O' H0 o6 h" G' \
  {/ \, P2 W0 c! o
     alert("重复密码与登陆密码不相同");     
! d3 |! F; ^1 j) s  d  document.sform1.password.focus(); 1 D- h/ o' Q' i- l
                                document.sform1.password1.focus();     6 V$ J- I8 f/ i% N
  return false;
0 F4 O3 C" H! n+ c  }  - J0 x2 ~" z# F4 L8 U. N
if (password<password1)     ) Y' D9 ~( I& l8 R$ @: \- ?
  {8 ^  T  S# @1 j/ s" J
     alert("重复密码与登陆密码不相同");     1 C' J8 U- `& \/ K% B" w
  document.sform1.password.focus(); ) J6 @& ~& y4 r
                                document.sform1.password1.focus();     
) d4 Q/ S. a. q  return false;' E3 a3 d! Q& ]0 S6 |: t, t- }
  }
% F# ~1 m6 F7 _9 S; C% j if (document.sform1.email.value == "")     5 A( O, {: Q0 v6 f# M0 I, N
  {     2 V, m* o: i+ k( c
  alert("请输入您的E-MAIL地址");     
1 [" E/ ^( O: c* q  document.sform1.email.focus();     
: p/ A" }/ A4 {5 `0 d) u# T  return false;     4 D9 V3 c$ l& k+ `. L
  }     & @: X4 @6 d' e! C4 C' v0 l9 s
      ' U# w7 h3 }, l) j" S( b- Z" Z
email=document.sform1.email.value;        W* n. k/ \# _
emailerr=0     1 d9 b* Z  @+ G8 J" D' @# M
for (i=0; i<email.length; i++)     
) S* t9 M& r$ g+ V1 n8 n  {     2 C: x' g. h6 P# n8 y
  if ((email.charAt(i) == "@") & (email.length > 5))     + q& S2 ^3 b9 b" v: w1 B
   {     
2 L. X+ b! c# o, \8 E     emailerr=emailerr+1     
! O& h( y# w3 V) k0 @$ O9 ~1 X( c   }     3 t8 U  q, n) h, F, `
  }     
) v3 @3 e8 w2 q0 {; E- U! D, @ if (emailerr != 1)     5 [" C7 `! s& m
  {     ; _+ S  y( c6 J' d
  alert("请输入正确的E-MAIL地址");     & N7 f/ W( j& R* X, x* p5 i$ z
  document.sform1.email.focus();     . Z1 o" V+ A/ \; ]* g: U5 v) p& D* u; r
  return false;     7 h1 v" N2 u( l3 W3 s. l
  }           
2 g6 e4 R0 h& R  % D8 x% I$ X  `3 h" ]
if (document.sform1.checkask.value=="")
7 A1 L7 S+ f3 n3 n4 V  { , t6 J! `! S! m9 ?, w6 R
   alert("密码提示问题不能为空");     4 M$ H' b( c2 v/ {
   document.sform1.checkask.focus();     
8 q/ T7 O/ c7 v, A   return false;  
, c) m# B5 [1 G1 R' |9 u; X6 A  } 3 t8 s# W) `) e/ ]. g: x* ^0 O$ e( y
if (document.sform1.checkans.value=="")
" z- ?, O) `2 q6 H' x, J4 k) g4 }  { ; ?4 a( p; L- Z+ u
   alert("您的密码提示问题答案不能为空");
7 q9 p. c$ p% q  U0 [0 K   document.sform1.checkans.focus();     
: b( \1 k1 V: w* d" u$ @# }+ L   return false;  % }+ V0 {& E" \8 l
  }
/ d* i( o- k. o! f5 b return true;     
5 [( P* ^9 j5 v: s/ E     
/ e+ Y  U3 t3 H: e8 u5 x* q8 b% D: j}
3 }: ^) D( ~2 o  G</script>

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