返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:7 b$ Y1 l) ^6 [; j* F
<script language="javascript">     
" w) k* A  e- nfunction IsDigit(cCheck)     
% A$ C. J3 w: Q! g8 ~8 D$ m/ b- _& A {     - j! O: p, v- U. X1 r) M- W
return (('0'<=cCheck) && (cCheck<='9'));     9 F# [+ R( }6 q0 j5 u% w
}     2 T  {" O6 W! \; O" C7 l
     % x- ?( _& `: j
function IsAlpha(cCheck)     . T: o  z6 N/ B/ _4 a
{     
, b4 d' V8 r0 A6 @ return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
6 V8 Q' p/ n6 f+ w( Q& Q. Z }              6 p1 R2 A6 F- \) J0 V$ [3 m/ d$ i
      " ]& ?% B2 D, e# B$ b' `
function IsaNull(cCheck)         
  _2 i0 B$ ]8 O9 |% ` {          ; d* s; O1 c# j3 g/ S
return(cCheck != " ")         - x& T1 @# f, m8 D% t
}                              
# ]5 T" A  p- A4 {        O, a0 j9 P: P( Y( H; V4 B- H& }
function checkform()     
3 B% i0 t/ d' A* H* ~! r' r. [{ # q1 f8 i+ q9 q8 A, q. A
  id = document.sform1.id.value;     0 B7 `3 |0 Y* |7 P
if (id == "")     ) F" Q. F+ b; v
  {     
8 K$ d0 m4 L9 d' }/ _  alert("请输入注册名");     
( |& `+ F( Z" v: X' M6 o7 ~  document.sform1.id.focus();     + s4 b, b, n! O' H
  return false;     
; x! f9 O6 ~* U/ v/ J! k$ x  }     
# u6 T1 \3 o! e     
& d" S  l* ]5 H for (nIndex=0; nIndex<id.length; nIndex++)     0 I& C! _/ z- ?, L4 N1 i
  {     
& U1 D1 J3 Q, i7 I# x  cCheck = id.charAt(nIndex);     
4 ~1 e. C' x" P: p  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     3 o5 M; V) ]9 M- \) k9 b7 b3 A5 V
   {     ) E$ \/ m- k# t* O( [7 g
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
2 S  y8 O! q+ V1 u8 W  X& l- F   document.sform1.id.focus();     
& n1 w$ z$ i0 |* X: |. p' p  _, `   return false;     
$ U& |  q, y6 G/ x   }     + K  s% t* ]1 a: l2 C
  }
/ G6 \) k7 \5 S* M) h+ J  chineseid = document.sform1.chineseid.value;     ' b! S  [# n) _9 z, Z5 l, c4 {8 H1 ?
if (chineseid == "")     
# ~* Z% H/ F# v/ i1 R% a  {     + o9 h9 ]) g. }5 l
  alert("请输入中文昵称");     ' {- k: [) k! M4 \, w' g5 x1 T
  document.sform1.chineseid.focus();     9 P( @) Y& C9 y$ d' s7 g
  return false;     
