返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:" _! B) I. I0 W& p4 G* @
<script language="javascript">     
. W) B; z3 l6 I: ~9 J4 f$ n7 Afunction IsDigit(cCheck)     
8 l5 g9 d7 z4 u2 A* U  Z' ]: R4 P {     
9 D# y4 g; V% G, w return (('0'<=cCheck) && (cCheck<='9'));     
% n3 H* W( [5 {% s. w3 f }     
9 e" J8 ?% ]4 c  ?     
7 c% |9 a+ f0 t8 d  Z, H, N' Dfunction IsAlpha(cCheck)     4 k& s9 h: Z8 i" Q
{     
0 ]0 `! k0 R) J return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     1 R; ^6 `4 N! ?
}              / O; l, n; v8 n* m1 o: B* d4 }
      2 e1 A1 ]) |* O/ F- T( \& @, b9 R9 B3 Y
function IsaNull(cCheck)          ) s7 u. C1 \6 A& g  U) w% Z7 `6 J
{         
* p+ y6 I2 V8 }4 w/ e return(cCheck != " ")         
4 e8 o) ?9 v* {+ p% R; l- a' ` }                              
: v" v0 I3 v' k! Z! N2 g      ' Y0 e4 e1 J/ p9 t
function checkform()     
1 s* v5 w, r2 H' v  Y4 |. H( E{ 7 N: V; X5 V2 X' Y" N8 ]
  id = document.sform1.id.value;     ! o5 ~4 i5 G8 G; `2 X9 d) {/ z
if (id == "")     ; [, [9 Z3 U+ ?* T9 k6 {
  {     
! C+ `0 g+ ]( D  }  o6 |# l4 n  alert("请输入注册名");     
8 D; ^0 d) O3 q- ?. X! t  document.sform1.id.focus();     
9 b8 T- a0 C& [  return false;     ' X9 G" U) ^  m; `5 v) c' _9 h
  }     * y) y) N$ _- Q
     . Q7 W$ e9 I1 {, R: k% A, |* H
for (nIndex=0; nIndex<id.length; nIndex++)     8 B2 U. c% ?' f# V7 ~% O* a  Y
  {     
+ \2 `) e- a5 }. c5 o) ~  cCheck = id.charAt(nIndex);     ' W; x+ U7 y9 F6 `- [8 V
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
+ k0 l4 B1 Z+ t" A! D$ |   {     
6 ?9 g2 o0 c+ Z( m' R" R   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
: M5 n1 y, U- R5 Q   document.sform1.id.focus();     
" D4 \; k, m5 `   return false;     
3 V" J: b$ [( G; k+ F   }     
& q1 |0 j0 J0 x. j) ~  }
+ Y8 ]' \4 j8 i5 X9 ^' [  F  chineseid = document.sform1.chineseid.value;     $ t% {7 ]1 X2 [  r) c6 Y3 `7 v/ h; U
if (chineseid == "")     
3 k0 @4 d) p1 N9 d, }6 q! w  {     / Z) [/ G8 f" S  R- h) m
  alert("请输入中文昵称");     
( y1 W' g. _3 m* ?# u; ~  document.sform1.chineseid.focus();     * T! G) w+ l) [  m+ E0 T! G* ~5 ^
  return false;     2 Q, u4 g& T3 b0 w
  }    ! ^; Z7 ?2 u8 m  E6 O
password = document.sform1.password.value;     1 C) S4 a; d+ h) e
if (password == "")     
5 {" U  K% C6 y' m' k% o1 l) k  {     
/ Z2 w; q( ]4 f# S  alert("请输入登陆密码");     2 O3 L, h4 `, g# X6 G" s
  document.sform1.password.focus();     
. M/ I8 h: H* o  return false;     $ k1 n! S" }+ }
  } 5 l( e6 ?/ j3 g8 I% [9 k$ O
password1 = document.sform1.password1.value;     
( V. ?; y! C; N: S4 } if (password>password1)     
; P$ L( W' @( J( E) r  {
9 j, [5 x. X% z1 N8 G     alert("重复密码与登陆密码不相同");     ; q' Q% I- Z- Y/ p
  document.sform1.password.focus(); : ^" x( H3 Y# P1 }+ l) ]
                                document.sform1.password1.focus();     + f# r( x% o9 }$ q$ L+ ^; l
  return false;) M& t! f5 ~7 Z5 W- g
  }  4 i1 \7 G2 W: Y6 E) k% C- J
if (password<password1)     
3 R: p" k  q0 |( [" x  {
: w9 C( N9 [$ `     alert("重复密码与登陆密码不相同");     
- m, |+ S7 d, Q5 S7 j" n  document.sform1.password.focus();
; m7 U5 q( w$ z7 a' ^' O                                document.sform1.password1.focus();     4 u' R4 ~1 t8 Z9 K' K! W1 H# S0 B' m
  return false;7 P" l3 Z# [+ O9 [, z7 f
  }   a1 }; b8 F2 ^" z
if (document.sform1.email.value == "")     3 A2 A' @* K- a4 b/ W- ~
  {     % v+ k# c, u4 W! L
  alert("请输入您的E-MAIL地址");     6 E. x- c7 {! y9 q% A
  document.sform1.email.focus();     
% Q) A! N! o# @' L6 g) f2 f  return false;     
  e' ?3 M2 t- l+ t4 r  }     ! v4 O6 n% j; r2 G$ D, Z% G+ b
      ! D' B% f6 W. U; |; _( l+ h
email=document.sform1.email.value;      
  X# U5 G' ~" v  s" h3 d+ v5 s' [# D emailerr=0     # ]- R- M* R. }/ K/ M+ c: ]+ K& Q
for (i=0; i<email.length; i++)     1 E+ u: h1 }4 @9 z4 U$ `1 X$ {
  {     
( k( I7 R4 m, E: |" r: Z  if ((email.charAt(i) == "@") & (email.length > 5))     ( `2 Q' r& O: F; }' p5 P6 y
   {     
  C% C# {/ f4 G& |5 _     emailerr=emailerr+1     # }& a. d# z# a
   }     / _" t5 k) T9 b- v5 p* ~, c; V
  }     $ h" A3 k  ]9 t- \
if (emailerr != 1)     ! p% E( ]- y0 Y$ `' o
  {     % G  E8 r7 }% M! I
  alert("请输入正确的E-MAIL地址");     . X( l  p" w6 M
  document.sform1.email.focus();     / N; C1 \. ~6 I* B) [0 @
  return false;     / I3 |  V+ \4 r) w$ v
  }           / y  K; ^) q2 r6 R" j
  9 a/ V) }. ]3 f2 b9 P# f2 `& A
if (document.sform1.checkask.value=="")
: a7 P8 G# h8 ^& e! n: X- y0 \  { % R) n9 U5 x, @' F
   alert("密码提示问题不能为空");     
) B( X5 j# q* q; C( W! w   document.sform1.checkask.focus();     
( J7 l1 U* q1 X4 N6 n) `9 p0 U   return false;  
7 c; y- j9 h- ]5 g" Q# G: A  }
3 ?5 [$ l3 o: u! [& J if (document.sform1.checkans.value=="") 9 ]6 O# U. D' C1 i- w7 A# Y( A
  {
  H  J5 g( Y3 m3 P9 l' `   alert("您的密码提示问题答案不能为空"); ' L  k* G# I# ~( f8 s
   document.sform1.checkans.focus();     1 N% D/ g5 T+ x. o* F6 ]/ K
   return false;  
! O% _2 R3 ^) h7 A  v9 @+ Y+ u  }, ^8 O% O6 [5 Y0 E# W5 u
return true;     * l( }/ I" R) J/ K9 f8 s
     
0 g3 @% i8 q) E4 O% F* [} ; z- g% p2 E9 j8 y2 J% o
</script>

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