返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:8 w, ^3 L. `* T4 O
<script language="javascript">     4 R0 y0 _# |/ H
function IsDigit(cCheck)     
+ y% |  z# t' }' C8 g' W0 x {     9 D; O8 _5 n/ S$ J8 X7 `+ |
return (('0'<=cCheck) && (cCheck<='9'));     
2 G5 V6 @% K9 O2 ~ }     ! l5 r. s( }8 l; Z% m" i
     6 [7 C+ R& t# \4 }: E
function IsAlpha(cCheck)     " a+ C/ f- b" V" e
{     
7 G) o  A) P! r* m return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
& J  q- n, e0 E" d2 J  ` }              
! m' v+ o) O* [8 _      ! ?8 R# d  m- ^9 q) `  h
function IsaNull(cCheck)         
6 m8 R+ r9 _% X3 ?% G {         
1 n/ |& B2 P$ r, h5 W4 G' x& @ return(cCheck != " ")         4 ^0 E+ J+ d* P9 S
}                              
$ i) D2 d- n/ u) e2 p) ^: i: ~      2 d/ F* h; ?( ^1 e
function checkform()     ( r/ O6 V" X  @. s: W% D" Y
{ # e/ h2 u: M3 r+ ^8 `  M
  id = document.sform1.id.value;     
, N, |, t& D' O' P! \8 x4 y if (id == "")     
) `. j" u; W; d1 D8 M7 h  {     5 X5 [0 N+ S+ p$ @9 F
  alert("请输入注册名");     
$ e6 ?' d3 N' W4 d  document.sform1.id.focus();     
7 a6 k, |; ^" m1 ^! V  return false;     
7 T4 f8 y) d( {  }     
( i9 K2 [6 D7 C8 `     7 Y: G$ L; Z; ]7 n$ b* c
for (nIndex=0; nIndex<id.length; nIndex++)     
  V( l0 \# @0 a+ l  {" K+ E9 k  {     5 F, q3 ^/ N: i6 M3 L: D6 q
  cCheck = id.charAt(nIndex);     6 [2 [4 j% i' O+ N1 T
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     0 v4 Q% N! c) E
   {     
; j) Z5 N) g3 T   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
% V  o2 e0 k% N9 F+ m) W. P8 o' i   document.sform1.id.focus();     . S1 _& |/ W0 L) b( ]6 W9 @
   return false;     
) f6 u" @3 {+ r6 s6 c   }     & s5 a/ H* s/ k% j& l/ y
  }
9 ~- H5 m; o1 u  chineseid = document.sform1.chineseid.value;     " U3 F& x8 ]) r- |! a5 ~$ j
if (chineseid == "")     ! q) A) n8 \/ m* s
  {       u! \2 ?1 U3 p1 n+ b% k
  alert("请输入中文昵称");     ) ]# ]1 k8 e1 z( W9 t" W7 p" F6 i5 e
  document.sform1.chineseid.focus();     $ ]$ O0 V. Y- H0 x# C" q8 S3 R+ [
  return false;     * l5 N0 V, @; X  s0 [
  }   
( u9 B6 p1 \3 l7 z9 s password = document.sform1.password.value;     - M  H' b0 z7 q) _# e
if (password == "")     
7 M" m% O4 T" C4 |% ?0 r1 R  {     
5 ]( `% U  q6 n- m5 x  alert("请输入登陆密码");     
  M2 F; E5 Q; b7 D  c' `: ^  document.sform1.password.focus();     
9 h) P/ h9 @: w# M  return false;     ( B4 f" p- V7 `# D( d' x
  } 4 K5 h6 C, H8 j( [. f  b. G
password1 = document.sform1.password1.value;     ; \% F/ P. \7 D6 h# X7 k
if (password>password1)     
! W  q4 W$ s3 M  {- S+ j. a! ^( m6 u7 U6 r
     alert("重复密码与登陆密码不相同");     * z/ i; M8 g! I* W5 F
  document.sform1.password.focus();
3 l! f( f3 W% |9 P% b: l                                document.sform1.password1.focus();     : t# h% `  o3 C/ \
  return false;
  u9 r$ u- n: E. W& b+ a  }  
" E" y% T3 `2 r6 X6 Tif (password<password1)     
/ N7 W( c" I8 \" |' C5 h& ]0 m9 X" D  {! d) o6 b, g$ `1 s( \! e5 k, A  ~" Y% e
     alert("重复密码与登陆密码不相同");     0 v( b& K5 E0 h! |4 E" Z
  document.sform1.password.focus(); ! P+ c9 W9 |( z. ]' R5 ^/ r
                                document.sform1.password1.focus();     * m$ y; u! y9 u9 ~% q
  return false;6 G3 N% I* a: e$ v3 X! }$ l
  }   B2 N3 |+ g- M/ ]0 }' r3 G! P
if (document.sform1.email.value == "")     1 u$ s2 J; F* S- c  E6 s
  {     
. b& u4 G- q: c/ o5 a* u  alert("请输入您的E-MAIL地址");     5 `+ L! }) [0 L. E/ k
  document.sform1.email.focus();     
1 e7 m  I: V; E' S  return false;     
7 x- k6 d' C+ |' l; W/ {# m  }     
+ A) n2 k% y1 k: g" V) s) v4 F      / u  f1 g( [& i
email=document.sform1.email.value;      
" N8 R3 O9 x' H  g emailerr=0     & S$ G4 u8 M. R# J- v
for (i=0; i<email.length; i++)     
, T7 T2 B! y  q. o, x9 s6 ?/ C  {     
5 R! U  s4 g; y4 d6 A2 d: O  if ((email.charAt(i) == "@") & (email.length > 5))     6 L2 W' M* x" ~" k
   {     " l& j$ K+ v  {, }0 e9 m
     emailerr=emailerr+1     ) I* i& P7 e: j: r
   }     
, v+ S, z& x' A% P  }     
0 o3 P' i& i  k$ Z+ m: V) w7 m/ b if (emailerr != 1)     
% `0 \: _% Z! D$ u* D) l  {     
0 _5 d) F2 @; `. E  alert("请输入正确的E-MAIL地址");     . i8 ~7 ]2 T$ J- O4 B8 w
  document.sform1.email.focus();     
; V+ ^- p& K8 P& s  return false;     
, i, T1 K1 V3 A& |' R: M3 M& d% T  }           
+ H& z) D4 c" S, o/ N8 L! D  
) r1 q  B- M8 t if (document.sform1.checkask.value=="") / ~$ s) u+ H8 J( T. b8 ~7 h  D
  {
+ R, G# T7 g/ t   alert("密码提示问题不能为空");     . S- O/ e4 B. Y/ G4 B& P
   document.sform1.checkask.focus();     4 G, M9 ]2 d. ?  M% Z4 T0 j
   return false;  
/ `1 c+ s& ]! L% s  } 0 d5 N+ G9 O) {7 T) w7 G0 W
if (document.sform1.checkans.value=="")
2 e5 n" o3 r4 V! a& r9 \6 Z  { 5 x2 F  c( d  b" \
   alert("您的密码提示问题答案不能为空"); 1 T; i6 g- l7 q3 R
   document.sform1.checkans.focus();     
% e& |9 K0 E$ ]8 e/ v; X; w   return false;  ( p5 {5 K+ _; U& R
  }
( Z# ^5 q8 w9 d+ a7 p% r return true;     
2 \9 {! T  W1 j) n     
% P4 Q% j3 ~0 M# f/ l}
* A* z5 ^: z& M5 g+ L' a</script>

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