返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
: g" Z5 E- q  G/ Z1 r) u! ?* p7 A<script language="javascript">     
, M8 s  Z1 h1 f5 I2 X; S2 Z  sfunction IsDigit(cCheck)       M1 T" X) v( e: @9 m
{     1 k& G$ \2 w) [! i3 |  \
return (('0'<=cCheck) && (cCheck<='9'));     
: V' S( f% \( V& @ }     & q2 Z. u( `- V+ x: _
     9 }/ t6 t; p. D
function IsAlpha(cCheck)     ; A- M; L1 g( s6 q: H5 ?
{     7 s( o3 k, D- `4 g# o1 K
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     " L" E! C; j) |3 s. n
}              
5 M  _. L  e. l. n/ h9 S3 J      5 A3 t  s. Q8 [$ m8 i- q
function IsaNull(cCheck)         
5 n- L/ k1 l# e0 D" P {          " O& d/ j, ~2 M+ v" [0 q4 ^: a( R
return(cCheck != " ")         
+ B3 ^' A1 h! ]) G) J }                              
+ A# h8 Z& |7 A( Q) _& T( K0 `      
& x: _+ R5 e8 pfunction checkform()     0 t# G- `9 k& l
{
6 T. R# @9 M6 |2 `! v% z  id = document.sform1.id.value;     ; T+ b  Z6 m4 f( S" ^8 _1 `& m1 Y1 k1 @
if (id == "")     1 }6 w( X# F, Q5 C8 a' K6 j
  {     % r+ W2 C) B4 K: {' w# `9 i; @
  alert("请输入注册名");     
; ]  j* B+ ~; i) U  document.sform1.id.focus();     6 k- P1 E1 I7 u9 U( ?
  return false;     ; Q8 x' ~7 j- c/ K8 c* C  m9 y
  }     " v6 H- d- F3 C) M7 g
     " ?2 @  W0 ?5 Q3 o! w
for (nIndex=0; nIndex<id.length; nIndex++)     4 D+ H' B1 f! ?/ ]; ?
  {     3 _  E# w* O* d1 Y) D' q2 C
  cCheck = id.charAt(nIndex);     , Y7 `7 l" C6 v6 w
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
. a4 Q$ ]4 Y! M. L   {     # c4 G$ K6 v( M; q+ _3 d+ q
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     " I0 B/ C: F! M( E. X% x' X
   document.sform1.id.focus();     
3 X) t. d2 B' K0 L  _, \- a   return false;     
; I( T  n, ]1 A; S# Q   }     
% W. ^/ L* ~$ K2 f  }
) Z8 ?  n+ C7 ^8 o) U3 F' @4 a& C1 C  chineseid = document.sform1.chineseid.value;     % r+ ~- A6 _( J! R% c3 y3 R+ X9 Q0 H
if (chineseid == "")     * l7 C# r/ F% ?7 K$ V  ?5 O% f2 f; `
  {     
) K& j" l7 G; O. q4 j  alert("请输入中文昵称");     1 T* C* T. l; n. {) L3 j
  document.sform1.chineseid.focus();     
6 Y% K2 h7 \( h5 v9 X) f) \  return false;     4 \! ]/ s4 B$ q% O
  }   
- b  q5 Z+ G. ^+ F( ?+ c3 O password = document.sform1.password.value;     2 m. I2 ]* y7 r+ k2 S
if (password == "")     
; E' E7 i) j7 i4 [% }  {     % P7 m' H* Q3 ?  u! d5 c
  alert("请输入登陆密码");     
9 q% w+ X  G8 p/ p% ~  document.sform1.password.focus();     
' ^& _2 E) I. g" j- s. b; ?& r  return false;     ) }: k4 N: u/ k, T; F
  }
" D1 Z4 m2 S4 H1 c password1 = document.sform1.password1.value;     
; g3 x8 Y5 f9 {/ Z' `) \) H, { if (password>password1)     6 y. @0 Z8 Q6 h
  {: w1 M# o7 b9 c& y9 p8 ?8 n! C
     alert("重复密码与登陆密码不相同");     % x: K6 q" u! n7 Z# p. i+ ~
  document.sform1.password.focus(); : a2 r! o& r8 x- |# q1 t
                                document.sform1.password1.focus();     % H2 q" a1 G& L
  return false;# s1 Z; F  b! N- u
  }  
/ x! t% v. T  ~+ y9 D9 s5 [if (password<password1)     
% \. A; v. S# o/ ~$ ~( q  {
; E. e$ U. E1 o     alert("重复密码与登陆密码不相同");     ! K3 ^( |1 s" t" Z) X8 {
  document.sform1.password.focus(); . \7 v8 e- ~; c  G2 C
                                document.sform1.password1.focus();     + _& y6 _! S$ C, e9 s) \
  return false;
5 q* O+ t' {1 M/ _  E3 Y* ^  } 4 R6 K( Z/ O5 T+ y" C
if (document.sform1.email.value == "")       O. E. ]9 b) g" I* O3 x
  {     
2 j& a! _- t/ ~3 v  A  alert("请输入您的E-MAIL地址");     9 z* `3 @, v$ H- q
  document.sform1.email.focus();     * e9 q$ d$ u9 F5 |1 P2 q
  return false;     
$ @2 R( t/ p/ L2 g, n  }     " O: h0 T( U; h- j, R" x
      
2 n& ~# Y9 ~' O email=document.sform1.email.value;      $ W/ ?8 c' C( w. s, d0 d) S5 W
emailerr=0     
2 g6 h7 R6 f9 Q( j. t: \1 f/ [+ M4 \ for (i=0; i<email.length; i++)     
8 z7 x1 t" I6 d+ {7 S- i2 p  {     
/ n8 O. B  V+ x9 J  if ((email.charAt(i) == "@") & (email.length > 5))     $ ~9 d# Y3 m7 w: w5 Y
   {     
! c5 L3 W5 i; I- Y% I     emailerr=emailerr+1     7 [5 E$ P  X; s+ \- [- [9 s
   }     
& |) f% t- D# z4 C; Z2 A1 ?  }     
) Z  l. M1 W# Y6 V7 _ if (emailerr != 1)     
. s0 x' h* ]$ A. r6 {  {     . k7 _- a- L1 B6 `; d  g( m
  alert("请输入正确的E-MAIL地址");     
. r9 k- N! I4 S: G0 c  document.sform1.email.focus();     
) ]; ^; \/ K  w* t  return false;     
) U6 Z& ?4 o: S5 L- B  u  }           # u" R6 T; w  G- n
  
1 P, V+ U8 t# d- q if (document.sform1.checkask.value=="")
& y  }5 |1 t, d) V- u  { ) D0 F: l9 B& T# C
   alert("密码提示问题不能为空");     
6 u0 S* [; k! t/ k1 U: d   document.sform1.checkask.focus();     8 j+ j/ P$ t: x9 I
   return false;  
2 P: u7 N, F6 W% J3 }, O  }
& o2 H2 A3 x* ~- i* S+ J+ w if (document.sform1.checkans.value=="")
. ~8 @- {5 h4 w6 f  {
; @% d& [; p& {   alert("您的密码提示问题答案不能为空");
9 n; t) Z$ S) |7 l   document.sform1.checkans.focus();     
) R0 G( l4 }1 J# H   return false;  % l/ X, k: |5 M) N# [9 ^
  }
3 Y! q5 q# ]* D* X* L return true;     
& m+ [" {# w  {& b! d  ?" Y/ v     
7 h1 O# Z9 ~# a: n+ |. E0 H}
6 V1 G' y4 u3 G0 v2 G9 P& L1 x( l) M</script>

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