返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:: u0 ?$ A$ r0 q6 T( s( E1 M
<script language="javascript">     
9 r3 j, F! x1 Yfunction IsDigit(cCheck)       d" n& I  J: v) l( V# ^8 m" C! ^
{     
6 ]) {) S# W: P4 H; \& v: P return (('0'<=cCheck) && (cCheck<='9'));     % a/ U4 h8 N5 r4 v; D& |/ O/ |
}     8 K% p% y+ a- H' b
     
4 {* P  {2 d6 p$ ~+ P5 xfunction IsAlpha(cCheck)     
- D  L6 c, Q- f4 g+ F. ~ {     # d$ H" w" q" I5 }; e0 r% Q. d. `* g
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     4 k& E9 |  q8 s: k' }  A2 [' l0 u) P
}              
  B: T; L  j$ l0 p1 L0 B" x( j& p      ; K. Q9 b: e2 i$ @
function IsaNull(cCheck)          - H) q! f9 n- b6 {+ |3 ?
{         
8 }1 b& A! ?3 ~9 w" Q+ W return(cCheck != " ")         
/ K! t6 k( K& ]! m }                               # U6 k/ Q3 i: _, l0 u% y. r: y
      
* `9 G( g$ n5 X- U0 i4 y5 Ifunction checkform()       @8 R! p) h* V* ^3 Q: n: V
{ 9 }! {2 l) O, q, |, T" A
  id = document.sform1.id.value;     
0 f$ S  u7 F: e3 y: g if (id == "")     2 `5 G5 m( v: s  b6 H, \# c
  {     
6 t+ L* e7 |0 ]$ G1 `  alert("请输入注册名");     
6 X$ g+ p1 K) E( B  document.sform1.id.focus();     & z# ]  _: S# S  K( U
  return false;     
/ w5 _* K0 h! ^. v7 c2 Q  }     ' x, I4 Y3 \- C1 F* E9 }8 I
     $ `: e4 s% o, \  Z" E' J# T+ \$ `
for (nIndex=0; nIndex<id.length; nIndex++)     
# E6 l% }3 ~1 R' P. _' `1 b* q  {     
. P1 X! x- Q0 l8 @. F" E  cCheck = id.charAt(nIndex);     5 D7 ^5 o3 z6 v) i
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
0 B. r9 c% J% D2 X, i  A   {     
5 ]: d, C$ q' C& c0 D   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
$ {# }' s& x: v- U! H3 t( ?! @3 S8 d   document.sform1.id.focus();     6 ]1 {7 S, u7 G7 Y6 y
   return false;     
, S* u5 Q/ k" l3 l- ~3 W   }     
5 x4 s0 M  ]$ `" m; s/ }1 U/ E  } & N7 k% e+ D% Q! [- A( `* K* B
  chineseid = document.sform1.chineseid.value;     
$ \% A) ~$ a: j- z5 Y3 W if (chineseid == "")     
/ O7 }2 m& i8 z8 R7 [) z4 y3 B2 V  {     
; _; @# O4 \$ z# `/ ]  alert("请输入中文昵称");     
" T' l8 n/ t# S0 R  document.sform1.chineseid.focus();     
7 M: |2 t! H* X4 o9 `4 K8 x4 [9 T: ~  return false;     1 j9 E3 w9 A# [
  }    ) S( g) p- o4 n% ?, ^
password = document.sform1.password.value;     
' y* }8 Z- q% M$ g  t, Q) R- X: j if (password == "")     
) V$ K% B8 \, U* l, |2 h/ X/ d7 X  {     
* F$ G; r; T2 V0 `) G  alert("请输入登陆密码");     3 n, K! B* k+ u/ i6 C3 I
  document.sform1.password.focus();     % H- t3 H* C& @, y. b
  return false;     9 i, `* i, X* Q" n2 e2 M
  }
6 f* k6 h8 v' p4 K; M. D6 k! |$ T password1 = document.sform1.password1.value;     # Z8 q" H) q! [, P( }9 G
if (password>password1)     
/ }1 N0 p3 \" W9 t2 x  {
' @- j3 _" r% v8 p' P     alert("重复密码与登陆密码不相同");     
1 _9 e3 ~+ p- q! M8 N  document.sform1.password.focus(); ; t3 M) A9 a/ y) G4 l: Y/ @
                                document.sform1.password1.focus();     1 g0 W4 o- ^% ^" E$ `6 E
  return false;. @! y# p( t5 W3 j
  }  