$ W* O; [0 c5 V5 z( u+ r  }   
1 S1 |$ R0 `. M1 i; K7 a password = document.sform1.password.value;     
, `9 k- [# x) d# G$ R' \- R if (password == "")     ) l( C/ d! s8 a5 J5 J
  {     
1 X2 K$ h+ X* y; e0 A: Y9 f  alert("请输入登陆密码");     " [8 d) }! u5 j0 I: V/ \9 K
  document.sform1.password.focus();     
1 q' z/ j0 T7 K9 K  return false;     3 B' f- \$ P/ u0 B% t- d
  }
5 x1 v8 z- D  W$ f0 V password1 = document.sform1.password1.value;     ) e% b$ u  G; b: b; T! n/ _
if (password>password1)     
& R& S, v8 z& S) i  {7 u) p6 R4 k2 A0 k* [7 D8 l2 z2 x
     alert("重复密码与登陆密码不相同");     % S' [* `9 L! Y; m3 Z1 z/ u9 j
  document.sform1.password.focus();
5 ?$ n# a3 l( u$ A- M4 ^. ~' L                                document.sform1.password1.focus();     
# M3 O$ A  u! l4 }0 H( U  return false;* P8 E* \9 j4 @( i7 |  M
  }  ; D) f( Z% {* t' T
if (password<password1)     ) ~' Z6 s+ `, p! j# X
  {
) y; ^5 t, m0 U- B     alert("重复密码与登陆密码不相同");     
! o9 d) r9 W/ Q1 E+ g* B" k  document.sform1.password.focus(); ) a9 m4 d  ~  x' J7 D
                                document.sform1.password1.focus();     4 d/ L- H* A) X( Y7 W
  return false;
; |9 t1 j, W4 y8 V  } & t  {* O7 y8 `. _! h1 l4 E
if (document.sform1.email.value == "")     
4 \* g9 s0 w9 u  @% @" z) T! F7 `  {     7 v1 p& B7 q/ I7 G  q# H" R7 O6 r
  alert("请输入您的E-MAIL地址");     8 z+ D9 j! g5 M4 O; s% t) c2 J$ y
  document.sform1.email.focus();     / K. F# w) c# Q1 W) R, F* A3 \
  return false;     ) X, d+ @- I6 H
  }     
& }; K% N' R. c5 M/ a, Q6 W      
  b: T8 R$ t9 O( {% ^4 j email=document.sform1.email.value;      
# k$ o5 j' z# m; _4 i& h8 w emailerr=0     
' I1 I0 D- m0 `5 m: T7 F for (i=0; i<email.length; i++)     
3 x$ T1 s! @+ V) s' j. {6 l  {     8 A8 x- M2 u/ `1 f2 f
  if ((email.charAt(i) == "@") & (email.length > 5))     , F) x- I) G; l  q) o+ G
   {     - p  p5 G. S8 z2 {4 D
     emailerr=emailerr+1     
' [6 p8 m+ q( _' x- R! l   }     
: V# a; ~! h* g2 N5 W! T* k  }     
+ X& _' C! P; r; X if (emailerr != 1)     
( _$ e0 y! @, d* i  {     0 \9 l0 M: h1 n
  alert("请输入正确的E-MAIL地址");     & V% U# z. [+ I! L, {* `+ I2 B
  document.sform1.email.focus();     
# A( ^* J& t2 o( u5 N/ Q  return false;     7 G. H1 b, j2 r* F' ~& N7 B2 x
  }           
7 U1 ^+ F7 b$ P+ I, o0 v  9 C+ C) K6 n4 n: _( E$ x% e! b
if (document.sform1.checkask.value=="")
+ _. ^: A( q. q4 e0 m  {
" d" ?# @' H, w1 t   alert("密码提示问题不能为空");     * |% I! P# B5 H. V
   document.sform1.checkask.focus();     ' I" C/ e4 Y2 G3 n
   return false;  & U- |) U: @4 i( p4 h, p; T) G
  }
+ A% t3 B# v/ R/ o* X9 {: J% i if (document.sform1.checkans.value=="") 0 R: ?- q& O" A& u5 O: a5 h
  {
2 |6 d" b/ c& a8 }   alert("您的密码提示问题答案不能为空");
9 _9 Q& E1 \  Y9 X& n   document.sform1.checkans.focus();     
9 V% C2 Y$ G5 q8 [$ N+ f   return false;  
9 {3 Y- l  \! H% X  @( ~+ g% a+ \  l  }
' t  T: {) z+ ^- g return true;     8 Q" S- B& c0 x4 D7 y6 [
     . |7 O, z5 l% B! E7 R1 m8 h
} + z! M7 H. h3 A- S; U$ w4 X" U+ O
</script>

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