返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:; K% A6 P1 l" x3 a  g5 w/ H
<script language="javascript">     8 |; \' z6 m1 L, g
function IsDigit(cCheck)     7 x# ?# M; D8 I/ X+ M
{     
- T/ N- L+ W' b return (('0'<=cCheck) && (cCheck<='9'));     
; [) K( g! u- |$ P! k }     ; H9 T5 s! ~7 F+ ]" N- g
     7 P9 p! d6 X* j8 q
function IsAlpha(cCheck)     1 M" v' X6 F  b& s# Y) X# y
{     : w' o: Y3 p3 w2 ~" _% W3 |* c1 D
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     + D& P) i% g3 p& ?  G; z* X
}              
4 M+ N  g/ [( ?* _& t8 x) l. G" ^      
* Q" m% p' k3 x6 ^5 A, Wfunction IsaNull(cCheck)         
5 g, Z/ X- W/ u$ t% l {         
: u( n) p- P& G/ f: Y* @ return(cCheck != " ")         
9 D: q$ o: t! H# m }                               ( ~! N$ T4 Z9 R* N2 V
      
) |! f8 h' ^* s, ~- ~function checkform()     
% e9 l; x( w! j5 V8 d/ j0 w  L& ~: i{ ' Y& j% ?7 s. o. }; c; Z
  id = document.sform1.id.value;     ' ?6 x4 I5 w$ Y. [& P9 ~; L
if (id == "")     : V8 j5 x" Q) a
  {     9 A" l. s+ m( d  k# a9 p; \: k/ V
  alert("请输入注册名");     ( b) {! o* h7 y( O5 h( h# B1 ^
  document.sform1.id.focus();     9 O6 u; R; p, Z' m# ]+ v
  return false;     6 K( [5 V( U4 o
  }     
  y) H' I" x: V) m+ l     
% x6 L$ D% R! ]4 l$ j9 r( o for (nIndex=0; nIndex<id.length; nIndex++)     
8 J2 x0 u. a9 _, e* f  {     + N9 Y" y3 S3 |; P
  cCheck = id.charAt(nIndex);     
. ~6 |8 N3 H; \) S( S6 c  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
7 \: f8 P5 E- A; r2 K/ D   {     
% h& }& y7 b2 e' ?$ z5 F3 S2 s6 o   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
* B+ _$ ~6 D" Q   document.sform1.id.focus();     * i3 c$ G( R" u7 Q" n
   return false;     
. L/ E+ y6 |+ c7 j! {   }     ! P1 k$ v9 s; o- A6 Y2 T
  }
$ I+ Y, c, N/ H/ j& P' A  chineseid = document.sform1.chineseid.value;       c' m# s$ Z# Q" x' v1 Z
if (chineseid == "")     , |6 S0 a0 _9 O+ x  W9 L# O
  {     
  K1 X5 z: s5 E  alert("请输入中文昵称");     
- ?$ Q" ^$ E- L+ Q" c9 k  document.sform1.chineseid.focus();     
: g' T  _4 i2 Z: d0 y9 J5 X  return false;     
  z3 c) O( b; c  Q8 [$ u) H  }    4 G* O/ o0 t. [8 B+ r" r/ n
password = document.sform1.password.value;     4 Y# |, e$ ]5 i, k
if (password == "")     . \' G, J7 |5 b
  {     7 c) p  p4 Q$ O$ [
  alert("请输入登陆密码");     
1 K3 W9 x/ {( O: ^  document.sform1.password.focus();     
' e1 X% q1 G- o/ ~% t+ U  return false;     , W+ J7 r& G3 }3 ~
  }
- S8 n! p# C: A  m  N( l% } password1 = document.sform1.password1.value;     $ n  L0 j9 p+ j( n; {% y& d
if (password>password1)     - p  b2 z+ W* F3 e- |9 A
  {& r! @" b$ D- ]. @' D$ W5 `
     alert("重复密码与登陆密码不相同");     5 N& L2 Q) f$ C1 ^
  document.sform1.password.focus();
7 \# K7 u. R4 l/ U' I                                document.sform1.password1.focus();       e# E" H, P7 m+ C& ~( e* [
  return false;
, _8 U1 ]9 e# r  }  
/ G  A2 E2 Y$ T( s  T* Tif (password<password1)     . a5 Y( s7 w; L/ N2 m! r
  {! D* b1 w( Z+ |9 A" z% s7 M4 q
     alert("重复密码与登陆密码不相同");     
* C/ S1 F. H* _  document.sform1.password.focus(); 9 W+ h' D$ _, r3 R( a; f
                                document.sform1.password1.focus();     # M$ ~6 t2 z6 F( l- A  M1 y
  return false;: w8 c; I  F' [8 G; V
  } 3 D- e* m5 x9 H7 c( i* f7 @, R8 _
if (document.sform1.email.value == "")     
* g, |" n6 }8 v; q! b) T: C  {     
% X* [7 Q& O6 l: |$ U) i+ D/ j  alert("请输入您的E-MAIL地址");     " L  h8 Q+ V. @: L2 |
  document.sform1.email.focus();     
+ {6 G) l/ A3 o+ H  return false;     
& U+ C, |2 m  h. Z  }     
% \0 A9 s( W9 A0 s2 C2 ~9 a! f      : r) M4 n& n. W6 V" u5 x  t
email=document.sform1.email.value;      
, g  T+ ]- y* ]& i" K emailerr=0     
0 L' `  T1 h% ^  H: M for (i=0; i<email.length; i++)     
7 |" t3 K" |2 X7 d) @( x  m; \  {     
& ^" P3 N+ t% l- E( [5 V- F# T  if ((email.charAt(i) == "@") & (email.length > 5))     
3 Q+ x0 Z0 A/ x# P2 s   {     
$ R/ o8 x8 Y+ r$ N% y) \2 ~5 O0 H     emailerr=emailerr+1     
! o+ j, S- }, ?$ v6 _2 k   }     4 _$ N' {' ~( M; V
  }     
% W3 i+ t8 C  \- U- s+ [/ |  E if (emailerr != 1)     : R/ Z7 _) t& D- p
  {     ' ?+ m- E) ?5 f* f/ n
  alert("请输入正确的E-MAIL地址");     
- S3 g% Z2 l4 ?  a, g4 W& H% A  document.sform1.email.focus();     7 w: G, z0 {+ ?( J) ?( w5 r- I0 f
  return false;     : K- r2 a0 k" V) V/ f
  }           & I; N& D2 W" w, ]
  . o( U8 U1 r% O2 r. M; }
if (document.sform1.checkask.value=="") $ ^# @* z: o& G7 [3 y; ~+ a
  { ; c% T5 X! L: T% F# q& S- d9 r" S
   alert("密码提示问题不能为空");     ; O5 N$ b; g' Z3 b# g
   document.sform1.checkask.focus();     
- a: `' H; H! M" J& D, K8 e8 S" z   return false;  1 k; E6 O' W6 O/ j( T
  }
6 n( x. J3 ?4 o! ?6 ~: h if (document.sform1.checkans.value=="")
% S- H4 n; v+ j: m5 {( N3 e  {
# H$ @3 W3 E7 B2 f   alert("您的密码提示问题答案不能为空"); 2 I4 S* V0 a# |: S5 S% d: R
   document.sform1.checkans.focus();     1 @7 \/ x" r. T. e0 K" \
   return false;  
% u8 i- [( e4 k  }
1 e, K+ r9 a4 y  n, }- y% c return true;     : D, _% l+ L0 T, |/ H. w
     $ W, l9 h: [8 V' }) ^& y  O
} # p3 A# Q3 ~, B. [. S
</script>

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