返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:+ Z6 t1 G4 x1 G3 a
<script language="javascript">     7 w0 d3 Q1 b% t1 q" s4 m
function IsDigit(cCheck)     
$ k  p& f" i* y5 v- Q# ] {     ) O) M$ w, D; d
return (('0'<=cCheck) && (cCheck<='9'));     
4 y' x" T, ]/ l' I3 Z% ~' J }     
: ?0 ^0 c* d' k9 v9 ~     + j- e% B% _, @* V- t
function IsAlpha(cCheck)     
) k3 Y7 m7 g, j. X {     * U. [3 A$ ~6 k7 \
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     ! W; a; ?0 P) |/ s) g8 t& `+ A
}              
0 D+ n$ `" X: d. m8 Q! w+ h- K      
' x/ k4 D5 D' f  |  B: a* z: Pfunction IsaNull(cCheck)          . ~& }* m1 I. |* D; i8 ~
{          4 T4 O4 M% z# ~4 ?! E! v3 a5 U
return(cCheck != " ")         
4 {8 I7 u) _! a9 Y' }; Q# ] }                               7 r( ^7 f2 U' `- }
      
3 ?/ `9 ?& U5 i" U/ m* g$ Sfunction checkform()     
1 S6 c; e# G( T/ s8 x3 R8 D- K{
& m2 a7 `7 @; t- v- I# ?  id = document.sform1.id.value;     1 X$ e+ x9 J- b9 y9 y& p
if (id == "")     # H  Y4 B# P) J, q) E- f/ b
  {     
0 O! `+ w( S0 ^0 v0 w8 b6 O  alert("请输入注册名");     % v3 {. W8 G8 o! m
  document.sform1.id.focus();     $ q! I) R" I5 [) ?+ b# z4 d
  return false;     
( q: u% `! g6 z& R/ c- O( `  }     ( j2 |$ w8 U  F- g+ [
     3 b% G* s2 x& G. X, H2 w, p
for (nIndex=0; nIndex<id.length; nIndex++)     3 R6 M. @' {6 V" n3 z
  {     
2 |, h  v5 D8 q9 K4 O' G( }* ~2 O  cCheck = id.charAt(nIndex);     ! c" ?6 x; m6 }: Z& k+ I0 c+ y# K& y
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
/ O! w( X1 t6 i# u5 [   {     1 p: `/ H) Q- k# Q0 C! G1 r1 j
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     3 L7 {  G( F0 [- ~( i
   document.sform1.id.focus();       t7 Q# _" G) R: \, B' T# Q
   return false;     
! M% S. ^9 _! K; j   }       V7 P2 _; [, e4 z8 a
  } 4 k# z/ Y+ ~/ D4 @+ A) ]! Z
  chineseid = document.sform1.chineseid.value;     
7 ]5 Y! q) h' G+ M9 D if (chineseid == "")     7 x, S1 A# j/ t% G% l3 x  \" _
  {     0 u! D7 Z' O5 P8 T/ b* V7 i
  alert("请输入中文昵称");     
) i0 Z/ B- g. |% |% i8 b, N  document.sform1.chineseid.focus();     
8 @7 V0 }$ d2 F2 s7 l  return false;     5 q: i8 Z; i/ k/ z3 v8 X
  }   
0 e! A% S; I" }7 s# K, u password = document.sform1.password.value;     ) ^. l, u$ {2 v5 F
if (password == "")     6 F9 [) G% m3 F
  {     + n8 C# ]. M6 x, C' _
  alert("请输入登陆密码");     
( l" u) H# Q0 _6 E) o- ^  document.sform1.password.focus();     " O* J" T* W" {
  return false;     
# }# j6 E+ j6 t. w6 _$ x& [  } $ ~/ O, @* J% F* f
password1 = document.sform1.password1.value;     ( o9 t. ]0 G- g7 c! K
if (password>password1)     / v- b: u, @8 k- }1 e# P
  {
& y+ R, d; w, {     alert("重复密码与登陆密码不相同");     . F5 c/ k6 m2 p7 Y* u5 s, ~+ A
  document.sform1.password.focus(); 0 W( h* a& ~3 ]0 `& i- M7 i# d
                                document.sform1.password1.focus();     : V$ e. P6 W* q& Z
  return false;
; ?% k/ z" S7 y5 ?  }  % J6 I- b/ ~+ l- r
if (password<password1)     
! a8 ^  f$ K2 q" a( u0 a5 P+ D0 y  {* A7 u( i# s8 W8 u
     alert("重复密码与登陆密码不相同");     
! b( b! n9 u. O9 |' ?3 p  document.sform1.password.focus();
1 T, T+ V# r) T: C0 T) e# q                                document.sform1.password1.focus();     
" b: j0 N& M- O! d! R  return false;
3 D: f) c+ c" s( r4 f  }
5 x; k( D+ }+ \' V8 c. \% n. C if (document.sform1.email.value == "")     4 j4 \. [& z+ ^/ b% z2 ^# Z9 K5 u
  {     
' O  T8 D. u) {$ w  alert("请输入您的E-MAIL地址");     : [: |4 [' ~* d/ P. k
  document.sform1.email.focus();     3 S1 i$ A$ p9 }& z8 C5 h
  return false;     2 ~( L, f: a# S2 ^
  }     
6 i' J8 ~# t+ Q8 O0 C* @3 H- Z      
  Z( r. h( K# M9 x* {! r# x email=document.sform1.email.value;      4 X# ^# f( |. g1 i; S, g+ d' ^
emailerr=0     ( n0 j: P' g0 |* H9 @. T" _+ z3 P
for (i=0; i<email.length; i++)     : b! }3 a6 M6 O" f. k; s4 r9 G
  {     , r! i( \  H! U5 y9 V# f& Q9 k
  if ((email.charAt(i) == "@") & (email.length > 5))     + e4 [. G6 e0 p% U+ Q
   {     
- R" D6 R' J, N- x# S! q) ]     emailerr=emailerr+1     2 z* ?3 n5 i+ G, R1 o  Z1 o
   }     2 @  L5 n$ P" o. `- y3 ~9 r; o
  }     ' M5 t/ b, W) B$ E/ m, r
if (emailerr != 1)     
/ M4 b* Q* ~. b+ o  O, N  {     
& v" V4 z2 E7 \0 R: b  alert("请输入正确的E-MAIL地址");     , G- H, e* T& Y
  document.sform1.email.focus();     ' ?- v- g: v9 A% }7 _* w" H
  return false;     
% g. k  O7 C$ E  }           & R* D( o9 \2 e8 b# w& i
  
! u6 b+ e7 C' j" Y9 ~ if (document.sform1.checkask.value=="")
9 p. [% |0 I5 {: r  {
& a' N* ^5 h3 N  e9 Z9 G, T   alert("密码提示问题不能为空");     - G. N! E; l) |& W
   document.sform1.checkask.focus();     
5 T! H& }: P4 B6 Y" g- D   return false;  
) V& e* N) ^& i9 r8 k. C  t# k  }
0 k1 _& K/ t( U( Q# j  E if (document.sform1.checkans.value=="") % Q* j- \5 ?1 c0 H
  {
6 V0 q, E3 u. A3 a" M0 y+ X   alert("您的密码提示问题答案不能为空");
3 U' P: G7 Q# }, c; s! v   document.sform1.checkans.focus();     6 p9 b# W& y0 N7 J( D
   return false;  4 B9 I$ k0 v! t( }
  }
5 R! k( S" ~! W% j4 F+ N  e0 j0 e return true;     1 `( @6 P2 c: e
     
; f6 M7 N3 M, \, j7 j}
& X9 M* h. d9 l- b8 A</script>

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