返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
0 M- ]+ i: S( a6 l6 U2 L- w( l<script language="javascript">     
5 M8 M( C, d; t* P4 o1 i$ z1 ufunction IsDigit(cCheck)     
; j0 l( P/ k. w: c* ?: h {     % H( b, w. o6 d9 l- N% u+ q
return (('0'<=cCheck) && (cCheck<='9'));     & a2 Y" _, H% u( M9 V3 L" R1 `4 p
}     & I: W( ~- ~# I5 Y! g8 C
     
, }+ }8 M0 V3 _; A& v9 n" B9 e) nfunction IsAlpha(cCheck)     5 S! b. Y. [3 A7 _$ P/ Y; ~1 Z0 |
{     : ^# H: ]7 d% r1 r
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     ( V6 y; e& C7 h+ e0 G. c
}              6 b$ J: E! e% c, T. v3 ]# S
      % l) k1 Z. b  ~: P* `
function IsaNull(cCheck)          , Y) B. N3 n" Y( V" @
{         
3 ~0 l1 i- Q9 U/ a2 a3 j' }2 f/ H return(cCheck != " ")         
1 z! i- E' I& A7 q. D: @& z; x }                              
0 y# Y( x0 C4 Y! p      
! i+ Q4 l9 S- o" |function checkform()     % Z' W- m4 R% I3 Z: B$ F
{
) o  H% `1 w6 ]/ K6 G  id = document.sform1.id.value;     * q1 b1 Y$ f  L) `7 q% j- n
if (id == "")     7 z0 ~7 O& C+ d9 C' h
  {     9 g7 m* E  n% y0 D$ e4 O1 a* O
  alert("请输入注册名");     1 a# r  C; v3 J- z
  document.sform1.id.focus();     
1 Y& f% p% X0 s  return false;     
' Y( G, o' `5 X$ R2 @+ N  }     6 K" P; l2 G: y  W. O' b
     
) R2 r( O5 ~) v+ k5 U. E for (nIndex=0; nIndex<id.length; nIndex++)     
1 _0 z1 |) k+ E# ]# X  u# R: }  {     
8 f) _6 z% p" S% u" m  cCheck = id.charAt(nIndex);     / \3 n* Z. o) {; x2 d- A% m
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
% I. C- j/ {/ Z. c   {     . }# W3 A  T4 e0 e) w" H! r% n4 p
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
! m  Q: w2 E9 B/ g! U" D1 \9 ~  N   document.sform1.id.focus();     
) h" n  ^4 J0 x" y  F   return false;     2 H  N$ H7 A9 _/ u1 T% J
   }     
2 V! J) v7 ?9 _/ K3 {" V- D  s  }
/ K7 ^8 ~1 n' _3 I5 {1 Y+ S  chineseid = document.sform1.chineseid.value;     
' @. R4 S' d- w& U8 b if (chineseid == "")     ) [7 M( Q" A! ~3 H0 n
  {     9 f, g8 c( m$ `
  alert("请输入中文昵称");     8 ^/ l8 g3 C8 H. X- \  O- m& \2 \
  document.sform1.chineseid.focus();     
- r$ c- x! g  h( [  return false;     # d7 ~5 K" z+ \+ ^  K* `
  }   
7 \5 p( P, o. C password = document.sform1.password.value;     ) z/ R4 \- {" |6 n+ N
if (password == "")       u7 u/ I) N% h! r
  {     
' A5 w* t5 ]) s, ]3 x  alert("请输入登陆密码");     
1 d- E2 t6 L4 z  document.sform1.password.focus();     8 z! ]- T% T* s" p
  return false;     
; k4 @* H3 A9 \' U: o2 U* H  }
7 N6 L9 `! }. l. o7 a  m password1 = document.sform1.password1.value;     2 z( U* v, V& c4 R, V& \
if (password>password1)     
" f3 H" {- T5 A5 S  R  {' m9 z8 `7 f: D7 U, \5 ?, x; a
     alert("重复密码与登陆密码不相同");     
& s% k7 A3 U1 S' D2 d5 c  document.sform1.password.focus(); 3 F8 @- h& \  o' _' S0 k
                                document.sform1.password1.focus();     
* K  |3 B# J0 W  B, [  return false;
& |  _9 I6 e: Y! v) ?' W8 _  }  * U1 T* q* G# ?$ ]( b4 U9 g
if (password<password1)     
' ^, h3 v$ T: x4 E. k" P  {  O1 k  @% n0 v& a) a
     alert("重复密码与登陆密码不相同");     
# f, I; E* K% n1 _# {  document.sform1.password.focus(); 2 R1 Q# }# k/ s0 ]
                                document.sform1.password1.focus();     
2 ]4 Q  a% n: l4 \: i3 A% @5 `  return false;4 ]( f0 Q0 @/ i  l
  }
2 t" w0 @7 G8 ~& `  D if (document.sform1.email.value == "")     
, M( e2 s) M& e  P  {     
( W. w2 N4 I( g5 B2 X" \/ P  alert("请输入您的E-MAIL地址");     
1 [3 q, J6 f, y5 b) {2 Y# _  document.sform1.email.focus();     
4 w. g1 g2 B' t: [) B3 e  {  return false;     
* {; x* G  x9 V  v6 Z: b, U5 O  }     % h# A; |( x% x
      
1 q* c  B% W8 O( a% x. s( g: g email=document.sform1.email.value;      + y4 z2 t8 C# y9 _' s4 N+ {9 g
emailerr=0     
7 c  [: O( t, i' m" H for (i=0; i<email.length; i++)     $ R" e7 h: \4 {. s
  {     
* O. ]8 F$ y6 V5 Z0 F9 x/ D  if ((email.charAt(i) == "@") & (email.length > 5))     
0 ?, L8 O4 r+ L  ^   {     
. R- }4 Y! P) ~( k     emailerr=emailerr+1     ; `8 J9 F& k* O0 D
   }     ! E, W5 p8 C8 j# B9 H* B
  }     
2 Q4 Z" l6 y* c* W9 l5 A6 E if (emailerr != 1)     
7 F" `, S5 v( h& c  {     ' L( u0 V, |3 Y. H' l+ [  k  h. m
  alert("请输入正确的E-MAIL地址");     ; Z5 ?* x- N6 A$ Y+ J
  document.sform1.email.focus();     
( G' p# \, U7 E, i5 o5 X+ K3 M  return false;     ) W; H5 W% j6 j2 @/ \9 L% S
  }           
# f3 N3 h% \8 ?4 a0 ?- ?. M" V( ^  
! ]" d9 J- G: O7 ~/ E6 d if (document.sform1.checkask.value=="") ' O: E/ }6 P1 m5 y) `
  {
1 N1 e/ O/ l# p5 L7 I) E9 b   alert("密码提示问题不能为空");     3 C6 e* y" k( M
   document.sform1.checkask.focus();     # f2 p, H6 S( I  o
   return false;  % s' _( w3 _$ z6 T6 @. `
  } ) B0 O) {) ]! a6 I
if (document.sform1.checkans.value=="") % p  V0 I' e' g0 b" w. K# B" x5 Z
  {
8 m& d5 K) x2 ]! `! n. z; B   alert("您的密码提示问题答案不能为空");
& A/ T8 V  f" @/ ?7 j, h   document.sform1.checkans.focus();     
( [2 x; u- a& U   return false;  , Y7 W7 [7 S0 a& i! u- Y: j! ]2 e0 ?8 e
  }
; f3 S7 a" p: ~% \ return true;     
3 U/ Z. [8 e% L& N5 U1 J2 m     & y  n# Y; v- H& `& u5 x# c
}
# V8 H5 v3 v( k) t</script>

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