返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
; E* V7 Y, \+ W) }<script language="javascript">     
1 v0 G+ [) K. P$ Q9 e2 J5 ifunction IsDigit(cCheck)     0 \& t5 k  x9 l, ?4 Y3 w. K0 {
{     
' h! _8 t2 n0 X3 M9 } return (('0'<=cCheck) && (cCheck<='9'));     
0 r* f8 u, F" Y2 s6 R }     
  T4 \" A5 r1 u& Z3 }( z     
0 Y! i4 N9 S1 n; \* ifunction IsAlpha(cCheck)     
3 x6 s/ e, ^% I0 ?& L4 W& K {     
! [# k8 Q6 R& c7 _3 f$ A* ` return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     3 ?7 z% {" x4 s- k+ L$ u
}              " n5 Z% u. |7 N" q& n; z' n
      1 N7 o& c* W- z" ~$ v
function IsaNull(cCheck)          5 C7 B' U* \( Q5 f1 u! [6 Q8 F
{          $ C4 V3 _8 C: r9 S/ c" A: p
return(cCheck != " ")         
/ e, j. g) U, K+ Y; ^ }                               5 p9 g" j- r1 Y& ^
      9 L7 h: y) f8 g6 s6 J1 Z
function checkform()     
( K: Q3 N( {, w  R{ 6 P8 F# \8 z$ r0 N
  id = document.sform1.id.value;     & v6 U& }" G& Z5 P" q3 M" z- }7 Q
if (id == "")     3 |# d9 Y9 r! Y4 B* `$ ?
  {     
: H9 q1 S9 f' n' b' E# [; U5 a  alert("请输入注册名");     
* e6 L- H3 A: w* u9 P3 I  document.sform1.id.focus();     
0 g6 u2 O1 K1 H& U6 v/ P; h6 G9 Q  return false;     
2 Z6 ?8 ]% S2 z$ B% g3 U7 q8 y  }     ) _# p5 A' B, N
     : U' a5 V) ~5 W5 Y8 ^9 e% |
for (nIndex=0; nIndex<id.length; nIndex++)     , C- P: d) u, f# \0 m2 X- V
  {     " ^6 Q5 @0 g9 J4 r- X9 b2 v! |
  cCheck = id.charAt(nIndex);     
2 o8 O6 u3 q' Y8 S  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
* S2 o0 \9 A/ e   {     
9 Q1 z5 x4 G2 R' I4 q& ?   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     / A' J$ A5 a) q6 B" S7 R) C
   document.sform1.id.focus();     ' \/ Y4 K7 l2 X: x7 s
   return false;     
3 N* \; |' v/ N3 t/ F1 D   }     ) t  D% n& I& v
  } ( L/ R/ t2 n/ }) }# e
  chineseid = document.sform1.chineseid.value;     : `( J9 z1 {! Y! _" r) q
if (chineseid == "")     
2 a7 Y) w9 p' b: W. m( e  {     $ g: n  ?% c& Z+ e3 U$ a
  alert("请输入中文昵称");     0 B# o: ?& X) L! g; g  u
  document.sform1.chineseid.focus();     
5 X8 J3 G, r0 K  return false;     : H* N; U: A( S% R2 J, ^2 M# j
  }    ' n7 @' P2 o3 s. A8 i5 s3 r
password = document.sform1.password.value;     
* E, v: f, ], K9 j9 D/ C" {. ] if (password == "")     # |( f# y  L" g* z
  {     ' z! M8 u/ I9 |2 \8 T2 R
  alert("请输入登陆密码");     
9 F) \- H: Y; y4 Q8 g3 x$ `2 P  document.sform1.password.focus();     : ?2 }! _; _) X7 G( ?* a' i/ n
  return false;     0 D1 z; ]! j- A9 z$ w3 ?
  }
