返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
' L+ X& r- t- d4 \<script language="javascript">     # R6 v  |& W& Z# L& T3 W
function IsDigit(cCheck)     
7 `/ y. n$ k2 d) @ {     : n8 ~" j9 q' ~4 A
return (('0'<=cCheck) && (cCheck<='9'));     % D# m) X5 Z4 G& s$ D# A
}     
: m4 H# g2 {, r3 T% j: U     
9 ?5 a7 L5 w3 m* h4 Ofunction IsAlpha(cCheck)     
! u# V, q0 c" ?2 s {     1 G! C& R3 R: [3 q0 W; M
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     % x; |5 r7 V- G4 s3 }5 @7 E
}              4 Y. J/ F/ H( F# z# K; b& _: \
      
- z) W( ]9 q: m) s  `" {4 }" ^3 Cfunction IsaNull(cCheck)          " z1 s: {* d' J' G) g
{          ) f' s2 i; K% J3 ^: @
return(cCheck != " ")         
3 r8 L9 @7 t- C }                               1 |: n, o" e& `8 E& h+ o( c$ r& N  ?! H
      
8 A0 C- \# M3 t( rfunction checkform()     & j; I6 V& c9 _5 O0 h5 b5 i
{
6 R) Q2 E6 _7 E8 u. ]  id = document.sform1.id.value;     * M4 o2 I4 e6 G& P
if (id == "")     
" Y6 H: v9 P3 o. N2 W' ^6 ^  {     & B$ a, l& F% f/ _3 X9 S7 ]! ]
  alert("请输入注册名");     - g# r7 T# l; E6 |+ ]8 b. S( \
  document.sform1.id.focus();     
, y& e3 a: u/ C" j0 y% D1 ~  return false;     5 M0 w4 a' O& e, J% a+ y8 B5 |
  }     
: `9 I# q3 X3 ?7 _8 O3 \( T( l     
  h( N5 I# O! E for (nIndex=0; nIndex<id.length; nIndex++)     ' _$ X3 J& A% [2 V  g; X! c0 K
  {     
: V' d* ^' S: c& y6 g7 a7 T$ b  cCheck = id.charAt(nIndex);       c) z, v$ f/ Q' J/ U! [8 ]( q( c
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
" E  H5 ^6 W7 @+ h5 H& m   {     
. D8 B+ ~  d% n! t! A( Q( S   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
4 A2 t- R: q6 R5 v) v  {   document.sform1.id.focus();     7 f" q5 O) O1 Q
   return false;     4 i; u; q" c* _
   }     
* G% ~0 H( ]3 N  } , j. f, |/ I5 K( Q/ \% v) b
  chineseid = document.sform1.chineseid.value;     8 I% {( t: s3 W0 h, U8 J
if (chineseid == "")     7 O; m. m4 h8 {  G7 `2 z
  {     - e1 g; P0 J5 E0 o. ]9 m% w
  alert("请输入中文昵称");     
9 ^4 C# M0 L! v6 C0 }" m  document.sform1.chineseid.focus();     
2 C) {8 n( ?) i0 a$ F& p$ q4 ^  return false;     / J' \& |' f- T" D/ Q$ {
  }    $ c9 A' o+ N$ O/ L6 Q* n' V; O# W
password = document.sform1.password.value;     $ p! s" Y+ C& O/ t
if (password == "")     
, T. i* P$ q: v  {     $ S: L2 v7 v' j* A6 M3 ~
  alert("请输入登陆密码");     ! z& [9 d9 K" }
  document.sform1.password.focus();     0 [/ V7 J. w  F6 t
  return false;     - @  O. S. |! R9 O+ W# ?7 S
  }
; W# N9 ]) H. F password1 = document.sform1.password1.value;     5 U, v+ H/ a8 {( C* n) ^
if (password>password1)     7 b7 M  i6 Q0 N# j8 g5 A
  {/ F2 i6 {9 \. F7 X. J
     alert("重复密码与登陆密码不相同");     
! i  i2 a+ {7 R1 P" m5 A  document.sform1.password.focus();
7 E- F- L1 C2 f5 ], r! S                                document.sform1.password1.focus();     
* v- s+ f  l! @7 W  return false;
1 @. X) V1 z2 ]/ w- m; f. f# @; V  }  . t, n4 j; @& Z7 p# e
if (password<password1)     ' j* Q0 j7 B! V0 O
  {
: n' y- W$ k5 z$ T; Q4 d, R! J     alert("重复密码与登陆密码不相同");     
2 y8 v7 _1 R3 j: R# m6 ~- W# H/ d  document.sform1.password.focus();
( B1 P4 x4 B! }$ R  F0 t8 V                                document.sform1.password1.focus();     : n9 W4 F. `3 \. _
  return false;8 W' B+ P( Q8 t, Y+ J
  } ; z' U3 Q2 E: U# j
if (document.sform1.email.value == "")     3 u1 S0 q' r& y2 D
  {     
: p" r/ O- x! `5 ~- [% P# ~9 J( ]7 g$ X  alert("请输入您的E-MAIL地址");     5 Z# Y3 Q/ U: d$ [7 O' T* `( y
  document.sform1.email.focus();     
% x& J* i+ L% u, K' d& u  return false;     
' x) q) e: m5 O3 \) m- ~; @  }     3 U7 e/ d$ f2 _% ^5 A
      
9 Q& c% Z; u: o/ N6 k! S+ _ email=document.sform1.email.value;      + e/ k3 u1 s9 t  |  Q
emailerr=0     
9 P5 Y" D3 G. `, s for (i=0; i<email.length; i++)     $ U+ N( e1 u* V4 _
  {     
) g8 ]& t2 ?* k* W' M& t. s  if ((email.charAt(i) == "@") & (email.length > 5))     
$ ?% o, Z0 T$ }: ~   {     ! x' h/ {) i* L' S: n
     emailerr=emailerr+1     + G0 `8 ]$ z* f( b: J* I! a
   }     
& K. F5 B9 ^  T  }     # T0 s, p2 f- T5 ~( S1 D% S! L8 f
if (emailerr != 1)     / N7 F" a- |# B7 z3 ?
  {     4 |% l6 e% k# G. y3 `4 f/ m
  alert("请输入正确的E-MAIL地址");     $ h8 K2 h' n9 |4 X5 ?' F
  document.sform1.email.focus();     
  l6 ^/ c# Y- ^# I( f  return false;     
: o- @' K+ L: _# F% j  }           
) _# C  f8 {3 }- o  ' A( j1 u3 j3 N" U) F
if (document.sform1.checkask.value=="") 0 r  _+ S% v/ L/ l
  { . j9 {/ O# R1 n. e, ^
   alert("密码提示问题不能为空");     ( a; H$ I4 n5 o& a
   document.sform1.checkask.focus();     
! I* h6 ?& O3 c8 G; ~   return false;  ' I) s0 X- ~) i& a" e; N  z
  }
7 V( B7 X9 d* O! t- r: | if (document.sform1.checkans.value=="")
, F: X% \, M  }% G  {
4 i. \, r' s0 {9 S( P   alert("您的密码提示问题答案不能为空");
6 \! F/ B8 y9 j- h4 {. v   document.sform1.checkans.focus();     
" m( m4 {/ D& h8 F, ]   return false;  
+ L& I% d" E: u  }
) m, H4 w+ f- k0 c% F return true;     
2 _% t' @' a$ U" Q8 K2 [3 Q     " W* z! n; o5 l9 F
} $ \2 i. I& w: s0 C
</script>

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