返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
( a3 p4 d& t: U. J<script language="javascript">     1 Y+ ]$ g. V+ Y6 u7 @0 C" u3 ?
function IsDigit(cCheck)     
2 T. c: H: T- S, x {     ! l! G/ I/ ]+ k* H
return (('0'<=cCheck) && (cCheck<='9'));     ) {" O' W! w9 x- k3 U( L! k
}     
. o8 N" J% ^. o$ z     
+ M: Z) X0 p4 G% F, o0 S4 t. ^4 p8 Dfunction IsAlpha(cCheck)     
! L/ v! c' g- V" k6 l* W3 { {     3 z/ p1 M. Q7 ^: p
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
) D7 r& {- h6 P- M1 S+ N& L }              1 v8 H: I1 h$ u2 O3 h5 ]
      ' [& m$ U; {/ ~3 d8 U( S+ o
function IsaNull(cCheck)          ; ?3 R2 [; `' S1 r& b
{          $ P% Z) y/ c. O9 R& [! ~
return(cCheck != " ")         
9 M3 X# A8 d+ J0 Y- q7 D }                               6 f! p$ U, H+ c* F
      * s9 l( ^3 [2 Q: @4 z
function checkform()     
, N7 u5 p1 r. p8 \! \  e2 g{ : W+ S6 w1 N3 ^9 {9 Z8 m5 l4 j" K
  id = document.sform1.id.value;     $ E* x% U; Z! P8 ]+ ?" E6 [
if (id == "")     
! d9 ?9 v/ C1 s" c  {     
+ E. X, ~5 ]' A6 i  alert("请输入注册名");     
. V' c8 C5 w& V  document.sform1.id.focus();     ; R, \  z$ }0 N& V  M1 w& x; h2 t
  return false;     
# ^! J3 V6 e9 @( w7 C0 Z' [  }     8 _! M8 R+ l( p9 Z; U/ I8 L
     
9 A9 j* i, T9 y' Q- o- r8 z for (nIndex=0; nIndex<id.length; nIndex++)     
! Z0 y' h  H3 |, L: ^, k- T  {     
' |0 Y1 V$ t- d+ N, h  cCheck = id.charAt(nIndex);     
" H% d! M# b$ e% x/ y, @+ z  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     ; w3 L) l: s6 Q' V' x7 G
   {     
& b/ C& F  a5 o  n; B" H   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ! m9 r7 Y  _# B$ K7 L% o6 d1 m" q4 j3 p
   document.sform1.id.focus();     
) F$ I% a! r6 u' {   return false;     1 B% o; x1 H0 m
   }     : M% Z: G8 M% N: I
  } 4 b- }. ~! x+ T+ {; P
  chineseid = document.sform1.chineseid.value;     
) }, ~: \7 K! E* z; J if (chineseid == "")     
3 x. H. e' Z' P6 U  {     " I$ @9 q$ B- W: v1 L* q! V
  alert("请输入中文昵称");     
. p7 y' N7 r& |) R6 s$ q8 ?  document.sform1.chineseid.focus();     
# S$ z) a. M1 f9 B1 H( l  return false;     : ~! ~! ]* W& {, p
  }   
% w: U) G3 }7 B, c& D" y password = document.sform1.password.value;     
% d2 Y; u9 C7 T2 f+ C if (password == "")     
. s" H: T4 Y5 t* q2 I, |$ w+ S7 p  {     , v, V( l2 _9 H' m( K6 C# E- s9 t
  alert("请输入登陆密码");     . e9 O- e7 p" U# \1 B! @) x+ J
  document.sform1.password.focus();     
- m, l$ R/ l. y7 g% K  return false;     # @' u0 F+ E% b" f) [
  }
" E5 [$ {/ H8 Y. r: F password1 = document.sform1.password1.value;     ) ^% k& ]) L+ `- @7 m# x- a
if (password>password1)     
8 Q$ R! ^; w. l" g* f  {
' `( D3 n6 w  P$ Y& X2 m) ^  u     alert("重复密码与登陆密码不相同");     
0 z8 e' x$ i. h  document.sform1.password.focus(); 7 D0 N" ?- I2 G, F6 \( F% y
                                document.sform1.password1.focus();     7 S6 J3 G  q8 n) l1 v$ O/ o% M
  return false;
