返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
: p: B" e2 S6 M/ Z<script language="javascript">     $ a( d! j1 x$ D! @
function IsDigit(cCheck)     9 H) j" R- S6 Q9 U3 l/ F- O8 ^( C5 t
{     
2 Y* l& s. S( a1 R  w return (('0'<=cCheck) && (cCheck<='9'));     * }( m( o3 _7 U
}     3 h3 O8 C( B  q  v$ q
     & ~4 ~: M7 Y# ^* L! I0 o. l/ R
function IsAlpha(cCheck)     : @( `5 A# n+ x
{     5 v0 t6 t7 F2 m- H' t; r1 g1 s8 z1 W
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
9 E+ h$ E$ Q1 h$ ]; h }              
' Z7 V  e$ C, K      - E3 }4 R( ], V2 g# |
function IsaNull(cCheck)         
8 {6 F' \- Z4 F {          5 N- I/ p" m* a! K% c6 r: j
return(cCheck != " ")         
9 k4 F+ @* D* `. M: A }                              
+ V. Y" ^, \2 e- h' Z: k6 N. Y2 ]      ( T" G8 O# V/ O2 @$ r
function checkform()       m4 ~5 N! {) t6 U4 B
{ & c9 g' {4 J/ Y6 |4 Z
  id = document.sform1.id.value;     
$ ^% j1 }7 y; y# g  O5 y6 T if (id == "")     
& K$ X$ o& O0 \# J" T) t" T  U  {     - G7 A- Q$ D7 g4 y' c3 z" {0 g
  alert("请输入注册名");     
: [+ z, [( r4 w) l" [- p) q  document.sform1.id.focus();     4 X. X9 @& @! K' G+ j# D9 @5 J
  return false;     
$ q6 q! l2 n' `1 H' q) O  }     
% N4 r- r6 ?! m6 S, A; ?* Z     
; N5 K( U: r2 u% k9 g# L for (nIndex=0; nIndex<id.length; nIndex++)     
. i0 s' I/ P1 }# K# b# w  {     
+ e2 O4 s4 j8 r+ J  cCheck = id.charAt(nIndex);     2 s$ H: R! }6 n* b
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
! P# K" k) _1 O  l* T# M# \' p+ h   {     " M9 i$ \, m! D- G* ~( [
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     4 G8 y; m) |3 f1 ?7 r6 W6 b
   document.sform1.id.focus();     
* `1 ^2 o, C$ p7 F   return false;     
0 `6 a8 G0 n/ ?: C8 B$ V& y# j   }     
( X  ^- _# S# P# w7 Q  } / A. H# q$ _; G1 j1 h# h6 ]3 |
  chineseid = document.sform1.chineseid.value;     
5 M' C; x" y6 A9 }; u if (chineseid == "")     
6 i) u  ~" K7 l' n9 Q0 C: j) b4 z  {     # R0 D- f1 l5 f
  alert("请输入中文昵称");     
  R/ U5 k7 u  r: r8 [  document.sform1.chineseid.focus();     
/ l. {( o9 B* ]& ?  return false;     
7 J$ G& ]4 {9 Q2 _# p" t  }    ) }7 a! f3 h& C  z
password = document.sform1.password.value;     
5 w5 D% R" D2 U* ? if (password == "")     ( U) s/ g% ~% o: u8 Q, V
  {     
; l& k! f" z7 F7 g% q  alert("请输入登陆密码");     - w& F$ d8 I/ q# u
  document.sform1.password.focus();     ! V; G* @+ j6 b' P
  return false;     % X! ^  B  t4 f0 w2 N' J
  } & y6 m4 f7 F: @- \2 U
password1 = document.sform1.password1.value;     # h7 I. s! ~2 W" o& R$ l
if (password>password1)     
$ n/ |6 ]- @, s, \  {
7 |9 n. t4 P; D0 n' j     alert("重复密码与登陆密码不相同");     
- q2 t+ N  I8 ?6 v  document.sform1.password.focus();
7 ]0 u3 `) C8 h                                document.sform1.password1.focus();     
. C( L7 j3 O( o  `4 l; b0 f) N: D  return false;
( D; d( Y% Y6 ^$ [* J5 A  }  
1 g; ]8 x( }- dif (password<password1)     5 p: M8 a4 l5 i2 r8 _
  {" }: b4 n2 H: b  X
     alert("重复密码与登陆密码不相同");     
: H/ m7 H, @1 ^8 P7 g. o9 K  document.sform1.password.focus(); & G9 p, ]7 K( `2 B1 K# K
                                document.sform1.password1.focus();     
# g! D. M% O0 H; h3 X  return false;, d9 O) R8 X  o% F# n
  } * L* j2 S6 ?0 m4 ~! x9 h
if (document.sform1.email.value == "")     - E" P1 j* j. |, }6 v
  {     
  t0 {# P2 h6 S5 F! `1 D5 d6 d# l  alert("请输入您的E-MAIL地址");     + k9 d( T( ~. [+ Z
  document.sform1.email.focus();     
7 J# F& t3 c9 n# _: g' p  q  return false;     
! H9 E0 m$ \8 e% e& a8 h& f  }     
' g. [3 |; y, Y) ^      4 @7 T" z& d$ r3 b  p" \
email=document.sform1.email.value;      $ b8 [( U* c# a* h$ Z2 U: s
emailerr=0     2 U7 V1 G3 T  p: Q, ^! C$ ^$ h3 j
for (i=0; i<email.length; i++)     
- |' s- k* E' h  {     
2 G7 t% [/ L6 {  N- M( R6 _' i; t  if ((email.charAt(i) == "@") & (email.length > 5))     7 e" ^0 f" i& m1 r% s
   {     
$ j0 s% J. t8 x4 ~' Q     emailerr=emailerr+1     
# E8 U1 n4 c9 w' i$ |. [   }     9 X  u* T* J$ v. v  O9 y  D7 T
  }     3 W% M- ?$ s! G) n1 f
if (emailerr != 1)     
4 h: G8 g$ H! F& r& F7 p  {     
" g( P. d0 f  C: h' @1 ?4 }  alert("请输入正确的E-MAIL地址");     
7 b# y! y* x  N" ~5 V1 F  document.sform1.email.focus();     , l  ]4 q9 U* Q# X
  return false;     + ~0 z, w) d( H! Z2 J: q3 x' H
  }           
0 Q/ a4 x, {5 `  ( c( }8 d) s5 }3 N
if (document.sform1.checkask.value=="")
( |& G9 C( F- t  { + ?" s6 K% U7 X/ y) ]  g
   alert("密码提示问题不能为空");     6 B+ j* l6 _3 t- M7 W! d
   document.sform1.checkask.focus();     # F/ m! T, X! k2 a! }
   return false;  2 m% l# S1 O: M6 X/ Q7 @
  }
  o2 z" `6 Z& X6 ] if (document.sform1.checkans.value=="") ' _: e- H  s0 _2 c0 ]( ^$ I
  { 1 ]3 \# i6 b2 m
   alert("您的密码提示问题答案不能为空"); : B8 x& k& ^' J) S" m
   document.sform1.checkans.focus();     9 i' Y+ B9 E, M$ D& l; M7 O
   return false;  - }& W; @2 P0 n2 |
  }
6 w% T4 p0 ^$ X% R9 V+ K return true;     $ h5 Q9 y) z* u. E9 Z* v
     
0 }. B# W  F/ ^} 6 @( \7 \6 `" y# u; z5 }$ j
</script>

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