! x! R4 |) J- Y& C! ~6 L( f0 Gif (password<password1)     2 j$ K& _( S* G, R' d
  {* a0 B9 ~. C* O, p* U
     alert("重复密码与登陆密码不相同");     6 R/ d  c9 r) ]2 q* f
  document.sform1.password.focus(); 2 G$ m2 q2 j6 I
                                document.sform1.password1.focus();     2 j- X; O+ o* f5 r
  return false;, @. n3 T" j- l4 Q2 f
  } , h+ Y) m1 f) t4 ]
if (document.sform1.email.value == "")     3 ]2 e# l+ U# K6 K
  {     ( V# Z- |& ]8 x# ?0 D6 }
  alert("请输入您的E-MAIL地址");     
1 f3 b; b. R, ?' R  document.sform1.email.focus();     3 ?$ y; u( [+ u# z( i
  return false;     
  j; U6 ^% F" t' f  }     % u3 l+ D7 t& _
      
4 O' j: O8 Q9 Z7 Z/ _ email=document.sform1.email.value;      3 [9 L1 d  D3 S0 _" U: u
emailerr=0     * r4 u: o* u0 Y3 P  [% ^( S
for (i=0; i<email.length; i++)     
" N$ m& I7 Q0 a3 X  ^6 q  y  {     # I- `$ X& z5 |' {+ z" v. P- A
  if ((email.charAt(i) == "@") & (email.length > 5))     2 o) v' X: r! v4 J: |/ Y7 j
   {     - s+ y& b5 e" g
     emailerr=emailerr+1     ! k+ m. b: f+ K5 v+ [
   }       \4 M' B  F% I4 V, R1 g8 J& G. ?
  }     6 ^, P- }6 h) W7 ~, ]* w& P
if (emailerr != 1)     " W9 m0 F7 k' }0 @2 Y
  {     
7 l/ Y! W  H6 z  alert("请输入正确的E-MAIL地址");     
7 r( x* ?$ d0 S  document.sform1.email.focus();     
0 b* P) [5 y4 K1 A# Z  return false;     0 P% y" T& }5 y1 k7 d1 }
  }           ' F8 O3 ^: n: g9 [- a; w5 [
  
2 B' c7 P& h8 ~4 N# j if (document.sform1.checkask.value=="") & x! \) s' F. ~" `" M
  { , `" w; Z" T5 U9 {- A+ v; y
   alert("密码提示问题不能为空");     + b8 p; q% `$ V
   document.sform1.checkask.focus();     
2 N  T, H' t1 `/ x/ p   return false;  
, V! b+ q: W9 _. L  W8 R" i  }
* C. d. v7 ]* ^ if (document.sform1.checkans.value=="")
0 h8 v! b& t+ k1 S7 w) n3 N1 e  { : S! O. C, v; a3 h
   alert("您的密码提示问题答案不能为空");
* P% L' t/ ~' k   document.sform1.checkans.focus();     ; D; Q7 D) V, ?5 S
   return false;  
" o: @9 R, G5 w5 A% `! C. V  }
( Z% \( i& ~8 ^# [( l/ B( W return true;     
7 r' y( V" y, A" `  b% ]     " H- L& T1 y& r* j6 i
} 4 T5 x( p2 y8 F* ^) D1 y) z0 o! \+ x
</script>

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