返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:$ z3 n+ f6 Z  s1 R! _
<script language="javascript">     + J3 e' T% x+ K( l
function IsDigit(cCheck)     
& @7 s. f, Y! t: x4 x7 k/ q; j {     # x& Y3 {1 O$ U7 w- m
return (('0'<=cCheck) && (cCheck<='9'));     
: N! e/ `) \5 \& V& n0 g }     
! t$ m# D* f+ P     
; B- s3 u0 V* c6 }function IsAlpha(cCheck)     
9 J$ }/ Z; b, a; w {     
8 V3 T; g, C' [# h1 D7 W return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     " r. m7 m: f: d5 h
}              9 G% t$ |; |! q9 z+ a
      
- E" d3 ]: n+ D2 pfunction IsaNull(cCheck)         
- g1 N3 V& x4 u5 s( J( p# a6 \/ y {         
' [& X0 n# {0 k' i% H. h6 B! @ return(cCheck != " ")         
: k4 U! }4 F7 `7 A, [! Z0 o" c: k }                              
% D  x$ s( C. w( O      
8 A5 p: B( \: Z( S  V6 `8 Ifunction checkform()     
# g" U' ], k' i8 r! `/ ^. _{ $ N! u# z& X& E+ \
  id = document.sform1.id.value;     
" P5 }4 `5 s! Y0 V0 d# W if (id == "")     / M" m% [- p7 ~- y& v
  {     
0 B. d! W2 F; J4 s4 _  alert("请输入注册名");     4 d: K- Z* k/ p
  document.sform1.id.focus();     - e) r4 O, [5 U( [5 D7 m
  return false;     ! a7 N  M2 C4 m0 \
  }     
. g' N; q; _$ c+ b5 C3 L3 n, V     
7 x# i9 Y* t' j5 E0 }* r$ n for (nIndex=0; nIndex<id.length; nIndex++)     
, J6 p; U& q* s- S6 F7 I! U" F2 U  {     
. l  ?- ^1 z) M4 x) _) g% ~  cCheck = id.charAt(nIndex);     9 {. P. Q5 |' a% s, c. O8 Y$ s
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     7 u7 D5 `/ ?: y& G& f# Q
   {     6 v$ B1 M1 Z3 [* H
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     + ]$ V% P2 f1 k. `( l# u- y  N
   document.sform1.id.focus();     6 Q6 q6 {1 M, t) d% x1 L
   return false;     & H: i( Z0 V, y" E8 g1 y/ S
   }     
1 E+ v6 ^% `! i( J7 @  } . k& S$ P+ ~+ g7 r3 d
  chineseid = document.sform1.chineseid.value;     2 `+ j; Z% m' a
if (chineseid == "")     
0 X6 _  l5 e& ~0 \  {     8 e! i) U4 ~; v1 H6 k7 Z
  alert("请输入中文昵称");     
. z: \; l5 [, h+ h4 @8 N1 b& Y  document.sform1.chineseid.focus();     
9 k. ?8 P. F' X- C( m  return false;     0 q5 B8 J7 O1 y$ l) f$ P
  }    : b* m& v$ K! _2 r$ r4 z2 R
password = document.sform1.password.value;     
% W0 r; N$ h* K if (password == "")     ; E6 y1 Z, K. y% t/ I7 o9 Q
  {     4 N3 a" H6 a' Y7 |9 j% `! z
  alert("请输入登陆密码");     
3 o- {4 |+ S1 K  document.sform1.password.focus();     
4 d/ U' `3 G  x/ w: M6 L% E  return false;     ) w+ ^1 {* h! O% v
  } $ \/ R. `! Z/ M' c3 W5 b
password1 = document.sform1.password1.value;     2 b( V% |+ J6 ^9 \) X: G2 S8 R; o
if (password>password1)     8 c% C, X4 A% O* V9 i: R0 q
  {. @* @. o5 t7 L5 v3 z0 a
     alert("重复密码与登陆密码不相同");     
0 J0 n. k: X0 e0 ^5 M+ \1 O; O  document.sform1.password.focus();
( m$ B7 l$ G) f* G% a8 b                                document.sform1.password1.focus();     
: P0 _4 B( b; n9 F7 o  return false;
; i; ?' w& e: v0 B8 C6 N+ x/ G: N  }  ( O- t8 A. a3 U5 y0 z
if (password<password1)     
1 i. [( P( \+ i* |1 w4 p" t- V' R9 J1 ~  {
! e) T* l. W; w0 X8 U6 F     alert("重复密码与登陆密码不相同");     
$ U, V+ n: m! m' u7 |  document.sform1.password.focus();
: G. Z; n+ `, C$ l+ v& C' J/ H; W                                document.sform1.password1.focus();     
5 |, I& q/ G- a  return false;6 E/ l# p! C1 \! H5 G
  }
) z" T$ ^# z! F! M if (document.sform1.email.value == "")     3 p6 J: b8 P1 B8 c
  {     
; x3 b, N0 J. @7 C6 R7 z/ N9 Y( f  alert("请输入您的E-MAIL地址");     ! g5 C9 C3 l& ?3 v- N& W. _. w# A5 l
  document.sform1.email.focus();     
0 w9 d( M/ i$ z4 j0 o' l  return false;     8 T% e8 t+ Q' a  G; j1 |
  }     
" X3 Q7 J3 F  r& ?' h2 O$ u1 k0 @      
& W+ N: R: l% {- B# |5 S& w email=document.sform1.email.value;      
6 Z. k0 {0 h& Y emailerr=0     
/ k; ^* y" x; a0 t* ~ for (i=0; i<email.length; i++)     5 a' L$ X9 |% g, b
  {     
9 z8 \4 d0 h2 P0 U! u  if ((email.charAt(i) == "@") & (email.length > 5))     
! Z. o- {: I+ t5 l$ W   {     
* a! t* R  T" P% f     emailerr=emailerr+1     / f$ x0 {8 \' V% l  L3 A
   }     
# L1 k' b- D! x$ l1 M# [1 i  }     9 ], ~" m( q) F2 D. i' ]
if (emailerr != 1)     
9 {9 B$ a' D* a# x8 t/ L  {     
5 d+ M  g9 ~- c) T$ H) E  alert("请输入正确的E-MAIL地址");     
, ?  [4 I0 s1 ]2 e  document.sform1.email.focus();     / ]6 O* g6 k3 e9 B. ^) \, o& T' H
  return false;     
5 C& h, B; s  v+ S* L# E) r  F% P  }           * S* I! S) b9 F3 I
  $ }% A) t; K7 ]2 ]" n* Z( V
if (document.sform1.checkask.value=="") % y0 H6 V5 C7 L2 {
  { # o; ~- v5 h/ H0 C4 a0 U- Y
   alert("密码提示问题不能为空");     2 _! b0 Z! x% u
   document.sform1.checkask.focus();     
  V7 F; R5 ]/ p7 D7 H   return false;  
( x  Q$ N2 b. k: i  }
1 g& i! i2 |. B4 | if (document.sform1.checkans.value=="") . l3 x0 H$ ^" `8 q' B4 ?
  {
5 s6 u$ U* t4 W# O8 ?& p" b8 l+ ?& H3 s   alert("您的密码提示问题答案不能为空");
$ p% G  n8 z4 w# M' i+ P# g$ Z/ K   document.sform1.checkans.focus();     & s4 ~: [& _" P
   return false;  
5 y. h2 ^! H+ p  }
6 z6 \, c) q* [6 n4 s4 e0 F( H return true;     3 M2 e7 Z' F' w* M* |* t
     
2 F4 b' k  E+ x: h( J}   M) ?, L5 b9 [5 c4 ~4 N* D. L( d
</script>

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