返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:( a- X3 l* j/ J  ^$ L
<script language="javascript">     
! y! j# R' t1 ]+ Kfunction IsDigit(cCheck)     
. e/ X# r! l* I {     
" c: f) e$ V+ `9 p! O7 @ return (('0'<=cCheck) && (cCheck<='9'));     
( [3 ~$ c. f0 M$ U) X }     ( E4 T. w2 D' Q6 s0 l& E" `
     
% l" z$ E# ?; ?' qfunction IsAlpha(cCheck)     + B& v& ?& N0 U4 ~7 b
{     
( l! s4 X. L9 p  r return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     6 q4 h! E/ v6 M) t, N* L
}              
+ _' L" |! O0 p" ?      * J1 H- E  @0 m' V
function IsaNull(cCheck)         
* i9 [) K7 E2 g6 U3 ~* k) }" Q7 I  R {         
9 }. A; j* W# t return(cCheck != " ")         4 n% V8 O; S/ C+ b! t8 ^( j- e: U
}                              
8 N+ t& U4 }1 g      
2 `# J' R( Y4 ?0 L+ S7 ?7 l- G$ y# yfunction checkform()     # z+ L$ i1 l2 k& W
{ 6 f- m4 ^. ?  U9 \# [
  id = document.sform1.id.value;     ) G# {% `2 J) P& p7 R
if (id == "")     
: L4 c. Q( g7 I4 Z% Q6 y( [( |  {     ' C" u1 s6 p6 i3 v7 k
  alert("请输入注册名");     7 M( m( _) L- c, a
  document.sform1.id.focus();     # D' {" L# U6 @- \
  return false;     1 I. W# Q& T8 t3 _) ~0 {  O
  }     
$ z) i# b/ o& v, p2 t     3 H" k4 o$ }/ }" p3 ?& z$ {) s
for (nIndex=0; nIndex<id.length; nIndex++)     ' L' t" i4 V# l0 J+ D
  {     ) ]1 b" N" C" F- K1 u7 N- |4 M8 B
  cCheck = id.charAt(nIndex);     
, w3 S8 W1 c8 |, f6 c  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     % w# b- E& K' o7 Y( L5 @
   {       p! I/ f/ e: W5 z: n
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     5 c: v$ H' Z0 w$ N
   document.sform1.id.focus();     : M% }# \7 U; j: e+ n, t
   return false;     : N- x% r5 R* ]; }4 F7 [; l
   }     0 z. m* \5 A: @# q  P  x
  } # T" Z# }% ?1 O9 P9 @
  chineseid = document.sform1.chineseid.value;     
4 L( n$ S2 h! X! b  c if (chineseid == "")     4 x; K  [; F: d' A# B
  {     0 c/ l# [' j6 j* w! ~3 v+ {2 M3 P
  alert("请输入中文昵称");     
6 i" _! H4 k6 x$ g- K+ a9 i2 R  document.sform1.chineseid.focus();     
+ t4 g$ N& h* M" [2 h& |" P) T/ @+ }  return false;     . E. N/ `$ O% e, W; W! b
  }   
$ N* U0 A. O9 G password = document.sform1.password.value;     
" t( p6 u: x: T: [7 ?( o if (password == "")     % R* ]" s6 r& R2 _9 S0 R2 E; K
  {     5 W9 m; S: O4 N) d. x0 c
  alert("请输入登陆密码");     # x4 H  W) {6 G0 R5 W6 k: ]( D
  document.sform1.password.focus();     
2 w) N" ^* ]& `( }0 B& Q. p; y. [  return false;     6 u& ]" p3 ~7 Y" ?* M
  }
" Y; e# s0 ?8 q password1 = document.sform1.password1.value;     
5 u) n8 [" m; \. c if (password>password1)     , N9 `7 a/ E) |2 l$ o( R
  {! u2 W6 Q. t$ h& O& k
     alert("重复密码与登陆密码不相同");     2 @8 p6 Y# Y5 d
  document.sform1.password.focus();
; A" [" v7 V$ p2 |! N                                document.sform1.password1.focus();     
4 r7 W' j* F0 L" E# |& j( O  return false;
/ `% L2 q) d* E' ~1 d  }  % t3 C3 @% P8 H0 s6 w; a# S
if (password<password1)     
4 D4 A/ t/ a+ u# m5 Y  {- Y0 j& C1 u2 h/ p' i
     alert("重复密码与登陆密码不相同");     
$ N' U; S) C+ w* C% K* }4 q7 x& ~  document.sform1.password.focus();
: Y9 z5 |) C" h$ ?3 J/ F9 P                                document.sform1.password1.focus();     
, w- Z' r. c7 d/ L, _5 t: |" V1 S  return false;* n- ~! e0 m: h
  }
3 u, K6 g$ \6 p9 J if (document.sform1.email.value == "")       R0 F( i5 U3 U) \9 `2 |
  {     
+ R) k! S1 I3 s; N5 k* X  alert("请输入您的E-MAIL地址");     & o; ~7 @, O: o4 J5 p
  document.sform1.email.focus();     $ \* l5 g4 ]$ _+ K
  return false;     - I" k- {3 X4 E" {
  }     
9 e! q6 c! a, M$ ?      
2 {8 L  @$ @! g/ s, n6 f+ R email=document.sform1.email.value;      
( f$ B4 d) F) Z: |- b! _! I6 I0 P& { emailerr=0     6 D0 q0 J0 p9 B2 t* D- h
for (i=0; i<email.length; i++)     
" }( Z7 G# X# k8 S, E3 q. }  {     
) A& L% Z& u1 W& @  E/ d' O6 }% P  if ((email.charAt(i) == "@") & (email.length > 5))     ( U, g: r& ~/ b8 f
   {     
7 [5 ^4 g) w/ N) _4 F) ~     emailerr=emailerr+1     
0 {9 s) }+ Z1 _5 s, a( A* h' E   }     - j' |' t8 l; e
  }     ) L" V* K, m/ c6 o/ T5 ?, l
if (emailerr != 1)     
7 Q& i% H/ Q0 s3 l$ O  {     - ?7 N# p7 L$ ?  p
  alert("请输入正确的E-MAIL地址");     
+ d, W7 `7 b( Y* a! g) W  document.sform1.email.focus();     
# r; V; Z# |$ ^" D1 m  return false;     
& ?  W7 y; R/ E; @7 L9 Q  }             `; I, d  V# w
  0 ~0 n7 x- M' o+ r/ y- l
if (document.sform1.checkask.value=="")
/ k8 `, y6 D$ Y  {
  v+ _- M- D9 ]% b; h   alert("密码提示问题不能为空");     " S8 Q! Q# I5 }/ u1 |1 G) ?  L
   document.sform1.checkask.focus();     % w, F- {; ]4 o2 M5 R- Z
   return false;  # e. |$ O1 ?6 V1 J# a  T# {2 E
  }
, d, M" l& w  d' U  ^& V if (document.sform1.checkans.value=="") 1 g) c' g$ T) u7 C1 J; ~
  {
6 {) i* _$ ]* W. F1 W   alert("您的密码提示问题答案不能为空");
( ]# C% g2 }: }' W   document.sform1.checkans.focus();     
2 v  Q  E+ _, I; Z2 x% k5 w   return false;  & z: g) W7 D, ~0 \" h4 t
  }
$ z4 y5 u8 a/ B7 Z return true;     
) W( D" X* l; `. o     ) q4 j5 ?4 k# m  v+ D7 E) U9 w
}
* y" Y+ I! m' p# {3 C</script>

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