返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:& d' A  J( O" W5 F" L
<script language="javascript">     
2 r! |, `8 M6 f+ T. u. a" i, \function IsDigit(cCheck)     
) w' f1 H& C: g, } {     
  s0 h$ n2 Y' E. N return (('0'<=cCheck) && (cCheck<='9'));     
- a7 R/ b3 A7 U }     " e5 w8 \. w! K7 \  P
     8 P+ B; U) B; P& q6 [9 ~
function IsAlpha(cCheck)     4 N9 u" c) r0 w" s6 k
{     
3 b+ y* N) p9 K8 K# q. z  q return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     * R7 w$ Q& a; ?' o% j
}              
; ?+ k' j! C$ r& T      9 P: u# G1 W4 w" |% A2 s$ O( W, D
function IsaNull(cCheck)         
' ?& l$ @2 r2 D+ U  A: B: C4 W1 [ {         
5 x3 U% a9 D' b: u9 _ return(cCheck != " ")         
, V$ T) c9 R* R }                              
; s! i1 T- y; Y4 q; _$ i, P4 B      
& w  T9 D% B5 `function checkform()     
; y# n$ X. d: l{
5 S1 u8 \& d, V+ ]7 g, B2 Q7 f) @  id = document.sform1.id.value;     ; q! l5 U. f* x% B7 t* k7 M) l. e& T
if (id == "")     
9 R* O/ s# o$ r& Q; B  {       _7 Z3 ~3 H& h
  alert("请输入注册名");     7 t3 E2 J2 z8 u5 u* V
  document.sform1.id.focus();     
3 Y7 t* Q* t" g3 V, d7 p. J  return false;     
+ D  M: W' a- s4 ?  v  }     
9 m+ ]; Z; o7 [2 G7 t, F# L% Z     
) b, G) Z1 W( }, Q for (nIndex=0; nIndex<id.length; nIndex++)     
1 |) N0 n% g9 S  {     , n7 y1 _3 Q" V2 f* O3 i2 X
  cCheck = id.charAt(nIndex);     
( y; h4 r- |  U6 r  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
& X6 @  y$ n  W: _- V   {     1 \7 ]$ W/ I1 A. t( P
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
+ ?* E$ Y/ z( I+ c8 y1 q. e8 R   document.sform1.id.focus();     " r7 Z5 G- J( [$ p0 f( h
   return false;     : p& S8 s/ @0 B+ Y' G0 ]( H* g( M
   }     
5 B+ e. }+ J7 ?' D' o! T  }
6 O0 Q" Z* R2 z. H8 n  chineseid = document.sform1.chineseid.value;     
. A+ h1 v" [: q0 C7 c if (chineseid == "")     $ h0 e: S. I& [
  {     
6 `" |$ V0 I+ N# p5 n1 T  alert("请输入中文昵称");     : y1 H' H' \4 S4 s, F
  document.sform1.chineseid.focus();     
" G) n# j4 @- U( |5 }  return false;     
1 _* ~6 V$ I4 k) h/ d8 e$ V  }   
* r  H. n0 n6 r' K2 p' a2 l password = document.sform1.password.value;     
* s9 B; y2 V% A7 _  J4 Q& } if (password == "")     # ~$ x& L0 L' [! v. t5 h" Z1 H* w
  {     $ v  H- A# I5 U# o% V$ a+ m
  alert("请输入登陆密码");     % {' R9 S$ o8 @0 n  ~$ D
  document.sform1.password.focus();     
; s/ x: h0 W  I( c0 H, B/ k6 D. @  return false;     
- }: `5 o: F+ S+ [  } ' @" N1 w/ V, G
password1 = document.sform1.password1.value;     
1 k8 }3 c6 L  N, V( B6 X/ [ if (password>password1)     
" b; G. T7 @4 S" k+ P0 q3 I: U  {
; A6 M3 m- o8 |- \  b     alert("重复密码与登陆密码不相同");     
. l* N, e. q3 J0 q3 R  document.sform1.password.focus(); 6 y, s; u  L$ q+ F, h
                                document.sform1.password1.focus();     
4 p1 m& {  W+ m  return false;
1 K$ s  V! I. b7 I) `/ p  }  ; ^) Y) b8 G5 ~+ b! b
if (password<password1)     ; b" y) D6 r! h
  {1 h# W) n( a8 F5 V' C& t
     alert("重复密码与登陆密码不相同");     1 d3 e$ k( m* q' `
  document.sform1.password.focus(); 4 @! T: N$ a% p
                                document.sform1.password1.focus();     7 {( m9 {& x" M1 q
  return false;
7 g9 l2 W  V5 R( T  }
. x% Z6 z) e7 o) C0 J3 Q/ ~' s* ` if (document.sform1.email.value == "")     ' X& L7 H6 p1 S4 p3 O* R
  {       I: ?; `/ w, a, Z; n  B7 b" y
  alert("请输入您的E-MAIL地址");     . Z9 @  g! V- _8 ^) g/ Z
  document.sform1.email.focus();     
$ b2 N8 ~- P) b! R  return false;     
: I; s( g$ l' D, k+ p4 W  }     8 `1 {9 F4 g# w
      , ^% j" O, `; P3 m7 ^( z9 }
email=document.sform1.email.value;      
8 q0 z- e1 M3 Y) p3 V emailerr=0     
/ c0 m9 [& `) ~ for (i=0; i<email.length; i++)     7 a- T5 C& }% ?$ X. V
  {     
7 [! K7 z! Q$ O/ Z* i9 @. D  if ((email.charAt(i) == "@") & (email.length > 5))     6 y) a5 E# |1 b! w" z
   {     
3 `" P: z# g0 U, W     emailerr=emailerr+1     3 f3 y6 f5 S# |) K0 V4 F5 b6 J
   }     % l' D$ Y* {% r1 y/ o4 o
  }     
. F1 h  d! f8 s: U# x if (emailerr != 1)     
- Y, M9 g  U/ F; U: h  {     + @& F( F. l" D; h& R4 `
  alert("请输入正确的E-MAIL地址");     
% Z' O9 e2 g- F3 y  document.sform1.email.focus();     ; A) t8 u4 t3 e  i
  return false;     + O5 y0 t+ F) I( i1 V/ [7 e
  }           
! h( f3 W6 W( e* f) Z  5 |: a- y, }, z% N' O! Y) n, c8 |
if (document.sform1.checkask.value=="")
8 _) N8 X" C: Q. g) g& x* q  {
% i0 l* n4 ~( V9 P. l   alert("密码提示问题不能为空");     & l/ h! F! d$ e3 [  [3 Z
   document.sform1.checkask.focus();     
3 Q' H6 L. K! a: Z. T- V6 g) l   return false;  * }. S( l& Q* b' Y( F* \
  }
& O0 ?6 W; E' w$ u, b if (document.sform1.checkans.value=="") ' a! L( t" w1 ^9 S" U  B2 b
  { 8 W5 n! g& O% U5 w" f" w
   alert("您的密码提示问题答案不能为空");
. D& r0 a* h* Z   document.sform1.checkans.focus();     3 o) ^) H. _9 ]# {5 |5 @
   return false;  
- x) }& {9 w2 r* G, D  }
( D( t0 R2 Z  Q4 y3 G& Q' Z' L return true;     
3 h/ |) X1 G0 N$ w6 o" o# c     
! x4 M' Q' M2 X. ~9 d0 [" i} 2 ^0 _. |4 j% Q/ b7 _% q
</script>

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