返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:8 h3 I" @7 C1 v
<script language="javascript">     
: M8 P# |2 q$ U9 Zfunction IsDigit(cCheck)     
, D0 p! i) h/ l; ~0 G) f7 x7 F {       T# R3 E5 M  a
return (('0'<=cCheck) && (cCheck<='9'));     # C: H  L9 r( ^6 L+ H' B3 P4 O
}     / G8 ~+ f0 P( X. S/ V
     7 Q( Y- d$ i- ]( i
function IsAlpha(cCheck)     
6 T* ?% w. G. F8 j  Y {     
+ l4 b9 Y6 m6 c* _% l return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
& m* N+ \: l) b" [$ }+ V }              
. S1 P! V5 ^3 t4 _, |" ~. _! n      ' R5 u/ @% A9 Z/ L5 S0 W* G
function IsaNull(cCheck)          ( s- V+ h) u/ l0 G: u  Y1 X) C
{          ' j; Q0 }$ h% K/ E
return(cCheck != " ")         / Q8 L- r6 e$ N' x* u
}                               5 y( N* V3 M7 Q8 N5 J/ |8 m# K. R
      ! q" P4 k) W: t- k) Z
function checkform()     
) @6 H, R+ w" A{ 7 j1 d! n$ e4 n6 W/ J$ F
  id = document.sform1.id.value;     
  z, f3 \% D. h* H; \1 q* r if (id == "")     
- R+ F! ]$ Z& V+ [  {     
" V. q& A5 c: U" U- Y9 _3 |  alert("请输入注册名");       [+ I8 Y4 Z3 X4 N/ a5 A# i
  document.sform1.id.focus();     
% y" _  f2 r  [6 E  return false;     
0 c4 U5 Q3 u8 ]1 Z0 G, G$ [  [  }     : k' }0 Z  `! H. o2 e/ s  m) I
     ' L/ j9 E0 H' T+ A( Q4 E
for (nIndex=0; nIndex<id.length; nIndex++)     5 N& B: L# U8 [! S
  {     2 {1 [, X& q( e; C
  cCheck = id.charAt(nIndex);     & U) a. [" v8 k2 K1 p, ]6 f
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     6 W* t- N, H, ?( G( q
   {     . q: Y9 t! r# s) i2 H* J5 M
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
+ D& U. B$ f/ m   document.sform1.id.focus();     2 ]) v8 Y; U1 C* y# o: m
   return false;     
8 Z9 c1 k6 [, j' E1 [, S   }     9 o  z" m/ l- v" {; x
  } ! r# ?0 M. G, |# U& D9 q0 E3 @' J
  chineseid = document.sform1.chineseid.value;     
2 \4 M4 c1 o6 B/ I1 @# S1 X if (chineseid == "")     % k( [( c+ S3 F- |, O
  {     
, u, n9 c; s* S  b6 l% Y  alert("请输入中文昵称");     
0 W8 B: H* ?9 K3 a! l  document.sform1.chineseid.focus();     1 v/ v& q* }7 |1 y5 a
  return false;     
# s/ {5 |2 i. H# g  }   
$ g$ Y1 u, h! T3 [1 C6 d, M* P6 I7 k password = document.sform1.password.value;     2 {. h: y0 }, m2 O$ @; [" ?* T7 U
if (password == "")     . `3 A1 c( y' ~9 o- \
  {     7 ]0 p: s3 Z6 U) I* _
  alert("请输入登陆密码");     
( k. o9 C, L9 }! R: a% T  document.sform1.password.focus();     
& c2 O: z" P* X6 N7 J; C- P% D" t, W5 R  return false;     
9 Q; Q. [$ o4 u/ T+ ~5 _* ~0 R  } # ~1 Y% Y1 h- p1 O
password1 = document.sform1.password1.value;     4 D- N+ J$ ~0 k# L
if (password>password1)     - z8 [" a( Y4 S
  {
0 T# ]' e, ^1 y1 ]( V     alert("重复密码与登陆密码不相同");     9 Y$ S8 l. ?% g2 C- l# D
  document.sform1.password.focus(); 0 u# K: |6 U' G% h4 l2 p0 A: ^
                                document.sform1.password1.focus();     4 X  Y- C9 q( F; }0 r: ~
  return false;, `9 u* F- U6 l1 |& ^/ }
  }  $ [, y1 C0 u( B3 N% T
if (password<password1)     
1 v5 L. r: |9 w) t1 h; h( Z  {. l, J6 K- ~9 k" X
     alert("重复密码与登陆密码不相同");     
" i9 {, A" R) P, j& j7 B9 n  document.sform1.password.focus();
2 p2 h1 b2 J: J7 J  T2 ~                                document.sform1.password1.focus();     
7 E, G" J/ q# [7 D  return false;
9 H( S8 g& M: N; D8 K7 D/ e8 Y  }
) y" I  W: S+ I if (document.sform1.email.value == "")     0 k! U- ^4 D+ B
  {     
" l- L5 K! C. ~% R( ~& `! ^- {  alert("请输入您的E-MAIL地址");     
  \' j  k% Z9 b! C: B  document.sform1.email.focus();     
- X3 g( b4 C, h$ ~  return false;     # P; w& z; L/ r. R( d8 Q6 h+ R% R; O
  }     2 t4 e, T8 ?, S3 I2 }( z
      
6 a: J, q6 r6 U4 O: Q+ ? email=document.sform1.email.value;      
7 U0 ]$ P9 h# v. u: I emailerr=0     7 x* D3 ~% k/ P. Z/ w& [
for (i=0; i<email.length; i++)     8 a) G' l4 b% Z$ C" C
  {     " U4 l$ t0 [' k
  if ((email.charAt(i) == "@") & (email.length > 5))     ; ~& p: K; ?* k0 ~2 n
   {     
6 h2 W/ X" G- T0 p5 n, |     emailerr=emailerr+1     3 I' [; U7 J/ M  R$ U6 [* M, K
   }     
3 U( ]' j: d* f5 f6 Y  }     
6 K$ M# e) p' ~5 j; s( C! d1 U if (emailerr != 1)     
; ]. {' C0 o3 t9 |9 N  {     
6 J2 G) P3 g/ k0 H- Y( I  alert("请输入正确的E-MAIL地址");     2 w8 A4 J4 D) x
  document.sform1.email.focus();     1 N: x& n- ]0 Q+ O2 w2 ^
  return false;     
- R. K: s# X0 _( ]) }/ v  }           * |- C# @  q+ I; A
  
4 M% @4 m* q2 _ if (document.sform1.checkask.value=="")
! S' Z8 N0 a5 B& I! `/ z( i9 u  { 9 D1 H; R  D& `# u. n, W, a% u
   alert("密码提示问题不能为空");     $ W0 l4 z  k. J3 S' z" R) x
   document.sform1.checkask.focus();     
& g" u" L( C4 [& u   return false;  2 f9 N, K+ P) M& x7 t8 y
  } 4 c% W: C' i+ `+ ^
if (document.sform1.checkans.value=="")
: t" |" k) j$ N" I4 B8 T5 z  { 5 I8 g1 {, O/ S
   alert("您的密码提示问题答案不能为空"); 5 l  W! C6 s9 j7 f
   document.sform1.checkans.focus();     
$ D8 |9 P8 a! s: ^/ f+ j   return false;  " g8 m1 c- _' _! s
  }  \* g' G; A6 r; s; Q& Q! X, `$ E/ H
return true;     - b4 y1 E# Z1 C4 d/ R5 r/ \
     ; f7 g$ l& Z* L1 T
}   u# p! T  d+ f5 g1 c' x* \% @
</script>

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