& z% `$ x; b' Z* w3 N password1 = document.sform1.password1.value;     
# Y: G+ U6 f! ~7 h if (password>password1)     , h( l( z/ a5 r; L3 \
  {
& P8 [) _: C% w0 [6 s8 }     alert("重复密码与登陆密码不相同");     
$ M# e* ?0 A# x' R/ c& T, n  document.sform1.password.focus();
2 v; s$ J5 e) z$ S' Y, [0 M3 Y: u                                document.sform1.password1.focus();     ( o# B3 @# S* S
  return false;
6 g8 \# ]) v' T1 d0 E  }  
/ @9 C% P& L# i8 E! pif (password<password1)     
3 }; @, l8 E& K  {
* I, v9 b, @- E# C- x! T     alert("重复密码与登陆密码不相同");     
* ?: ~% {$ \( ?) M5 h  v. D7 g7 [, I  document.sform1.password.focus(); 0 a( O$ i# y) |" o
                                document.sform1.password1.focus();     
! h; N+ q7 c% M4 o( t' Y. a  T2 |3 E  return false;
0 A0 F8 Q* A  x5 o- {0 Q  }
* N) D9 J# D0 L& G6 ?' ~ if (document.sform1.email.value == "")     
/ T0 @7 a5 h4 G, z6 {  {     % b% ~8 f0 B4 p6 e1 r
  alert("请输入您的E-MAIL地址");     
; o! E2 t; V. O7 Y/ }7 q" I  document.sform1.email.focus();     ) J9 R+ \: C! f; \( Z: Q* u
  return false;     
8 F* O- L. r+ d9 [  }     
; v% s9 A- y* `$ s# A( X; Y      
3 ]6 B9 V" U. `* X. R+ t+ o email=document.sform1.email.value;      ) f1 ?8 |# Z4 s$ h1 [, w" F
emailerr=0     % N2 g: k, y2 N3 X" p5 R
for (i=0; i<email.length; i++)     
5 C6 C( ^9 U; _9 x! p7 r; T1 s  {     ' P: ?3 ~$ b) G
  if ((email.charAt(i) == "@") & (email.length > 5))     # ]" m# f0 d# p7 h* i2 [
   {     
; I( @9 s* R0 {- Q+ Y6 T! F$ @     emailerr=emailerr+1     9 t1 D# B7 l7 \
   }     
" ^% ?- \0 f7 ?5 r  }     
/ n  e+ H6 g7 Q if (emailerr != 1)     
# |1 V! T/ B) ?; Z' w4 F- ^  {     
1 }1 M' d( C8 h/ J1 T1 B& W  alert("请输入正确的E-MAIL地址");     + i0 w% q: V) q3 i- j
  document.sform1.email.focus();     $ A) C; G- n% K9 u; s
  return false;     3 G% Z' g# A$ K! @/ e
  }           
$ l4 c) C* P; J) ?; B1 @  C; P: Z  & u5 I4 ?7 g( r9 G
if (document.sform1.checkask.value=="")
1 r# R! B  M4 [7 r0 @2 J* w1 X  { . f$ w5 ^0 |: F8 h
   alert("密码提示问题不能为空");     
1 Y9 R, f6 I  T" c   document.sform1.checkask.focus();       D* G& f% C" ^/ L" t5 n2 D: J
   return false;  1 @) m) y! L1 I( ]5 r
  }
) U. ~7 d+ C/ c& O3 o3 B9 F+ q if (document.sform1.checkans.value=="") ( b  ~& i# d" n  e/ l# y
  {
2 E, K6 A/ G0 P6 o$ i0 a1 M/ ~   alert("您的密码提示问题答案不能为空");
" i  ^0 d  x) o3 v* r   document.sform1.checkans.focus();     
* S4 _% f: r$ e8 j   return false;  
: }8 l+ D/ H% z: P- ^! v1 S  }  E+ A* H% ?% V3 ~! y' [1 u8 O7 n
return true;     7 J7 f9 _) A* j, c! h5 L
     
' [4 n* E6 T6 j5 r} 2 F& _7 V1 P- w. Q. F" ~
</script>

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