返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
- U7 ~' h! {! h<script language="javascript">     1 W7 X, [7 z" ~2 N) H. ?
function IsDigit(cCheck)     ' W: u( E  o, {: j7 A& c3 j* U
{     
" I. p7 w% N; A- P3 h: n  Q  p return (('0'<=cCheck) && (cCheck<='9'));     
3 a5 \! O1 ]+ t6 T  U% q) K! b }     8 @2 H. o5 v- u" K' F) e( `5 a
     
+ o4 ~. J5 F. r* J7 N" _4 i$ |function IsAlpha(cCheck)     : J9 q8 g8 o! y8 C, f  c, A& q
{     5 s, N4 B$ D" S5 Z
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     . M6 [" p$ Q7 N1 R) p5 H; {
}              / I8 E8 k( M7 r  B6 L/ n$ l" E% ^  s
      
# @1 R, }, e) S5 Ifunction IsaNull(cCheck)          5 j3 c1 |5 l1 p: g
{          % \8 r+ k0 E$ f
return(cCheck != " ")         7 J9 W7 }, B$ g% v3 q" t5 ~
}                               9 g+ A; G& }1 R# j( s9 D
      ; n6 W  ~6 F' V  _  n. ~
function checkform()       k) @7 V7 H8 \! ~5 J
{ 6 \; s( X) U# B" ~: {, Q" A) A3 @
  id = document.sform1.id.value;     
. T( l; R' c3 I* z if (id == "")     & g# H, \! H- U. {* p
  {     5 p' N3 b6 k# W2 ~' c) D9 k2 g1 A! u
  alert("请输入注册名");     ( E, u3 O% Q& `/ g% w" ^5 q
  document.sform1.id.focus();     
% B2 l! H# J  `. h+ a) c% |  return false;     " v# q6 E8 A1 U9 U& T
  }     
4 m3 n9 `, `8 q% I) b# ~     ! F, V9 z/ ?3 \0 s, ^
for (nIndex=0; nIndex<id.length; nIndex++)     
" a' C4 p1 y  L$ r. _: W$ j: I, N4 S  {     ; o0 ^6 F3 c: J4 r( W/ A
  cCheck = id.charAt(nIndex);     
6 _& G  |. e. D' J3 I( A) X  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
& J. x6 {+ p+ l6 T+ _" X   {     
! a9 r, \1 B; O$ r/ _5 |: H- }5 q   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     4 {' t1 Y' C/ {$ N
   document.sform1.id.focus();     ; i4 T6 `5 B/ U
   return false;     
; U2 y" l( ~. v# |5 x   }     5 x+ M* c( |$ M3 g- F$ X0 M. {6 m
  }   B% C1 ]6 b5 t; j2 S* }# p
  chineseid = document.sform1.chineseid.value;     . }; b: N" e# n
if (chineseid == "")     # d+ Z$ n( Q& T, Z1 R- |
  {     
1 ~4 D* n7 [: R, W  alert("请输入中文昵称");     
. ]( b0 F/ `9 X! i, \: e  document.sform1.chineseid.focus();     9 i; c# h6 C1 ~' N
  return false;     4 v1 n+ e" I; ]; y) G1 L4 C. ]
  }    1 a1 }1 R+ R* N
password = document.sform1.password.value;       w' g; E, f2 Y6 ?6 Z( o! i
if (password == "")       O: ]% h8 s5 k6 r2 f% a
  {     " s+ C* x0 [  _( @
  alert("请输入登陆密码");     
  ^7 r- F: P; O( i0 H  document.sform1.password.focus();     : s: C  g  E6 J  l, ]. m
  return false;     
+ F6 {9 Q% n- w  y  } 4 R# a( L" \5 U0 g  [  a5 \" X4 r
password1 = document.sform1.password1.value;     
4 \8 D; e# X* O! B  ]) f if (password>password1)     5 k, S6 B; s1 ~. b. x% z
  {1 L% n3 j- z) a# A
     alert("重复密码与登陆密码不相同");     
$ k4 K/ y) y; H$ R; s' I8 A  e/ z  document.sform1.password.focus(); 5 C6 H4 j6 ^7 x
                                document.sform1.password1.focus();     
: B; A5 a" K% i: `  return false;
/ |9 O! G/ Q& R  }  
$ k/ ^' X+ u6 p: a3 V; ^6 Hif (password<password1)     ! d( j* z2 q" x9 g  @/ D: i
  {/ w5 x6 f, ^; y' s8 I8 w
     alert("重复密码与登陆密码不相同");     
) ]5 [: G1 I3 I  b  document.sform1.password.focus(); 6 x/ i- X6 B# ?3 f% ^5 t/ s
                                document.sform1.password1.focus();     
& u9 z, \! L. Q0 I( e  return false;* q& k% X! R3 \% Y3 W) B- L9 i# F
  } 8 @5 N% L, v/ _! G
if (document.sform1.email.value == "")     
) Q; Y- s1 U, u/ t4 n5 v8 H  {     
4 b8 J6 e3 B) \  alert("请输入您的E-MAIL地址");     ( o) [5 W/ v! Z# o1 A, x
  document.sform1.email.focus();     
9 X# h8 t) I( m  return false;     
+ R% \! E  k3 q9 ^  }     7 P4 L6 y. ]/ Q: y
      1 S0 X; d# Z9 h
email=document.sform1.email.value;      
; d8 w+ Q& W% u4 h emailerr=0     
- t# N$ p+ |) j) k6 \ for (i=0; i<email.length; i++)     
; L( z6 B; S% `! l  ~  {     . Z& j! y, T5 Y) {
  if ((email.charAt(i) == "@") & (email.length > 5))     
0 l" u' b, c- W+ a9 @3 ~+ w   {     
! n. `  B2 j" p2 n- r     emailerr=emailerr+1     $ @8 Y$ `, W5 s# S+ V' B) q
   }     & B$ l% z  @5 F5 x
  }     
- ]0 @( b' F* y  A8 C+ } if (emailerr != 1)     6 X" i) F# M) ^# k! p0 q; a, x
  {     5 H3 a) q/ W# U; I! u. X5 M7 O
  alert("请输入正确的E-MAIL地址");     
/ N2 V* Z" q$ R2 e  document.sform1.email.focus();     3 F3 V: s4 a3 T
  return false;     ( E9 m- R! i2 e, \& \  ~
  }           * _% l+ {8 n1 X, s6 U" f/ L
  
; t5 U! O: W; m. }0 W8 a if (document.sform1.checkask.value=="")
) Y0 P" F0 h) s7 O  { 2 H& k& H- g4 `2 E
   alert("密码提示问题不能为空");     9 R& ^- W  R& G8 H+ i! v
   document.sform1.checkask.focus();     
7 _2 t8 r+ Q3 q8 u   return false;    {- A. }* _2 L! \
  }
( |6 w( z2 _5 t# V, X0 _ if (document.sform1.checkans.value=="")
' v4 G3 o1 |2 \4 g' n  {
0 N9 ^( h# J( |& [( I3 T5 X+ m3 w   alert("您的密码提示问题答案不能为空");
7 f3 s' L4 h7 w# q- _   document.sform1.checkans.focus();     
; D- J. S$ H9 r/ T" Y; G   return false;  
: h1 h0 {! d% L+ e& Y% c  }
; z6 n: R$ q/ n# f/ D return true;     
' }- e! q% _0 r+ s8 r     ) |4 c! }; {1 c# A+ J! B9 Y) U& T, s
}
/ u1 q; l- t: U; S! C0 {</script>

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