返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
, X, ~5 Z9 H; B. N" W<script language="javascript">     5 E2 @6 Z: }/ r! P9 X! x
function IsDigit(cCheck)     . }! b  c( D( I4 H
{     
  @2 E4 o, {+ _: i  _3 A return (('0'<=cCheck) && (cCheck<='9'));     
6 r+ P4 m4 m' n! K5 `+ e( [8 v! B }     " w& e4 a( g1 d! r0 S
     3 }7 Q0 _+ ]% U- Q
function IsAlpha(cCheck)     
" y* r* y1 F& u% w! E' R6 y {     
' Z, |* s5 E( ^# r2 f return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     ' C; z" l5 G3 j
}              7 ^- _& x8 h' L2 F; v# p6 G; p
      
" k- S8 J' u* R4 B  Ofunction IsaNull(cCheck)          7 G+ ?' V- a0 O5 n
{         
2 q: t7 j. O+ X  q6 t' y return(cCheck != " ")         3 @5 U1 _8 M$ s
}                              
4 S/ L3 Y; }! B$ V' [  G" X      
, W1 y6 N; s9 G1 l8 S+ lfunction checkform()     
# S: j2 R( t9 i7 N" o{
% o' K) e  \! W/ f9 l' C  id = document.sform1.id.value;     - }( f6 `+ C& ^: b% T& c0 E
if (id == "")     
& |$ h" ?( x; |& i2 L9 R+ A  {     9 R0 x: c+ f3 n& I
  alert("请输入注册名");     
. [& S# V% B) x( ]" t- a  document.sform1.id.focus();     
( }1 L9 E% G* K9 N- G; q! j  return false;     / C$ t+ S0 C0 z6 ]' U. C
  }     0 k: l2 B4 d7 g! T$ Y, ?
     
% H6 _1 z4 c9 b4 g/ B4 j$ Z% ^ for (nIndex=0; nIndex<id.length; nIndex++)     
% s3 J, ]& @% ^  {     9 O) ^. N: L$ q5 e# c+ d
  cCheck = id.charAt(nIndex);     
  q& m; f) p; w8 {  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     % F( B$ n$ ?3 N' h. g- |+ R
   {     7 A3 i0 h- ?1 E: ~+ s
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     - s& D+ L' z: R  t6 I; Q/ v4 P" f
   document.sform1.id.focus();     
* l( l  m  s  `, }5 S" |& t   return false;     
. b7 o0 ]! Z, [. V) o   }     . }, W( T; J% E8 I+ J( m
  }
5 s; S! J4 \6 ^8 S  chineseid = document.sform1.chineseid.value;     
- y6 a! K$ K7 U1 F7 Z if (chineseid == "")     
, y3 ]1 X, i, c. Y. l  {     
* o" M5 \/ R0 e- V+ ]  alert("请输入中文昵称");     
$ V1 f5 C0 B0 H- o$ B* U( p5 M  document.sform1.chineseid.focus();     
# ]% v5 @( M) y; q3 a" D  return false;     * F6 e' a; {3 W0 c2 b
  }    3 U, D/ S! @5 }' ^. b2 P7 V) r) L
password = document.sform1.password.value;     $ @7 i" c- p, U8 @% l+ ~& Z  n: w% B( V
if (password == "")     
) y/ s/ B* j6 u' e  {     , ^: n& L: U, }- ?& P9 f! l1 U7 a; I
  alert("请输入登陆密码");     0 W( y& [: O; G: C$ R: v
  document.sform1.password.focus();     . m# `$ z% c+ K5 ?
  return false;     
( D9 q# w+ a' ]( y4 H2 _  } 8 b3 H0 y" l  p' [4 M7 j
password1 = document.sform1.password1.value;     
2 E8 S) p# W( k  f8 O, s' i; v if (password>password1)       W3 Y: `8 o  z9 ~6 b1 v- L
  {$ O5 y: n& s" l7 }: l/ Z; V
     alert("重复密码与登陆密码不相同");     & r. g/ O1 c/ Y# R+ c4 F
  document.sform1.password.focus(); ' t. A& ]6 J. L' f1 U
                                document.sform1.password1.focus();     / s3 }" O+ U/ b& u: o
  return false;
4 c- ?8 T+ h# O4 s  }  
( [& |: G9 z9 Z- H+ tif (password<password1)     : J# |& f3 @4 P% e& P7 i& n
  {* ~  F3 ~8 C1 e3 j
     alert("重复密码与登陆密码不相同");     ; V6 L2 s4 J) U7 u& U" ?0 h
  document.sform1.password.focus();
" _6 a7 ]$ a, B: @5 z3 R/ q                                document.sform1.password1.focus();     
3 ?9 K% ~2 @; N5 p; @  return false;* z# N6 m/ V2 m3 P
  } ( v7 {* @- _' P' b+ {6 `
if (document.sform1.email.value == "")     ) J, P4 ?  h0 P+ S' [+ Y+ W8 G
  {     3 p! O7 W+ K% X$ y$ ?# A& D
  alert("请输入您的E-MAIL地址");     
  g* V3 F+ J6 a/ l) p9 P  document.sform1.email.focus();     , [3 G! q9 g! G( L5 s
  return false;     , [# `" O4 s& B5 s) s
  }     8 F3 ?2 b" Y+ i1 S$ ~1 o, J/ s
      
0 a# B9 K, m6 r+ K$ J2 ] email=document.sform1.email.value;      
. N0 R1 e/ m/ F$ J* ^2 D emailerr=0     
, e! e% s6 X+ ]4 o1 J" `' v* k for (i=0; i<email.length; i++)     & C" l. ~2 R8 A7 c
  {     . I* L* u' j2 b
  if ((email.charAt(i) == "@") & (email.length > 5))     
8 d& I! ]9 S7 L( g0 I; w/ C   {     
+ U! O! h2 x% V- H     emailerr=emailerr+1     
; q8 s' t) I2 ^0 \' p   }     
3 Z0 s: S. F; c  }     
3 b. K) W9 F6 V5 z if (emailerr != 1)     4 F3 j2 U* A* g# [7 e. ~
  {     
" B& }/ R6 h7 ]4 H4 n( h' v. t  alert("请输入正确的E-MAIL地址");     6 M, K: D) {1 N% R8 x! }5 r9 d
  document.sform1.email.focus();     
! K  u2 k5 G6 e  return false;     # d3 E$ w/ N' b7 h7 c  ^
  }           ; ^2 X8 @4 Z& W( _/ }
  % G. |5 {5 S7 h# J
if (document.sform1.checkask.value=="")
5 p% |$ c$ B5 M5 x9 `  {
/ R; z3 |! [6 @: {: U6 c   alert("密码提示问题不能为空");     * z4 m8 d% N9 i9 z+ ~
   document.sform1.checkask.focus();     
! e" Z+ e5 E; n0 u: `7 \   return false;  ; u% W0 l( |8 r
  }
5 O# `. l* \- ~ if (document.sform1.checkans.value=="")
3 O+ p5 @( p& X. C) V/ ^' A9 t  {
( t" w! m6 O  e: p; u   alert("您的密码提示问题答案不能为空"); % K  w. A; y6 t+ r  V1 _( c
   document.sform1.checkans.focus();     - K$ g! I* ^4 V9 o
   return false;  
1 T6 M. F/ O) H: X) ?! G  }7 s. i! w  f& `& J6 d4 G5 K
return true;     
- O+ f; F& b. l     4 j) \# D* h9 T. s5 I- w
}
& _8 c. u2 a# M</script>

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