返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
$ C0 B3 C7 h2 k- s. i<script language="javascript">     
5 w( @* H, e( rfunction IsDigit(cCheck)     
7 x  K/ L/ Z# N! S6 ?2 G. y/ c3 ] {     # `; k2 F. {, O! @
return (('0'<=cCheck) && (cCheck<='9'));     
+ S3 l& W8 T: h" V  F2 | }     
" R1 E5 h4 G0 ^, e     
9 }3 u. b9 J9 a. x2 Z* Lfunction IsAlpha(cCheck)     / o( E7 n/ }8 j. ]  E" z
{     
& M! X5 u5 Q. b$ m0 [0 X! T return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
- @% x1 ^: G0 t  d# ^8 ? }              1 T6 K+ v6 e  t
      ) ~  j, t( {* v1 a
function IsaNull(cCheck)         
3 c- r; P6 ~+ [; [1 g2 G8 V* U! O {         
; W& ]  b  J' e) x return(cCheck != " ")         ) L9 v; k: p7 Z; t3 O/ u6 G! r
}                              
& w3 f; T7 i) ^! X      1 \1 N5 }# u1 e, P% n. a9 K7 s
function checkform()     " i# r1 K: H0 ?
{ 2 \2 N2 F$ `! M, X- C
  id = document.sform1.id.value;     
: h1 R8 X2 K: n( S7 v if (id == "")     
- m+ A2 \0 X1 m4 X: _% T; @  {     
5 A$ h' T* k' r& {+ _+ Z  p  alert("请输入注册名");     
7 e8 B, g, p/ K/ t5 g8 E  document.sform1.id.focus();     ! A' o, k' w5 u0 Q$ ?; a4 L+ O
  return false;     
" p' e0 p$ j, a3 j3 {2 m8 t! o/ H  }     
' L9 D( J! b0 i; S4 p+ _% c4 r     
6 |4 S# d. ~6 Y# j for (nIndex=0; nIndex<id.length; nIndex++)     
7 V* ^+ \& L0 O: r% k  {     
! c: Z1 [! ?. y+ i: V; K  cCheck = id.charAt(nIndex);     $ J- W! j" O( B. I6 ?
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
0 D% |, e4 O) Q8 b8 w   {     : u* T# n  V! j( C. H  k
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
0 E* r; B% C! P6 x; g- P   document.sform1.id.focus();     6 E( Z1 n  `7 o: q# k
   return false;     - ?) ^3 w9 s4 ^9 {" @# e. Q
   }     
7 H3 `; T/ J8 P* `6 C& |( O9 l  }
4 h  u/ w  `& F' r* Y  chineseid = document.sform1.chineseid.value;     8 I! b' t2 _8 i9 y( q
if (chineseid == "")     . O8 k" l  Y" s" S
  {     
8 d* n! Q! F- D. T4 b' p  alert("请输入中文昵称");     4 V6 d  Y+ _! A2 k4 c1 W9 U& |% \
  document.sform1.chineseid.focus();     
" b) u3 w3 c+ K$ @1 H* Z  return false;     3 a: v$ Q6 a/ K# x& q( t
  }   
8 J* _& e" S$ L9 ^0 `0 D4 W+ t$ i: z password = document.sform1.password.value;     
2 Z# K5 |0 _; b7 V6 B  Z$ m if (password == "")     . D* W3 F4 V7 u1 T+ ]
  {     
2 b6 j4 g# s7 j( }7 j  alert("请输入登陆密码");     
  o  a" V1 L" w! y  document.sform1.password.focus();     3 q; U3 ?1 G: }0 X
  return false;     6 c$ K$ M7 X9 i7 A
  } ' y% p. P$ U- a0 S6 l3 X
password1 = document.sform1.password1.value;     2 C" [6 N5 V+ \% }/ \$ ^" m; E
if (password>password1)     ! u! V* B2 K% }* R  o
  {
7 ^3 p! b$ N6 c+ ~* x: g     alert("重复密码与登陆密码不相同");     
* f. p+ ?# s6 B9 I0 U! _2 }3 R8 {# I) m  document.sform1.password.focus(); / i* u, E2 [: g* {' q! @
                                document.sform1.password1.focus();     
4 k* K4 W* v5 Z9 `. o  return false;3 V6 m( X7 I  @) P3 c
  }  ! g8 ?/ K2 {# h; `! |1 b
if (password<password1)     
9 b) o6 s; ~$ q$ N  {6 v# c3 o$ B# M4 _# Z/ D
     alert("重复密码与登陆密码不相同");     # H3 c( d6 d( i; K! L$ [9 [
  document.sform1.password.focus();
% V* M# A3 w2 R* c" C* M$ N* @6 Q# @                                document.sform1.password1.focus();     
1 V2 Y* h" r1 J& ~+ ~9 d( |  return false;
2 c; W8 x: W9 K4 C* R  } 7 [5 I' d/ v! \  L  q
if (document.sform1.email.value == "")     
# x. d) W1 W$ \' f! p- W  {     % q  G( P. C! H1 G4 s& m
  alert("请输入您的E-MAIL地址");     , Z, W7 _5 G; ~- ^  N) ]4 ]
  document.sform1.email.focus();     % x  X3 B5 Z- ]9 k: ]( L& L
  return false;     
1 e  w' C. B4 c4 ~  }     
; h8 Z. }' [2 ^# v' t. }& F/ r      
# a- D8 T, R5 x% O) L% P  z, L5 W$ c; ] email=document.sform1.email.value;      
. Z' x3 |( c- P3 N) o! R emailerr=0     
- z, C, x2 @2 |/ q: m1 T5 d, l for (i=0; i<email.length; i++)     3 m5 M& o3 E& H, j
  {     
( F# `3 A# {! p2 e( l7 C  if ((email.charAt(i) == "@") & (email.length > 5))     % O) M' ?7 Q/ d" F3 R  w
   {     ) O- _" @8 m, W7 W
     emailerr=emailerr+1     * ]" B5 A: @9 \4 W! ?' U0 A$ ~
   }     
6 ^5 {" |8 `# H; o  }     # ~" d2 ^2 j: W( t& n
if (emailerr != 1)     ; j: a- ^/ m$ h+ s8 f
  {     8 X: ~9 s% i' m
  alert("请输入正确的E-MAIL地址");     
" R& h  a1 g9 A% y  document.sform1.email.focus();     
( K7 \9 R  l+ `3 f: g  return false;     
% e& S  L( U8 q2 I  }           5 }' Z' _0 Y5 f' C& J% a
  
' s$ W, m" J+ \  ~3 J+ n7 p# E if (document.sform1.checkask.value=="")
" D! o) s% }* {  { 3 d2 y- V  a- v* i
   alert("密码提示问题不能为空");     " x8 d/ \) G" d7 y- A5 l  L
   document.sform1.checkask.focus();     & U( M! m# L- ^% S9 i
   return false;  ( d7 f. m; z# ]" f7 v; I
  } # \0 N( l$ B  |. I% Z% F$ l
if (document.sform1.checkans.value=="") ; P! M& }! \! b5 h9 P$ r
  { % c; _& h3 f. A5 p
   alert("您的密码提示问题答案不能为空");
2 t0 X/ @8 t+ W   document.sform1.checkans.focus();     & t0 X0 n0 Z7 V! V/ }: F
   return false;  
  }2 R! q; J9 f6 L; W4 _  }2 m. y' A6 Q6 v% i7 N
return true;     
- N( a3 |/ x4 Z2 t- s, k( A1 Y3 S     
4 u2 _# u; Y4 t4 O: @3 q( p} : P) T) Z7 N" Z8 Y
</script>

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