返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
! v+ d' e7 \; }- N<script language="javascript">     7 p) ^# {" o7 W7 D1 z7 k! t
function IsDigit(cCheck)     % {% q: t; N6 c7 u0 ?
{     
! V0 l, T. q: H5 Q return (('0'<=cCheck) && (cCheck<='9'));     , ]1 D+ {. O7 t) U1 a0 k& _
}     ! n7 c. G) X- m5 a7 V
     9 i4 O8 V0 @+ U/ u1 c: z+ c
function IsAlpha(cCheck)     * C7 A0 K3 b0 ]" t  C7 F* P& x7 ^
{     
& W  p/ Q2 j  }; n return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
- N( X0 q; q- U: ?3 G, H/ }7 c }              
6 f6 H/ c9 G1 D, q- z  x      4 G" J# }7 Q) P; |3 z0 w
function IsaNull(cCheck)          7 t9 S$ z& I# Z/ @4 b
{         
* F% l- [8 l7 l& G7 i- U! i return(cCheck != " ")         ( A# W$ R4 O2 c& d2 W$ S
}                               3 p% [$ D4 N# S9 m/ C( e
      5 W. l* x! m+ |8 q, L- G
function checkform()     
( L2 b$ j# h" d8 F{
2 o( I" m& M& Z4 h6 X  id = document.sform1.id.value;     
5 D& {4 h" q$ \: {: t( B1 c+ J! L if (id == "")     6 N# ^. p) a. F2 ?
  {     
; N  O: |0 I! H7 ?6 ?" q, r  alert("请输入注册名");     ; W- e5 b6 P  b9 o$ ~, A8 D
  document.sform1.id.focus();     
# M8 C/ I) t( \9 Q& T9 p1 `  return false;     
- F# Z; V% r/ [% \  }     & n, W. O9 a. M! t9 @; P7 j, @
     7 V0 ?  r, P& d9 z9 R' F
for (nIndex=0; nIndex<id.length; nIndex++)     
. r. J# A- q2 w# P! k& O  {     
9 s0 l* W3 Q' F  cCheck = id.charAt(nIndex);     
. R/ Z" D* B8 x# d: ]5 ~  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
3 u: ]# D8 |# j& Y8 q. E: [   {     
/ o1 R& u! O: u  h" B* H; A& u   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ' N( e- I# ]$ e2 U5 ]1 i$ x
   document.sform1.id.focus();     & h7 R$ l' X; U9 r
   return false;     
7 Z* `% R$ L, W* y% N   }     
' Q1 I7 B0 h$ N( Y5 h$ v  }
2 f. f9 J9 q% v( y. u0 j3 V  chineseid = document.sform1.chineseid.value;     
' E; l9 u6 K# ]* f if (chineseid == "")     + K* f9 H2 I3 U8 @
  {     , x3 K7 g: j4 u+ e, p: o
  alert("请输入中文昵称");     9 }( O9 x! ~' s' H- [7 _# L
  document.sform1.chineseid.focus();     
% D! U; @$ b0 c# X+ T  return false;     7 E- I0 N& c$ s, ]  Q, A/ S
  }   
- G9 l( r. Y9 G/ {! S6 U password = document.sform1.password.value;     
4 p! Q" L% t$ d9 \$ g5 D: A. ~# ~. f if (password == "")     
- Q5 P: i+ J8 W- H* d6 F) A# o  i  u  {     
; |" d% y# L9 o# M* h; [  alert("请输入登陆密码");     
' H7 J6 d/ t9 q/ @  document.sform1.password.focus();     
6 k/ H. K  f4 E; n2 {  return false;     
$ U+ L( J; C9 Q) j2 i0 S* F; [  } ' y# C4 C, ^9 f4 v6 W4 @4 `5 i' j% U
password1 = document.sform1.password1.value;     
  S7 Z5 N1 ]! @ if (password>password1)     
( o7 M! b8 A6 m3 h  {4 \# L9 _; z6 I8 Y/ L* X
     alert("重复密码与登陆密码不相同");     5 V* z% r+ ^: T+ L0 |4 b  H2 ?
  document.sform1.password.focus();   N/ l: M+ H/ }  ~# I3 V' E* y
                                document.sform1.password1.focus();     
' ?# X  o5 r3 G% G+ F  return false;
9 G" a, p1 |' Y3 R1 L$ D$ B  }  
* `* j& d, B2 nif (password<password1)     
9 t: I- \: E4 N) ~, Y* R" U  {/ K% |! v/ e3 E+ f1 D' Q, B
     alert("重复密码与登陆密码不相同");     7 x) ^, U2 M) H1 @% r
  document.sform1.password.focus();
5 z5 E& v$ c, @                                document.sform1.password1.focus();     
* g  a7 ?3 Z1 r+ P9 s+ }6 }& D  return false;! k1 }4 X. S& f9 z
  }
" z- f- w$ Q) B4 c3 F# k( \ if (document.sform1.email.value == "")     7 J6 I( I' T; V/ r" g, u
  {     
- [; c8 u. |0 E: T( H' O0 s! j  alert("请输入您的E-MAIL地址");     % O6 a  N" P% q6 ~2 l
  document.sform1.email.focus();     ) B1 b* u1 k  [  _, V! T
  return false;     - q8 @+ j8 H1 r  W4 J3 y7 `+ _- t
  }       R0 b! X) {' O
      
/ c* z- h5 y% {  _& _ email=document.sform1.email.value;      # r" \* |" W- [
emailerr=0     
  m  M4 D3 V3 n+ r for (i=0; i<email.length; i++)     ' k& `( u0 N- b5 N4 z
  {     / |8 l$ Q7 F5 i" r4 C, j& H
  if ((email.charAt(i) == "@") & (email.length > 5))     
6 S& S$ f$ P7 p0 U* d   {     8 g+ O4 n/ H( g0 x
     emailerr=emailerr+1     * i- w; S" o  h6 X0 b# w' m
   }     
/ A7 q( y5 }: V3 }) U  }     6 g7 f/ C9 V+ u3 o) q
if (emailerr != 1)     2 L- F0 e* j2 |6 u& h4 a  Y
  {     
2 Z( ]6 g" ], Q1 ^' j! I4 Y- p  alert("请输入正确的E-MAIL地址");     
7 f/ }+ r/ R' t4 D. n' A. s  document.sform1.email.focus();     
9 y3 _- M( V; t* _  return false;     
* ]( H: y; s- B. I1 K& |9 X7 H6 f  }           * m5 h  I, X6 U0 w6 e! K
  
! ~( z# L  P5 r6 H6 z- e1 o if (document.sform1.checkask.value=="")
1 V( x; {$ ~7 ~; ?$ h1 P$ x: g) |  {
5 J3 L! b3 h3 n! L0 ?8 t, l   alert("密码提示问题不能为空");     
0 I5 p) x' i' g& A( t# w   document.sform1.checkask.focus();     
" q, H4 j) y3 z3 Y9 P: f) H   return false;  
1 L: h& H1 x9 y9 B0 x  }
( n; J; Q& O7 Z; O if (document.sform1.checkans.value=="")
$ q1 {2 U' R- l: [. P  { . C( F7 b2 _$ J6 P' v
   alert("您的密码提示问题答案不能为空");
) q7 C2 E' A! |. Y+ I! i   document.sform1.checkans.focus();     
+ b8 ^, X4 h) z. b* X   return false;  
, l/ ?, f8 g/ m- _# f% B  }
' |3 S! h. ^7 Q+ X return true;     
7 |' w- I1 \1 D     
' i& E2 t. q4 b: k} 1 |2 F- `  b4 `
</script>

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