返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:0 D( }9 e2 n0 M" R2 f
<script language="javascript">     
' w( y: e& T- T- M$ Wfunction IsDigit(cCheck)     * q! f' F4 l3 c+ n/ G" J. V
{     . X+ V2 p. [" b/ C, i$ M( {
return (('0'<=cCheck) && (cCheck<='9'));     ; j/ U$ {( o$ i7 T, z4 V
}     
- o% Z+ }6 x& k7 X& `     * B2 {9 Y0 }" F! k
function IsAlpha(cCheck)     6 X( l2 s, B& o3 [: C
{     : o, K9 Y3 a. _/ M, Q
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     # _9 h6 S7 @  O" @: j8 C
}              ! ]$ q3 ]* n# e- q
      - r7 k% l" G' v9 `- d: a7 E) h; b
function IsaNull(cCheck)         
; z. ^- A: g; \2 ?3 ^& v {          4 x8 l. H2 K% @  Y& o# w( C1 k
return(cCheck != " ")         
. i- I# F) h( D9 i }                              
1 A: H8 D# k1 m4 n      
4 v& Q  W3 w( C  C: [. [function checkform()     
6 x( U0 F0 f4 q{
; r3 @+ B1 d3 W9 H  id = document.sform1.id.value;     
& N$ W" J6 z+ ]4 W) K% ]) D if (id == "")     ! C' \' i* o2 q- n, Y; ~) r3 g! M4 z  ?
  {       U$ ~% A+ L5 h; u
  alert("请输入注册名");     
0 r$ h" K2 i) o& X) j  document.sform1.id.focus();     - i/ @& H7 W4 X1 m' O
  return false;     
9 T2 P5 @1 I4 e& p! b4 a/ \3 m; w/ y  }     
2 z, c9 b6 m. X8 R     
; ~6 Q: ^. D% }3 Z: K8 M. D for (nIndex=0; nIndex<id.length; nIndex++)     & H/ R3 Y0 m/ X
  {     
) ?, v+ H7 ~/ t( @- e$ ~  cCheck = id.charAt(nIndex);     4 q# ^. A  _) Q' o
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     # R/ }5 P. C- i! N
   {     * c' L+ O1 D- D/ `1 J
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ) L5 b* ]6 Z6 y( G+ H) p$ a- `- c
   document.sform1.id.focus();     $ u, k& D9 f$ X: W; X1 z. l
   return false;     
5 k$ U2 ?( G8 B6 ~, c# A   }     $ P3 b' i5 I5 ?0 r, C9 P
  }
/ N5 R2 a/ x6 k7 H; y4 Z  chineseid = document.sform1.chineseid.value;     
6 ~+ [/ }5 D% w8 i6 |+ R* o, f if (chineseid == "")     
# y# K0 }& T* s  {     / b6 v7 O( f$ A+ P0 F8 E
  alert("请输入中文昵称");     4 A8 K/ H# b5 b: b5 V0 I' ]
  document.sform1.chineseid.focus();     9 L! x( j7 x$ f) L" T
  return false;     
7 a$ g5 e$ @4 K) m5 Y6 W( L  }    8 A5 C! O! \! }( D5 L+ d3 ~
password = document.sform1.password.value;     
. g. q( [2 b5 g9 v" I. C, @1 A, t if (password == "")     9 F7 k2 C( Z1 l
  {     
7 C& F8 i& S: _/ f3 h  _& i  g6 e' k8 Q  alert("请输入登陆密码");     
$ w# Y0 J) Z1 d! s1 M6 j# g  document.sform1.password.focus();     3 P, L5 @+ X5 D7 c& G# F' I
  return false;     + a- a% g# a2 U9 w
  } 6 y" g+ Y6 \$ @
password1 = document.sform1.password1.value;     
. \2 @5 X( @& z( a* h: l( @2 c if (password>password1)     
3 j* c' f. F' c  {( y" \& A6 s2 t' @9 F8 ?
     alert("重复密码与登陆密码不相同");     + y2 j4 }) L) D
  document.sform1.password.focus(); . F- ^0 R: e3 p) V8 k; _
                                document.sform1.password1.focus();     ; u  b: W9 p2 W9 l: V' f% C
  return false;
9 Y; r$ D5 n5 `# z- {9 s* h  }  
5 z( l' q! t4 P) V6 cif (password<password1)     
+ f; c, L3 ?! w. a# J  {" X# O/ A# ?6 g+ B
     alert("重复密码与登陆密码不相同");     
/ ^$ c8 W2 {; a5 b- X  document.sform1.password.focus();
3 W* W* W7 k* u9 k1 o' T9 [5 Y                                document.sform1.password1.focus();     8 }# _7 [  x- s3 I$ Z" N& z* C
  return false;% |  `- M  R! ?, c
  }
' N3 J. [3 x" G7 d. w& P) ~ if (document.sform1.email.value == "")     
. q/ g' e! P+ C# D3 Y4 a8 A  {     
5 K# n' i# |% e1 p2 l  alert("请输入您的E-MAIL地址");     
! H, A; U5 ~! E7 J  document.sform1.email.focus();     
/ \6 z' z# n, J9 [5 h* p  return false;     ( X0 e$ j) u; U$ b8 F
  }     
) W! p9 r! ^* H1 [/ d      $ w: \  X9 s+ U: S, B
email=document.sform1.email.value;      
9 u; y; ~! I4 o3 U emailerr=0     & _/ W1 h, t) Z
for (i=0; i<email.length; i++)     . `* [+ R! o$ F7 s% b
  {     # A/ @6 d: S# O% Q/ b
  if ((email.charAt(i) == "@") & (email.length > 5))     : J/ Y! ]* |& W
   {     + z, y4 Q4 w% n+ Q
     emailerr=emailerr+1     
& z+ V/ c/ w$ X/ {4 n$ P   }     ' K( Y+ |! \" W( J! ?
  }     & Z6 {' w5 ^7 d% q% i" y
if (emailerr != 1)     + e5 k( r" v/ c
  {     
: I: Y9 m* i' N+ ?, C; Y! q  alert("请输入正确的E-MAIL地址");     
& |) x  M+ u, t  document.sform1.email.focus();     
3 F+ _: \! c! ~- l* J8 G+ y  return false;     2 V( [0 ]/ N' z: G4 k
  }           
% [( O  }5 n0 W# B, _  3 u- f6 W" m! a1 L9 E
if (document.sform1.checkask.value=="")
" Q' Z7 f" @0 U" f  {
- j$ d9 Q+ `+ f8 v   alert("密码提示问题不能为空");     8 N! ?! |% [2 `" G: C
   document.sform1.checkask.focus();     ) A, P- o: x6 ^; q; T% \5 F) ?
   return false;  
; P1 d& g6 M# L9 o2 {* j  }
; b% I) D0 Z1 y' _; m. u if (document.sform1.checkans.value=="") 4 \& M! }8 I* W! N2 {
  { $ a- l  W2 ^9 m7 |6 ?" y
   alert("您的密码提示问题答案不能为空"); " `8 `6 B, r# C: h+ L8 P+ V0 w
   document.sform1.checkans.focus();     
( N2 C3 @8 @3 f" \7 o; H   return false;  8 ^& L5 `# H5 U. p
  }
) K& d4 P$ p( @' N6 i return true;     2 m# @2 d: k" X8 k& J; S3 b& s9 S2 @
     0 G+ u& ?. w/ {; G4 W9 g0 H
}
$ I' R- X' L/ \9 X; \9 c) P</script>

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