返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:4 m' C! B- O4 e1 h& W
<script language="javascript">     
/ Z- R+ z, X# g* N$ Z- M. Jfunction IsDigit(cCheck)     4 D, b' @8 q4 t+ L0 x- p
{     
5 g% `0 Q+ Z! F# L- n3 O$ n return (('0'<=cCheck) && (cCheck<='9'));     
$ r9 f" b/ L! g" E: l- D/ O }     + J9 N2 Q$ P. }2 d2 Y
     
, Q6 `) {$ a# k) vfunction IsAlpha(cCheck)     / q/ Z% p& w+ e( Q
{     
% i& ^6 k: B. o* \0 {0 y return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     % x8 o. s' g5 v4 F* U; {/ A
}              + l- O2 {" X2 ?9 y4 B
      
) S9 }1 e; C$ r2 Jfunction IsaNull(cCheck)         
& {, k4 Z; }* _7 P {          4 ?% V. d6 u: u& L( `0 _
return(cCheck != " ")         ! F* f/ O# n/ w& t3 ^1 y
}                              
, v5 G0 J) o+ ?& X' x: [, L; O      2 Y2 a# ~1 d& M  B3 q+ |: k
function checkform()     % R9 K6 Q3 a5 s8 J
{
* }% o: Z3 W0 Z3 {8 b  id = document.sform1.id.value;     , h; S" X6 \4 z: `
if (id == "")     ! f1 B) d" }; @2 k5 [( B
  {     
$ v2 J7 a0 w2 F; t2 a5 X) ]3 r* J! l  alert("请输入注册名");     
% G4 f: ?: y9 O( x; U) b+ x( K1 h  document.sform1.id.focus();     
$ E6 O+ }; I6 j% s9 d' _1 V) Q+ z  return false;     & c" Y) s2 G0 f, y! \4 e
  }     
