返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:8 R) }1 T/ J9 v- U3 _
<script language="javascript">     
$ D7 G) `" N) z% @0 Rfunction IsDigit(cCheck)     
0 ]4 W" S8 w2 K6 j  }. `9 m# z {     
$ e0 }$ {. [) w6 u, U# W return (('0'<=cCheck) && (cCheck<='9'));     
4 @8 I* A# w5 ~) a- j }     
" J: c" X( a3 [; t  N     
, h1 h+ c9 c) H7 ufunction IsAlpha(cCheck)     
( o7 }6 ^# t6 w8 t, Q) X {     
" W$ H" I: o9 h" i/ w) N2 m6 X' y! ^ return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     % s- _9 Y+ h4 P7 Y# c
}              
3 S/ M9 ^  i! A0 E7 h) U$ V9 M      ) ~& y3 ~  G1 H0 z  ~" u
function IsaNull(cCheck)         
+ M+ [( e! d3 x2 n& H# Z {         
. Q: Z6 j0 ~0 @( a, @3 Q9 R  d return(cCheck != " ")         7 v/ B. O- [7 w5 \! j
}                              
: g$ L/ h, J2 G3 K5 L& h8 t& T      
, N. c8 N3 B1 ]4 c8 j" e' pfunction checkform()     
6 m$ U( @9 M4 F) Q& b  C{ 1 g) i& h# K- V$ P4 W. a5 ?$ |- r
  id = document.sform1.id.value;     1 `2 H- z! C5 m; Q! B
if (id == "")     
) o6 e9 _3 B- F% m. F& y' W+ j( h  {     
+ Z6 x6 ^+ W7 w( D+ Z  alert("请输入注册名");     
/ z- Y+ d, x4 S) h  document.sform1.id.focus();     1 d% S4 b0 T. m' e/ h
  return false;     # {' S6 v/ O+ ]; a6 B# E0 p( ~
  }     
! i/ {* U+ R' g- L( i8 |     
- b9 ~  o% o! p' g" W% W. ~ for (nIndex=0; nIndex<id.length; nIndex++)     - T* P9 P  |' o7 c9 f5 k1 U
  {     0 i4 B2 w3 b* n  ]
  cCheck = id.charAt(nIndex);     
2 U, [5 d7 b4 D, V$ F6 E  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     6 E0 G9 Q1 S0 x% Q
   {     6 Y5 Z8 k4 j% F$ j6 j4 N  a0 D
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     : h7 D/ t% n  ?- v& [6 X
   document.sform1.id.focus();     , }) ~& B( d/ S8 E
   return false;     ' N1 |% L1 e0 a7 B. N
   }       K* L3 E  H& M
  }
" t3 V* p/ Q( c# p4 N8 s  chineseid = document.sform1.chineseid.value;     
) {9 o7 ~3 t: Q+ G3 @ if (chineseid == "")     5 w- j) F( {# b0 i! S; G' E/ Q. v
  {     8 U; q, a  m5 W
  alert("请输入中文昵称");     , P& P% s# {. T! g
  document.sform1.chineseid.focus();     
0 W4 L- u/ |+ p$ G* l: F  return false;     
3 ?$ B4 p5 h7 s1 f: o% W  }    + v: Z4 `# U" y
password = document.sform1.password.value;     * i) w9 ^9 L( e# _8 Z( k6 F
if (password == "")     
9 y4 A- W7 g4 \" w  {     $ s( ]* v1 j) R' T
  alert("请输入登陆密码");     + o; h! m& U, x8 b
  document.sform1.password.focus();     
! N0 b9 M7 X8 d3 U% [% {  return false;     
. |. E6 I2 U4 u  } % J4 N7 X; U" a8 j
password1 = document.sform1.password1.value;     
* [! [/ a2 j% Z/ q' n/ t$ n' T( R if (password>password1)     ) ^/ m  E6 r$ q3 P9 h! W
  {! p3 t0 m! u* c& e. M5 X
     alert("重复密码与登陆密码不相同");     " e9 I- g. f3 d6 x8 E1 L
  document.sform1.password.focus();
5 c5 V! F1 C  Y7 j. d2 F' Y                                document.sform1.password1.focus();     
6 a. ]% _; T8 l% \; \. j3 [  return false;
* e( U5 L6 y3 `$ d  `$ c  }  + j5 {) h( G# a7 `
if (password<password1)     
2 m: `" k. U0 p/ F& r! H" D' I; l- e  {
' ]4 h- _1 a; e/ V+ r& [     alert("重复密码与登陆密码不相同");     ; j8 {4 f  q2 s% d5 a
  document.sform1.password.focus();
6 I9 E* {' }4 t4 r# {                                document.sform1.password1.focus();     : [# d  F9 d/ n  |# _/ c
  return false;
* I; Q3 U4 i1 p/ f  }
4 A, ~& V' Q+ Z! V if (document.sform1.email.value == "")     3 J8 L% N8 }; U( d1 G1 ^# _
  {     ! d1 O+ A) b3 Q% g3 U4 s
  alert("请输入您的E-MAIL地址");     1 W1 W2 o( u  R. @/ G5 F! R/ l+ |
  document.sform1.email.focus();     
' `) v2 b% ^* V( q; L; g/ }  return false;     
0 S; t% U2 H/ _! j  }     - w. E4 ]: F( s4 c- X5 n
      9 s3 O6 d4 Y3 R/ q/ ^
email=document.sform1.email.value;      ( h5 o" ~1 d# I5 a, F
emailerr=0     9 M4 Y1 ?" G; y: G
for (i=0; i<email.length; i++)     
2 Q  n: g9 ?9 J5 Q, P8 r6 `1 Q  {     
4 ?+ k3 V! @8 m4 g) q* E0 O  if ((email.charAt(i) == "@") & (email.length > 5))     4 p% d% [2 L9 D8 Y0 k9 f
   {     
' a3 ^: Z/ J7 S: [  ], {3 `     emailerr=emailerr+1     " G/ x4 R7 r* m, I/ \0 }& N
   }     
9 ]9 w7 G* o. f1 u! E' |  }     
; K) a  W% ~) t( \ if (emailerr != 1)     . Z) }: o) n5 F3 x
  {     4 ~$ I# O& c2 O* w% D
  alert("请输入正确的E-MAIL地址");     
5 ]# z+ s: }5 i7 I8 R  k  document.sform1.email.focus();     
) J  @( }/ c) ^  return false;     
2 U% A" U" i  v( h  }           
' n- ?6 C0 v. _3 t  
  ~9 [# \5 {" @+ R7 O  a if (document.sform1.checkask.value=="")
) R5 F4 K( t: e$ q  { ; G4 ]6 h/ u  @8 }
   alert("密码提示问题不能为空");     
& k! ?9 Y( X# t1 r6 P   document.sform1.checkask.focus();     ) h+ \, q( M% j$ \
   return false;  , G" A& ~; q! u$ @
  }
: v6 B* z& B" r# M: q. {# ?- L- n- k if (document.sform1.checkans.value=="") ; H4 k) h- z" r( x0 e
  { ; }" n0 Q3 y# E8 @' ], U5 u
   alert("您的密码提示问题答案不能为空");
; U" [% x# L( ^( T* r   document.sform1.checkans.focus();     ' T5 ^  U" m4 \' Q0 }
   return false;  . i/ `% J! D- Y5 d$ P# N
  }! N7 I; Z6 G4 F, M6 k4 Q' ~, H0 t
return true;     ' x( |9 Y: |. \7 e, @
     ' T# E0 j3 e6 D/ r
} . M& R- f/ u* D# b# U4 W
</script>

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