返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:2 _( e) m$ n! w$ u* y4 i6 v- t
<script language="javascript">     
- V; M# D% W5 R. i# p% Lfunction IsDigit(cCheck)     1 N0 i: m/ J  d3 M0 r
{     
9 e# a/ k$ ~: {# {# b4 j return (('0'<=cCheck) && (cCheck<='9'));     
* O, ]# @  Y+ c! j) U }       \" c( u9 b0 ~$ C! ~
     , t; L% A& i/ b' K* e. n6 G+ {0 Q4 Y
function IsAlpha(cCheck)     * z0 I5 l3 \  g9 d( x
{     - M# D  U: V% ?5 }
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
6 U5 m- w$ ~/ z0 M }              
2 s% o0 b5 I* _      
9 h: q2 x% o+ A6 s; h* T, qfunction IsaNull(cCheck)          2 {9 Q$ n+ y. |$ o
{         
8 Z' i& L  k( }" }3 f' q' K return(cCheck != " ")           T% H) ^# \4 J2 ~" b' }
}                              
  o8 V% E: q# j, F: j' H      
. @/ ~0 c7 \* z' M# U2 N4 [function checkform()     2 h7 R. V% |3 W& f( c& T0 ]2 C9 ?6 S
{
6 S5 D1 X; {- e& o& w8 Q  id = document.sform1.id.value;     ! d( d& l, j9 R
if (id == "")     - O" o+ g7 a+ l# q1 }# H. v
  {     ( g; @; k4 p7 E; c  o! ?
  alert("请输入注册名");     + T9 n0 L, e$ s1 U
  document.sform1.id.focus();     9 _0 @/ E8 s5 {9 v5 x. h$ a
  return false;     
8 `! h& Q- M; }5 x  }     
7 d. K7 q% B8 F# u* C1 }# o     4 C8 m, a- \7 C0 p5 I0 {& Y
for (nIndex=0; nIndex<id.length; nIndex++)     
, j; V5 F0 k) o( t4 L: N  {     
- |- X: c1 M0 u& l7 U  cCheck = id.charAt(nIndex);     . F+ l4 |/ r5 @2 `8 t
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
5 F- A1 g  W6 D( D1 E   {     4 {) j& z. N/ @. |0 F" p
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ; J! k$ D; X  A! s2 i& o
   document.sform1.id.focus();     $ i/ l, {% q% E  ~# n
   return false;     
$ A- N" r( z, t" V9 Q7 R   }     
' g+ C- [( O( s; ?6 f; l' k  } 3 L/ I( u. z/ ?  p; p
  chineseid = document.sform1.chineseid.value;     
! B  E. u/ t' L if (chineseid == "")     % J6 h8 v3 X8 _! y$ A1 `
  {     
! s( w: ~! ]) G& Y6 v' {. |. x; ^( Y; @  alert("请输入中文昵称");     
  v% t4 a( v' v4 }  document.sform1.chineseid.focus();     
9 d  r6 F2 v( q3 ?  return false;     
3 ]  o: Q" r1 _  }    7 G  U; S/ |: s( {7 h$ n) K
password = document.sform1.password.value;     0 w: }+ A/ G9 v- w# t
if (password == "")     ) P. f4 X7 {+ ~) C1 Z* a/ m8 H
  {     
  B5 d. N1 q5 z2 i: ~! G6 ~" X  alert("请输入登陆密码");     
: ?5 h  S7 M& c2 ~' d  document.sform1.password.focus();     
8 `+ U0 B' K$ @6 _3 J  return false;     
9 W" X$ V! W# ]" D  } & r+ ^8 A9 q4 P9 J& X
password1 = document.sform1.password1.value;     
1 b. p% S  ^( y if (password>password1)     2 L) z( z* o4 f$ y3 o9 K8 s- M( @: N
  {
5 l6 x# P3 k) Z! f0 ], ?- F1 l4 g: w6 [     alert("重复密码与登陆密码不相同");     
+ q2 ?" x8 l$ \  document.sform1.password.focus(); ( N+ S4 [2 }0 N; T* d! j4 a1 K- A
                                document.sform1.password1.focus();     
6 I) C( `, m( ]% Z: H  return false;
+ W4 m) E1 x2 W$ s/ Y  }  
: C2 F' E' F) e* Qif (password<password1)     ; [2 Z% ~! F- q
  {4 R3 q; g9 E- Z- ?) N2 H
     alert("重复密码与登陆密码不相同");     + q. g7 ]; E& ]
  document.sform1.password.focus();
+ i- n3 B) r" t' R& S. P                                document.sform1.password1.focus();     6 G" E( n$ g  ]: O
  return false;2 l2 w; q  w, r1 j$ j6 c, w
  }
- d  C, D* _0 x if (document.sform1.email.value == "")     " _* _' K+ }2 ^) R7 N
  {     & y+ h1 F/ J% P" o
  alert("请输入您的E-MAIL地址");     1 z0 r0 \) x( G- u) L
  document.sform1.email.focus();     
3 J4 c$ a& }" z" C! {% d  return false;     ; O7 O/ Q3 G3 m; I# R0 U# H
  }     2 s2 V1 U3 ^5 P% {; i# R
      # P- ~2 k7 c7 S& [& X, O- |
email=document.sform1.email.value;      ; |; z2 C1 o: n2 n! n* X
emailerr=0     ! k6 a! R( |. v, g3 E9 |' X
for (i=0; i<email.length; i++)     
: P; q- {; m; d$ p9 x7 S( \' j  {     - Z: F2 x% _; m4 b7 g
  if ((email.charAt(i) == "@") & (email.length > 5))     
% w& N! C: J8 c7 d- d   {     ) B) W" w0 A" |* J4 o. b' L
     emailerr=emailerr+1     
0 x6 ]  ]+ X5 D2 o   }     
* }7 s1 D0 f, O& Q# `, h/ o  }     : l: n  v, l# [1 z0 I) _3 T
if (emailerr != 1)     
5 r0 t4 e$ G  ?" p" c3 a  {     
4 ?7 O  x& d0 P* `) n1 S  alert("请输入正确的E-MAIL地址");     
7 q# D0 E* x- i: `( q+ ^, L  document.sform1.email.focus();     
2 H7 @" ?* O! l5 C/ u; @  return false;     ( i' t8 `, H  q. `9 r7 b
  }           . p" [9 ]. c0 W  O" W0 ^/ Z) u- |: w6 ^
  , b' d2 y2 v$ M- e0 b) F
if (document.sform1.checkask.value=="")
5 y$ ~7 h0 X" X# Q: ]0 m  { 3 G+ _: c- Y& J% ]: P  R/ E  m: C
   alert("密码提示问题不能为空");     
" R+ X2 F0 e& V) f3 j   document.sform1.checkask.focus();     
3 a: o2 ?4 e" n8 s0 |4 j   return false;  9 p, p+ d( }9 I5 t8 E; w& G
  } * Q9 u8 ]  J; G! s
if (document.sform1.checkans.value=="") ; ~  }% k5 ]  Q# a2 I1 ?7 b
  {
) {3 D: R$ ], {8 r# c+ U   alert("您的密码提示问题答案不能为空"); # x* H! L  H& ~: J: R
   document.sform1.checkans.focus();     * g$ B6 u  I( N: H3 n9 a& o
   return false;  5 n5 i5 K7 N2 ~  j8 ]
  }- N4 O- i7 ^/ j. S) f
return true;     
3 H$ [5 P. S7 `6 ^: t+ t- L       O/ h* z! V$ F2 B
} 8 U" O* H6 H* D) |5 u
</script>

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