返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:* ?- ?- T& U4 f! C' c. ]1 l
<script language="javascript">     . l, L/ r- ]6 p4 O2 T
function IsDigit(cCheck)     ) @! _, I/ V$ z
{     9 x( O( b/ [3 x5 f% r
return (('0'<=cCheck) && (cCheck<='9'));     
7 ~6 o, z( Q1 {2 [! Z9 l; A }     ! B/ z% o0 s9 q0 o9 R! p
     0 s8 Q: [* l0 U
function IsAlpha(cCheck)     
+ y9 {' P- T+ z% \) \! v- M {     & D5 r1 z" z4 q6 V" G/ z) }6 E
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     # o0 B$ f6 p/ D8 \. R' ^7 v5 c9 \( ^
}              ! E3 _% e/ O& d
      3 `) b8 Q1 g+ n$ b- C
function IsaNull(cCheck)         
8 N8 @7 l' _/ }4 k% Q9 G {          1 Y, O, h  R* Z& C6 A
return(cCheck != " ")         1 O: j1 G  Y4 i5 @1 a& g/ n
}                                 _# `3 i3 k) T) S& W8 y
      
* k0 ]4 h( O/ N5 bfunction checkform()     9 q5 M9 b2 _8 W% y1 A
{
: s9 M( s2 V' t3 i! E  id = document.sform1.id.value;     . x/ E% I5 H3 _' ~) k" Y4 a# b
if (id == "")     4 s' s/ j4 n/ `. `+ I+ w6 M( W
  {     ; ?. p# `) q& J/ k
  alert("请输入注册名");     
: w  B8 G) X& Y# w' x/ \  document.sform1.id.focus();     
- j' Q; m; {6 k3 b1 g7 l  return false;     5 ?1 g4 x, ~2 E' M+ Z
  }     
0 P9 i8 R2 E% E     8 g- P! B: F8 y" M9 M
for (nIndex=0; nIndex<id.length; nIndex++)     . m1 r9 x( {. n% ~1 Y6 O
  {     
7 R& B9 F& z/ `- I9 d# H; l  cCheck = id.charAt(nIndex);     9 d# {1 _: u1 v, W' Q' P7 I/ p6 q0 O1 t
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
& ^9 R2 f8 [. ]; D   {     3 N5 D4 a. l; K  V+ o. V- b* j/ n
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     / g) X" f& R  L9 r7 s) R
   document.sform1.id.focus();     
% j5 p- m) ?! Q* q+ G   return false;     6 X' R4 U! X) e- @0 K' N& O! m
   }     ; W; l) P' v* K9 ~, _3 a' y: L  }
  }
* C; T  l* e- `  chineseid = document.sform1.chineseid.value;     
0 ?! p, [/ u! u: ]7 u6 M7 P if (chineseid == "")     
! G! X5 F: ]6 ~& p  {     
! K) s! e7 U0 @* ~9 a9 ~9 L3 a4 k  alert("请输入中文昵称");     
5 X! p. T1 x* b1 [1 r  document.sform1.chineseid.focus();     ) }" @9 L+ P' {; U
  return false;     3 i0 R0 k, g0 e- X0 h* r; P# B
  }   
5 ]( v! X! p/ i) f" t% H! W, @0 V* ~ password = document.sform1.password.value;     2 i5 D9 u% z5 Y: z5 P2 P# s) I
if (password == "")     $ d* O0 S. j! [2 }' N& U7 o4 j
  {     
0 ^" y5 g$ T+ _, `1 e" _  alert("请输入登陆密码");     & P9 l  R0 m) k5 L; q8 T4 w5 O
  document.sform1.password.focus();     
( e) c4 j3 W8 ^/ x3 M& E4 x8 M4 @$ D  return false;     
- f& T1 w; o. _' X6 y. ]  }
/ v! f' b* ~  L& L% _& C: ^ password1 = document.sform1.password1.value;     
9 K$ f+ D3 {) v if (password>password1)     6 ?4 M0 [! k! G/ j$ H& c7 K
  {% _7 A8 }" W6 Z6 X
     alert("重复密码与登陆密码不相同");     * W/ {3 `( }' [1 ~. c% H" H* F- f
  document.sform1.password.focus(); * x$ \+ {: G# d5 H- h: J( t4 [
                                document.sform1.password1.focus();     
+ v1 l2 t9 S4 Z, T3 {% j  return false;
- J: ~) e/ I3 d. L$ W1 f  }  
  i* Q% n6 P  n" L: Tif (password<password1)     / e' _$ k6 G" E: I3 M
  {" ^- s) C9 i! M: N% k
     alert("重复密码与登陆密码不相同");     
; S2 F# a/ O" Q8 ~) P' V  document.sform1.password.focus();
: L4 O/ V4 D# S, U. t                                document.sform1.password1.focus();     
" @/ i8 ?. \/ m2 w6 R6 p  return false;( ]; A( B6 ]* |" k* Z
  } + V4 Z7 q9 G6 t  U5 m
if (document.sform1.email.value == "")     , z- {4 b5 T4 v2 m( R+ ^
  {     
1 z" P7 s2 J8 _+ Q$ k' u5 W  alert("请输入您的E-MAIL地址");     9 Y# F/ m+ B' p0 A- e
  document.sform1.email.focus();     
5 R' s  \( ]& p; K1 L) |  return false;     : Y/ f: s' {  U, L: q
  }     
/ E: g. y# T" M& }4 T      8 Q/ F) r& }7 z5 J
email=document.sform1.email.value;      % o7 d/ X% M& B
emailerr=0     ! c$ w9 u/ K4 m  m( V* R, u$ [) Y
for (i=0; i<email.length; i++)     
. N. @8 R# e' i' B" b/ \% w  {     4 |( t/ @3 E% z! Z
  if ((email.charAt(i) == "@") & (email.length > 5))     
4 B2 i" D3 Y% U8 T; g   {     
5 O, S2 i; Z8 P3 H     emailerr=emailerr+1     
) G  h( J: ^; {9 {6 e   }     # d) @; w8 s& o) c/ L
  }     % J, d- A! V0 x& Z& K  I  C
if (emailerr != 1)     
# m9 ?& k- H. `9 u' P& f  {     
9 W1 W/ K4 f- g% k  alert("请输入正确的E-MAIL地址");     4 ?; _" B: E; t% \
  document.sform1.email.focus();     
1 L3 \( J$ b( c  return false;     & X" P7 D. x% l/ U! t& ~" Z
  }             m% m4 Q% B* X" f
  
, l4 q& ~1 |, O% k) ?) j if (document.sform1.checkask.value=="") # T6 P; f; t) `! {# s1 u; T" n+ _0 q
  { & Z$ t4 N  O1 o2 k
   alert("密码提示问题不能为空");     
8 U. v7 x  y6 n   document.sform1.checkask.focus();     
3 v( O+ y# n" x) V  d   return false;  
: J. i$ O/ @4 T  }
& C; E, p+ C) V0 e; | if (document.sform1.checkans.value=="")
( Z7 H2 k/ ?1 R# L3 a  {
6 }0 F2 s4 P  P5 o$ ]   alert("您的密码提示问题答案不能为空"); & M* j9 Z; A% T$ U
   document.sform1.checkans.focus();     % G7 v" I" C% N) @- q1 X* h
   return false;  1 R0 b# Z9 \1 @
  }. Y' R" y( f8 ]
return true;     
3 g% b. l0 L5 I( B' ~     
! ?$ j: U6 `" _! ?+ `* S9 ^& S} 8 ~+ T/ D1 s2 l  ?
</script>

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