返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:7 T2 G; ?9 V& L5 l& r2 Z& }  P
<script language="javascript">     
) ]( ?' I% ^% y- W" G( _$ ]function IsDigit(cCheck)     
- ]. G- ]2 g: D {     + `2 R. J2 Z8 ?3 h! A1 A; a
return (('0'<=cCheck) && (cCheck<='9'));     " V# l* b7 |) H0 {3 l
}     
$ G. |0 Z# G/ r3 X2 [3 L     + B9 I/ T. Q- A! x
function IsAlpha(cCheck)     
* s4 n1 z" J  r1 f8 ]# @* m {     
9 m6 R: l+ x. K2 b: H! p, q& U! Y return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
# a' g0 l0 R4 x' ` }              , ?7 V& {  `8 V6 t- e' m7 Z& I8 _
      
" f0 }- M; Y) j: B, xfunction IsaNull(cCheck)          ; B! y+ E2 a5 l( b5 u6 s& H) y
{         
* Z: J( e7 ~9 q2 H return(cCheck != " ")         & C. x5 K6 `: _- F6 H
}                              
% q+ `# e/ k5 U+ R; P1 r      
5 Q* x" g4 j. Yfunction checkform()     
$ \/ m2 H: S5 e3 p. M+ Z7 l  ^{ ' H4 a- S; T) Q5 l
  id = document.sform1.id.value;     
3 x9 D! _5 }1 v3 W- a if (id == "")     
/ k+ R5 e4 X# b7 L  {     7 I1 }! k+ }3 J+ _
  alert("请输入注册名");     * t: ^* v+ w& \- z
  document.sform1.id.focus();     
8 {8 K1 O% W2 v$ `* B  return false;     
% \# w& W9 x4 z5 E# L- Q  }     
' I/ W* w9 Q/ c+ y* d; {' q  O6 `& i     
( Q1 E! b: p" G& Q0 B for (nIndex=0; nIndex<id.length; nIndex++)     $ _1 s( I. l$ Q, @0 |- w; Y: D
  {     
$ B0 r6 K& R$ N  w+ l( `6 [  cCheck = id.charAt(nIndex);     # j  d+ S0 X' I" A" Y
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     . [. ?5 S+ I8 F1 X9 s- z
   {     1 O& F6 a/ @# b: [# t% k
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     4 D$ h6 o6 ^& Q& |, e" n& K
   document.sform1.id.focus();     5 q" ^) }, B2 M9 J' y
   return false;     
9 }2 w; o4 R0 h1 ]   }     
% S5 c+ L8 I5 y! Q9 q  }
) j& {5 a; n; p( g) t0 ^& s  chineseid = document.sform1.chineseid.value;     / U' w/ Q9 J' f$ m
if (chineseid == "")       x& d% L0 k, \6 `! R, |- W
  {     
$ k, F. a( N  s  alert("请输入中文昵称");     
9 B* m( S+ L, W+ a) B  document.sform1.chineseid.focus();     8 `3 ~" o, \" U4 L
  return false;     3 X  |" {# e) f$ |# x
  }   
3 h) V$ |% R+ G# o+ G) W, s password = document.sform1.password.value;     8 f2 b. R9 ?( w2 n% ^. ^& m0 {
if (password == "")     4 k, F  c% |. ?1 D
  {     
9 V; ~( i! ]+ B3 T1 ^  alert("请输入登陆密码");     ( z( m5 \% q( u1 l9 _
  document.sform1.password.focus();     
0 `8 @5 l* F! w' ]7 M) J9 ]- h9 |8 k  return false;     # x' R$ |+ ^( Y& c' I* R4 P5 h' a* [
  } ' H2 [# P' r1 B' G/ P
password1 = document.sform1.password1.value;     
- d5 J& @3 E* m3 I3 q if (password>password1)     
0 s2 B5 n/ t. ~+ S& p  {
9 X  R* |; j3 Q; O6 B( t     alert("重复密码与登陆密码不相同");     " l' z  B8 S) v' |
  document.sform1.password.focus(); ( E; D& @- f4 L! x2 \1 B9 y& I
                                document.sform1.password1.focus();     : q' F; i, x. }( y- t3 J( b$ l9 s
  return false;  ~' L) t0 d! U7 R" I
  }  
: ~: G5 g) ]$ }- u1 z* p( V8 bif (password<password1)     
7 Y' C5 A6 r8 M. l  {/ m) p$ d, q5 }3 ^( P9 c* _
     alert("重复密码与登陆密码不相同");     
( |% D2 ^) a+ E6 g) {* ^/ P  document.sform1.password.focus();
, s8 I0 H6 K2 F6 \; m                                document.sform1.password1.focus();     
# }* R8 W: J3 K7 `- V& Z  return false;
( k7 A: s: j/ Q) _/ b9 j- ?/ g: ]  } 4 B9 Q9 O% J. G# b. @
if (document.sform1.email.value == "")     ' F! F5 w% k: s! @
  {     + }1 {2 B3 p4 b' K& `7 ]
  alert("请输入您的E-MAIL地址");     / @4 t" I  R/ Q& j4 v' R2 m* i+ G
  document.sform1.email.focus();     
  K- |, o. {+ I2 S/ q* V  return false;     ; Z% }5 N  @8 Z! x
  }     
, S0 |! }/ R7 D) T+ E4 f      
; y) e7 L$ x" l! ] email=document.sform1.email.value;      
# `: J3 p# d+ y emailerr=0     - R% d' X" x, ?  j* K
for (i=0; i<email.length; i++)     ; }! }% B3 s, S1 c7 Z; i
  {     
5 l( n7 K/ F; O+ h& I# x  if ((email.charAt(i) == "@") & (email.length > 5))     
! d/ M* R7 g" ~& M2 E   {     / q8 U8 ?" v; e( X$ R) T
     emailerr=emailerr+1     " a4 m( `$ Y8 K. z9 e# {, N- h1 _9 f
   }     7 u* x) u2 i$ Z* m
  }       W4 V9 {+ I, m3 v7 d
if (emailerr != 1)     
9 T1 e1 i9 {$ j7 }' Z" Y  {     
4 \; w9 m4 W7 p- p- \0 O  alert("请输入正确的E-MAIL地址");     , o  n2 A2 E) Y
  document.sform1.email.focus();     2 O# D* e" d* s! ?0 q) V) `& q
  return false;     : L5 n  B  Z9 M# [' c
  }           
9 e# f/ n3 Y7 J# J8 o: b3 F/ y4 j  / n8 S6 Z  C7 w7 l% H% o
if (document.sform1.checkask.value=="")
% @3 J) C# n) c( |4 `. b  {
: }2 ?# @6 Z6 ~4 |! p   alert("密码提示问题不能为空");     
; m1 ?' C% n; d" v0 A   document.sform1.checkask.focus();     - @. N# @; Q: {% j
   return false;  
! s- W; |6 ~5 U  }
. z; i; n! \1 P# K1 Q if (document.sform1.checkans.value=="") . q  W6 J4 J7 o7 H) m2 r* j
  {
! l; K5 [0 ]$ E, x6 W& k   alert("您的密码提示问题答案不能为空"); 4 o& m  c, A$ N% X# n
   document.sform1.checkans.focus();     
  `% I% h+ Y. m2 \! K$ G. r& O   return false;  2 P) d) Y& Y" e( S6 L
  }
! X1 {: [# N) i  r* E" | return true;       G7 g- j( {* r6 T4 X0 f
     + D% R9 P" Y+ ]8 Z3 ^0 V
} % ^" e0 f/ n, t1 e
</script>

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