* A' O' Z- M2 e* N# J3 b$ A2 m     
  t( q6 K- C; p& ]1 K  x0 @ for (nIndex=0; nIndex<id.length; nIndex++)     / R# _$ i" F. k& a/ r( z9 c* q' S
  {     : b: h. L$ t( u8 d& _- Q# \
  cCheck = id.charAt(nIndex);     
5 k; S9 j6 g$ X1 w0 C$ ]) l  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     & l9 L1 {' ~, b) m/ s9 n, x+ O
   {     7 y2 C0 ?( }1 }8 u: f% R
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     $ D( K) Z$ e; o$ [5 b6 ?
   document.sform1.id.focus();     
; x! @1 J6 W# h! j- Y) [0 o   return false;     3 {" b. C' Z$ B- c4 z7 w# Z
   }     
+ H3 m* B5 G5 ^. Z6 f  } * N9 d' L/ Z- z& ^" h& N6 D+ c+ R* X
  chineseid = document.sform1.chineseid.value;     . G) X7 c5 k. k) j0 p; [
if (chineseid == "")     
0 R! w) ^) q5 B! v0 |  {     
" u, u( v( W: H/ I8 K/ e4 c6 w  U6 f  alert("请输入中文昵称");     + ~4 j) J0 Z9 p
  document.sform1.chineseid.focus();     
$ f1 _+ j! l( v* g9 P  _, w  return false;     
3 f3 y4 d3 ?5 K  }    - h! L2 a( |3 V, l% |
password = document.sform1.password.value;     ! ~% Y: n2 S* g- ^' w
if (password == "")     2 [6 |1 t. M# q" g
  {     
5 l& Q* j' Z1 g% v2 N# s9 q( y  alert("请输入登陆密码");     # j; c' f9 ^/ h2 E
  document.sform1.password.focus();     
6 x! t7 b3 k% X, ~4 r1 O/ G+ R% U! G  return false;     
, C4 F2 v; c% k/ n  f% K8 L  }
' i5 }  I( p% ?$ a: t$ D% R) d password1 = document.sform1.password1.value;     
+ v" m9 n, {$ C0 @+ x$ E. u if (password>password1)     ! }% h+ _4 P8 t: m$ N# ~
  {
9 x- ^; J$ |- [! |8 p, G0 o5 ?9 d     alert("重复密码与登陆密码不相同");     
8 l- ]7 p9 U  Q6 C1 B  document.sform1.password.focus();
& l% G# G9 a, l7 w3 g) N; v0 Y                                document.sform1.password1.focus();     8 T2 r8 c( V7 @8 ~9 @
  return false;1 k  b4 j3 R3 q3 n: u$ d' N: U
  }  9 \+ k. z' J9 o9 a3 a
if (password<password1)     
2 r( {1 r- X" s1 Y8 J/ o+ e  {
- W6 o; o  ?1 H- A4 i7 O6 Y     alert("重复密码与登陆密码不相同");     
6 C- {6 x& E7 ~" N  document.sform1.password.focus(); 7 k7 R) k& W7 F3 ]/ Z8 _& B
                                document.sform1.password1.focus();     . m: |* Z' F% W. ], b
  return false;$ E! b' v( X# G4 H) N- `- W# C
  }
+ [+ A1 g) ^) e. X- s. \- O+ h if (document.sform1.email.value == "")     ; B1 `5 C6 k& h9 i8 s7 [) Q& q
  {     
& B  q; ]& f, \4 X+ W  alert("请输入您的E-MAIL地址");     2 w4 H) k9 }- R8 g  z- Y& ~. ?8 `  `9 U7 Z
  document.sform1.email.focus();     
# X4 u5 ]6 j9 l" l  return false;     
( n  L( ^7 i0 l8 o+ ]0 g! S  }     5 e# G- i* P; _+ L
      + X- t2 f/ F8 V
email=document.sform1.email.value;      , \2 F" G! c5 }' Z$ L% L
emailerr=0     " \. n- b4 \3 l, J9 S5 W
for (i=0; i<email.length; i++)     . S* F# M3 H1 S% X
  {     6 f7 c% {+ h1 Y' p
  if ((email.charAt(i) == "@") & (email.length > 5))     $ d# G: m- Z# E
   {     " O! m) E- n4 v% D+ u; u7 h. q
     emailerr=emailerr+1     6 D+ r5 k  ?! ?1 Y
   }     
* h4 f. D: d  g6 i9 ]3 j  }     
4 d! @" `% z: ?* l if (emailerr != 1)     
5 m8 ]8 ?8 u. o1 {  {     . d; C8 A. `6 k  l
  alert("请输入正确的E-MAIL地址");     
6 X, d2 ?, q4 U. U  document.sform1.email.focus();     
- i5 v2 d4 G2 I. O6 Z; }  return false;     
" ?( b2 \6 d* P( d. \9 K; j: @  }           
8 F0 }: Q5 t+ D- f4 D5 P/ A  
8 E3 y3 i* W/ c+ G9 n if (document.sform1.checkask.value=="") - W  a# [% f# ?
  { , U/ ]  u; f0 J7 I+ m& l$ w4 F8 k) E
   alert("密码提示问题不能为空");     4 q0 R1 @. M% F7 @6 C# x$ i
   document.sform1.checkask.focus();     
0 t* c& E4 V# l   return false;  
  H9 {+ ?- m* E6 Y  c# t  }
/ ^, v4 ^1 N4 E if (document.sform1.checkans.value=="") 2 U  {( e. E: Q" U( f2 l# a
  {
) f9 y: F, x9 ]3 \1 I) i, X. q   alert("您的密码提示问题答案不能为空");
/ T' C0 H: a5 Y: o' l   document.sform1.checkans.focus();     ' G/ e1 \+ K# D" k
   return false;  
! e9 t5 a! q' s' t  }
; }9 K0 G2 G0 _5 Z( E2 ~8 X0 E0 G return true;     5 P- q: Y  s! j, J! v
     1 K0 J) h- A1 x2 \2 |  W) [. a& C8 P) E
} & {$ e; B: Q/ c8 i! L4 Y+ V( R' \4 r
</script>

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