返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
1 O0 l( D/ x+ F# D# s  [# T<script language="javascript">     1 q8 P, y1 F/ S
function IsDigit(cCheck)       h; d4 c, F( H: o4 Z. f8 h+ N
{     9 q8 q3 j  Z4 ^( r% X5 J1 _
return (('0'<=cCheck) && (cCheck<='9'));     . A( I' T6 y* K7 e4 R4 p& B
}     
, y% {* Q( j1 R) l6 _8 x. x     5 @& F# I6 {# F  r& O# h: k+ D/ x$ e
function IsAlpha(cCheck)     % W+ V/ @$ [7 j5 s* N
{     
$ G: h. z& h% F/ M- N; _$ C" F return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
, e3 F( I3 r& S6 o! r }              1 ?. G& @7 X& t* C' `
      / D; R$ e$ e/ T7 l9 w' W
function IsaNull(cCheck)          " s$ B: M- [! T* k/ b( b+ F3 @
{         
( U5 i+ o7 n" G' ]8 W return(cCheck != " ")         6 ^& `2 E7 ^3 F4 D- q- H8 }$ U
}                              
$ W, Y8 ^1 i% @( }0 ^      - \0 \$ y8 t- v2 S9 Z6 ]
function checkform()     7 [/ A. P/ A+ g
{
7 O) N: Y( g* m  id = document.sform1.id.value;     # ~+ R# k9 E5 ]5 L9 P: n$ U
if (id == "")     3 D6 w8 W0 _* Y( c6 y0 {1 B6 y
  {     / c1 u$ O8 u- ^/ U) P4 Y
  alert("请输入注册名");     
4 w4 Q! s8 }1 I& t' @: R- N3 v! J  document.sform1.id.focus();     , X7 b% \9 `, D3 J" G, n
  return false;     5 l. h8 S% E! i. O& [* h5 {
  }     
$ W. s/ Y% ]' X3 g* M# Y& B2 v     4 o. n! Q* l5 M
for (nIndex=0; nIndex<id.length; nIndex++)       l7 L) ?  ]8 {6 H5 |" u! W0 n2 U
  {     ; O; @# d' J8 c% @/ \
  cCheck = id.charAt(nIndex);     
$ Z( M6 x- L3 k! l! T5 @& |/ @  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
* B  b4 L  B! u" ^) ]0 `1 |- \   {     ! ]3 s: J. a9 E4 V! S. Q
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
- l/ h2 C( N' k& K- L   document.sform1.id.focus();     
" ^, D3 F8 m  b! `7 y" T( q+ K   return false;     
9 v% \3 M7 X* @2 h- C3 s, Z0 Z" X   }     4 C8 g( s- Q- b. V; j6 C1 a# Z
  } ( J3 a- J0 X  E" A
  chineseid = document.sform1.chineseid.value;     2 q! Z7 {' s8 E9 f( r8 A
if (chineseid == "")     
5 E' P) j2 J  p: ~9 m& }: D0 ~6 c  {     
% H3 G/ P1 h. a5 @" [* P  alert("请输入中文昵称");     $ N+ i2 j7 q& f
  document.sform1.chineseid.focus();     
, H: N6 ^) s' ?. C; y1 O  return false;     ) \" R7 t; B, k1 l1 M/ ~
  }    . v$ i$ ]+ r" y$ P6 j8 i
password = document.sform1.password.value;     5 c) g0 H8 a0 N# z, D" z
if (password == "")     0 a; F, K( i2 d) V. i, ]& t2 M
  {     - u& d7 ]3 P" q4 D( d
  alert("请输入登陆密码");     
& i7 b  N# r) Y5 K5 {  document.sform1.password.focus();     
" Y) S: v5 j3 q  return false;     : _8 |2 v+ c2 G8 u: u
  }   y0 e- }( T9 a
password1 = document.sform1.password1.value;     
/ l/ m) B/ T6 i if (password>password1)       g8 X+ l. n9 w: k* z1 H+ M
  {
9 d$ v1 h3 ^7 ~2 P8 ?$ G& M% e     alert("重复密码与登陆密码不相同");     4 @7 @# R$ ~; Q$ I# q  {8 p
  document.sform1.password.focus();   r$ H! z8 k2 r
                                document.sform1.password1.focus();     ) ]  H- h4 _% x% N$ \
  return false;
3 |. e$ w1 ?4 o, a& Y; O  }  ; j3 A; c+ ]: w, @# V
if (password<password1)     5 ~' V- v: v0 C" [3 \* w  n- r
  {
: ~7 v3 C# `0 `     alert("重复密码与登陆密码不相同");     
8 Z2 I# \& d( }4 L# d  document.sform1.password.focus();
& Q/ }5 _, H% I8 Q% W' y/ l                                document.sform1.password1.focus();     . J, `" K# u! }/ G0 l/ {: ]9 ~; ~
  return false;
0 i6 E2 \( t) m  } ' x; s# Z. I1 P, E  [, N  q: H
if (document.sform1.email.value == "")     
7 {0 {( ]" X! E) e4 o  {     / [* r4 Z6 A0 Z/ Q
  alert("请输入您的E-MAIL地址");     
6 ^; X  n0 H6 q& B% S  document.sform1.email.focus();     . I% E# D/ f4 d7 F+ M! x2 q
  return false;     
" g# `2 z5 W" k! l! Q9 ?( H, E  }     % ~) U2 a' T6 w( }/ W3 ^
      
+ D6 c0 _5 O* I1 K7 G8 c, }5 B7 Y email=document.sform1.email.value;      - z! k3 g7 Y& O8 `
emailerr=0     
) W; q: G, l: l) M# y, _ for (i=0; i<email.length; i++)     
! S5 a' N+ Y4 ]# P  {     - I; S  F6 |6 q- Q. ^; P( I6 u
  if ((email.charAt(i) == "@") & (email.length > 5))     
2 I7 g( q* K: F- H9 L; |! ^. `+ o   {     
8 f0 R* p0 ]) t# r% X     emailerr=emailerr+1     4 b0 y/ ^# ^/ m
   }     
( V& O0 x, w+ H# n  }     
! C9 R1 i6 o0 [6 `$ y/ p9 U0 G- B8 b6 V if (emailerr != 1)     " z, S/ A0 L# @/ l( v6 Z1 s
  {     / B' L" C# k3 X- {
  alert("请输入正确的E-MAIL地址");     
( d, ~$ A2 b8 G  document.sform1.email.focus();     
2 F/ o% s; p- f  return false;     9 v( S2 `1 F' z; Q% `
  }           
) ~' F% J6 T0 G8 f2 a# [  # e! }7 f. R7 d# B" C
if (document.sform1.checkask.value=="") 9 u7 M) g5 l6 _$ W* L$ `% Q. B+ Q4 P
  { 8 R: n' Q% O' Z
   alert("密码提示问题不能为空");       E+ A, R0 E) Y! j; h& Z
   document.sform1.checkask.focus();     
0 Y# A8 R1 a0 E" H! K' N: D/ z) `/ {# M   return false;  6 |! b3 d1 C  `( @# K/ a
  } 4 _+ s7 F! f: M6 J0 f$ N2 l
if (document.sform1.checkans.value=="")
$ }+ x% u' J! p) n) Y" h  { * W7 g9 s" X" A! i- n0 G
   alert("您的密码提示问题答案不能为空"); 1 V/ J7 C) k4 l5 ^8 l9 \0 g, X" B
   document.sform1.checkans.focus();     0 J5 K6 J3 K" ?% w/ ~: h
   return false;  6 ~+ o4 p- b* @% H5 i1 e( M
  }
; x8 P" H. q' u& o" G return true;     " ^7 y* R/ {, [- q0 o0 B4 U) M
     ; O& P  [* W0 x. y& p9 ~0 `, J
} ! {& b1 C. l) P
</script>

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