返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
. x: o; q. W; Z8 ]9 v<script language="javascript">     
" i, ~5 W/ w6 x( ~' afunction IsDigit(cCheck)     
/ |6 m2 W# h$ u) k, s  i) f/ E) i {     1 [- a( w  @# z3 i
return (('0'<=cCheck) && (cCheck<='9'));     ; P; z1 o0 v) _" Q
}     / y) {/ m0 i/ n$ k. f
     * C. B' q2 z4 O, b, z7 ^0 X
function IsAlpha(cCheck)     
, x; c! P* U  d9 }/ x8 ]7 P {     " B1 L) |5 v  E4 h+ B) T$ ?$ j
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
  i7 b+ I' S7 N! g9 H }              3 [5 `  A) m6 T# j/ u
      5 n/ y+ Y# ~* Q% H* t: ^
function IsaNull(cCheck)         
# x% J+ W% q, f% H1 U: N {          & h0 [4 A. d/ j, w5 O
return(cCheck != " ")         # Z5 h6 c: J/ @8 G% e
}                              
7 {8 M1 g6 ^% a      4 s! [/ R$ Z3 z9 W, d9 B9 x
function checkform()     + @- D% s" h- [* m! Y' R! D! {
{ : I# ~% ^7 c! y
  id = document.sform1.id.value;       C5 ^: ?" h( i8 ?1 l  P' G
if (id == "")     6 Q. g* p6 z9 C# }' m$ s
  {     3 J4 d  `  Q; o" N+ c
  alert("请输入注册名");     
* w. G7 Q$ M( E1 }- {  document.sform1.id.focus();     
( V4 l7 m0 h/ ]- d5 B' e9 N  return false;     
1 O* C) |2 _* @- i  }     ; S$ A' i2 H4 ?! g. V
     
3 u2 g2 Z- ^7 s% F. v2 q. Q for (nIndex=0; nIndex<id.length; nIndex++)     
6 ]1 m6 x- I+ K7 b0 Y' n  {     / M$ x& N( G' U. A
  cCheck = id.charAt(nIndex);     
2 n& m, k: m. E* K  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
/ O& ?1 o- Y2 }) g   {     
1 c8 b$ _4 d7 D6 n) ~! v' A   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     8 [) E8 `. k' S; v3 }7 x
   document.sform1.id.focus();     
2 C) \) A: S) t7 q, s6 \   return false;     
1 a& E, m5 `0 A, f$ D9 H- N% H   }     - j% J; d( i8 A, [* F5 O3 y+ j: k
  } 8 Q/ c5 p% t0 w
  chineseid = document.sform1.chineseid.value;     
( B$ T/ @; V( y* i! |( C if (chineseid == "")     ; N- r" U% T( U& U( n9 ?
  {     3 o6 A8 P& w* @9 v+ |% v
  alert("请输入中文昵称");     
9 s7 P. n2 V- q' S2 l& g0 u! A  document.sform1.chineseid.focus();     
+ W; m4 u+ U/ e* f0 \  return false;     1 _8 ^# S; A5 M$ y: R6 }
  }   
. \+ B) f: H) V- k password = document.sform1.password.value;     1 ?9 H7 @# W+ l; I- f7 n$ A( a
if (password == "")     
3 t4 q7 z+ G3 b* m$ D6 \2 L  {     
2 N% H8 \3 o* J  alert("请输入登陆密码");     " d$ C1 g. m( o
  document.sform1.password.focus();     4 J1 U6 `) x' n5 C
  return false;     
# l' q/ s# h( M+ B/ |& i) l' [  } $ ]/ {6 h4 F1 ^4 j4 @# M
password1 = document.sform1.password1.value;     - W* W2 P" r# q8 h. J
if (password>password1)     
$ m- ^: S" g1 _: N' I  {( U& V3 e& i: \: E2 @
     alert("重复密码与登陆密码不相同");     
& x3 u8 {! q1 b8 W' t9 J: V4 m  document.sform1.password.focus();
% I8 z. L) m; N/ {; D, O$ y' i                                document.sform1.password1.focus();     
; ]1 h. i4 k7 W1 C  return false;8 C6 [( n9 `5 [# R9 V$ l7 x
  }  ' I' M4 c+ G2 B  r
if (password<password1)     - e+ M8 D+ i6 ^1 D) ]; G- f# K
  {5 {6 M4 e7 ~. b5 O
     alert("重复密码与登陆密码不相同");     ! @- d4 m7 h3 W' a
  document.sform1.password.focus();
; J4 Y6 m$ \* ]3 p3 C6 k                                document.sform1.password1.focus();     # `8 H0 {% {- [9 Q( t. Y
  return false;& a$ C5 x0 c9 B; L, p+ x& V
  } + g3 i4 ]* c0 \- G  A' x
if (document.sform1.email.value == "")     
0 a! u3 p: z) u  {     
1 T! `, v0 {1 E% w# z+ i) a  alert("请输入您的E-MAIL地址");     
# q" a3 R6 K, ^9 t  document.sform1.email.focus();       u' ]& W; J  L) P1 z' b, ?
  return false;     
5 D8 T& y9 ]) U: K  }     + O) p/ m# Z$ A6 }6 T
      7 H: x) N# P0 [; v* c! S& ]8 s
email=document.sform1.email.value;      
& g" r4 I% m5 D8 _$ S emailerr=0     
7 \: Q/ k+ C1 ^& n* _ for (i=0; i<email.length; i++)     
. U6 B2 A3 F' Z* g6 d  {     ) U4 [4 N. A, ?3 d0 p+ I
  if ((email.charAt(i) == "@") & (email.length > 5))     
, U$ V: Z% b5 _: ^   {     
1 O& q% `5 A+ X* Z- b# G7 X     emailerr=emailerr+1     
; p( Z$ s9 X# Q, i: T: ?% K   }     
% k: \3 A7 n' H3 ]$ Y+ E" {  }     5 R  U( G  R7 G9 u) F- T8 O( Q
if (emailerr != 1)     & T  q9 J3 P& a+ s# L0 q1 G" |3 T' E
  {     
' c  w0 R: ?- t* M$ b  alert("请输入正确的E-MAIL地址");     
! R6 m, V# P: l  document.sform1.email.focus();     * S6 ~0 O5 y1 \
  return false;     
/ P) `1 b; s# |; i  }           9 d7 c( e- y5 K# _2 N% I' ~
  
( T" @% N  A% ~* S6 P9 } if (document.sform1.checkask.value=="") ' d: {9 I5 J! z4 o  O9 g+ R% H
  { - ]8 o0 \$ n: Z! L- Q' G5 P$ K$ t
   alert("密码提示问题不能为空");     8 J4 F7 {% w$ m; t) @
   document.sform1.checkask.focus();     
2 [9 g5 y8 i& }   return false;  0 N  U4 G4 D" t& X/ r5 Y/ F
  } 5 F4 U* @+ M% W# a
if (document.sform1.checkans.value=="")
! }. {( E0 t5 j  { - H' \1 ]- i4 }% m& l$ M) E# r) D
   alert("您的密码提示问题答案不能为空"); 0 K1 i. L5 h& \% `% W+ |; x
   document.sform1.checkans.focus();     
( D% `( ]2 s0 z" B" h. w, V   return false;  
+ b" y# c2 @& P' Q  }
+ |/ ~" p  _0 M/ ~ return true;     ) _1 B) U' B+ A
     0 R# l' M$ C* F* I$ N/ a/ C  Z% Y
} 5 j* v0 u& I9 T9 W2 b
</script>

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