* v9 H, a1 b+ `; ]1 j2 c$ v  }  
% R; i8 m$ A& E# |! d: s* s  C1 cif (password<password1)     
2 A( _: k9 u$ d+ d% B$ N  {- A$ D. M/ F' s, K
     alert("重复密码与登陆密码不相同");     
! k' q6 }- C4 q2 K. _5 N9 K; {  document.sform1.password.focus(); $ W- d6 R* C0 ?/ W* E
                                document.sform1.password1.focus();     
8 c% o* _5 L# S' [+ k! B) N* n8 t  return false;4 g" ]$ ~' d0 \( z( u. Z6 i: W
  } 8 G! S5 d8 O  N& w7 ?) f+ Z4 S+ M
if (document.sform1.email.value == "")     
* w; I# J6 z. V. o3 o# H% r& L  {     
$ U) l  O3 ?0 J% b  u1 s$ f  alert("请输入您的E-MAIL地址");     / f6 U8 ^+ O! E1 m7 Z
  document.sform1.email.focus();     # T9 }, M7 R1 `/ [; O
  return false;     
! ], O4 E1 r* I+ F  }       b0 X& K# o' L
      7 h7 a/ T- D+ A: ~, |. z
email=document.sform1.email.value;      - k1 J4 ^, Y$ h* N
emailerr=0     5 ^0 q3 U) B# t
for (i=0; i<email.length; i++)     
) n/ n- s* S" O% Y; j' E; S  {     
$ s7 S& K- s( X! Z# w1 p  if ((email.charAt(i) == "@") & (email.length > 5))       g$ g) u9 S; D. Q- _) R
   {     
4 S& i* t( X( r" b4 Y! q7 T1 Y     emailerr=emailerr+1     + B  `. U3 H" u2 M0 i9 q4 _8 Q$ ^5 I
   }     + d' p8 _1 @: r' x8 p2 O
  }     
1 }+ F' o" V; J$ N) l+ H if (emailerr != 1)     ! z$ z8 Y6 D2 {' R0 @3 A' Z; u
  {     
# ~! j) A: s6 G5 l) @6 b  alert("请输入正确的E-MAIL地址");     
/ F6 k+ ^% _' v1 d+ P  document.sform1.email.focus();     : t9 z, t) J/ K0 j4 w
  return false;     
- U4 K% T% ^( ]- P* E/ C  }           
; \3 }, g$ ~- H( n/ N5 i6 a% T" W. v' P1 S  4 E" t4 g# P# b& r3 l: w! u6 E
if (document.sform1.checkask.value=="") $ _- k1 r* E7 r2 l' u" o1 b
  { % n/ r2 Q* S6 v0 |
   alert("密码提示问题不能为空");     
: c; x  m0 n- g5 L1 D1 R# m# J   document.sform1.checkask.focus();     8 D9 @0 s- [3 ]% _7 ?4 w
   return false;  7 Z2 W2 u1 ~  S. z' s
  } - m2 ^' n. S/ ~! b' V; z
if (document.sform1.checkans.value=="") ) k$ ~( C8 Y8 _: [- j
  {
4 P8 J9 ~! d- x9 v8 @& R' r8 @4 s$ ^   alert("您的密码提示问题答案不能为空"); . {. k# Z% h6 \+ t. H
   document.sform1.checkans.focus();       b) O1 G; C  y8 Z# t% Y
   return false;  
- D1 w, b. f" D$ \  }
, w+ M  b3 f0 K" a: T/ }+ y7 f return true;     , h6 E# T9 M: ?; [8 E
     
- j) D5 n1 W3 t- X8 }5 p. x} 5 [7 e! a* G; i: S* O- G
</script>

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