返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
. a( n5 O" W1 m* U. h8 V/ N) s/ `<script language="javascript">     8 z, E0 e+ a* m, w/ E: V  I
function IsDigit(cCheck)     # n: Z0 ^/ U4 o/ o! a. G  G/ n( H) T
{     ' u9 Q& N! y# S, l2 V, a; M
return (('0'<=cCheck) && (cCheck<='9'));     
. S) \2 A" m; O) _ }     
! \1 m# J* j0 p+ q5 b8 |3 |+ E$ [     5 N3 a( L* s/ q! q4 I1 \, v: h+ d
function IsAlpha(cCheck)     
0 J. ^- x2 }9 L. V% g {     
0 M+ j, V5 e( b2 ]4 J2 K return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
* n# N( Q# I* [4 N6 s' y3 A; K }              
, C9 f& p+ H$ f2 k* u* z* a% x8 m" u4 |      8 Q5 Q" C5 `# Y; ~
function IsaNull(cCheck)          $ t8 t2 B$ \3 U0 J; W
{         
6 t3 V( r$ M9 O- I2 H& W return(cCheck != " ")         
% D- r, i; U( q: U* `$ a }                              
3 ?( K8 B+ U/ T. I2 H- W- _      5 z! L! r2 P' b, ^& t" p
function checkform()     
/ F: E& z7 r8 f( U+ A* \" G. b{
4 T- ]+ [3 z+ b/ y* H% G3 W, g* a  id = document.sform1.id.value;     ' o, P9 j  c* _. B2 P
if (id == "")     2 }) C, `' `; b; X$ [$ T
  {     ) l* ~- P' J! z) @( w4 ]6 F
  alert("请输入注册名");     
; O/ U0 [4 C" `- C' F  document.sform1.id.focus();     ' P0 Q2 n: E! F6 @( I5 c7 W
  return false;     9 z! K8 e* O& w9 S$ y8 M
  }     
3 v. N+ r- }+ y+ P# Z- M- C6 l     0 t$ Y6 s( t$ S' D; j8 V
for (nIndex=0; nIndex<id.length; nIndex++)     
0 N+ w5 F+ B( G0 }8 {% [  {     ' A, f# g0 @& O. T, W
  cCheck = id.charAt(nIndex);     
# t5 B' r# y/ B* M  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
! m& k- |. D/ J8 t   {     0 p  a$ N  u) m1 V3 ~
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
1 c; _" d) q( G$ j8 A   document.sform1.id.focus();     5 d4 y% {1 V5 _5 h
   return false;     6 ^: y3 u7 v+ K* i, I9 T
   }     & `0 S+ k/ Z' m' h/ C* H- U
  }
) g1 x, \% r4 Z! H  chineseid = document.sform1.chineseid.value;     
( `+ b  F8 L/ X3 D5 i7 ` if (chineseid == "")     6 j  _2 ?0 Z5 A+ C$ z/ `, J
  {     * C1 W1 w  o4 a9 s
  alert("请输入中文昵称");     8 S# H1 `+ }# P7 W, u
  document.sform1.chineseid.focus();     ' r) u' V4 H9 r+ z( t0 S
  return false;     
1 I# T# m2 \) Y7 r# N5 s  }    ; h3 d) _4 N7 Y& [2 k9 ?! E1 u
password = document.sform1.password.value;     
5 q. |: k3 I  `% r# s if (password == "")     - j, [' f- e6 j# S% i! _2 J
  {     5 P" W% |5 U% S$ \+ q
  alert("请输入登陆密码");     ( K2 X2 {5 \7 S" G$ a. L! I) W
  document.sform1.password.focus();     
9 u# Z8 R2 \% d3 F$ k. v/ \* t; a  return false;     
! w; F" w0 N$ Q) ^  } . B$ f, ~5 j3 J* T  k; L# ^; l4 o) X
password1 = document.sform1.password1.value;     . X' C9 R# }1 B7 h0 K- i: {* D) D
if (password>password1)     
9 D# W& T1 [0 H  b  o1 B  {
. G# }; a4 K6 e" Q& ^9 I; f     alert("重复密码与登陆密码不相同");     
5 T$ ?5 b9 t8 z9 Q) `  document.sform1.password.focus();
" @2 V0 v$ n4 C0 a. Y' n) O8 Q                                document.sform1.password1.focus();     
9 n  Z, Y; v' k$ B% s6 c7 l) s  return false;% t+ H; `, }" L, o7 [# g  \* i
  }  0 n2 z/ ?. }3 n; T  d: a
if (password<password1)     9 y3 D" H" [0 j$ R5 S; W9 d& {1 e0 o
  {$ d, e. a7 D& Z( _+ r
     alert("重复密码与登陆密码不相同");     
! t; s4 `0 F0 R3 h  document.sform1.password.focus();
% r& J9 g8 Q4 n7 P7 R) a, E" S! H                                document.sform1.password1.focus();     
8 a! i, D8 t9 z0 A" A  return false;8 M% E# P' n, O2 f/ `
  } ) J% t( A& H2 W% C# t/ r
if (document.sform1.email.value == "")     
! w( {2 d9 ~% Y+ d9 A  {     # {" g0 c7 {  }- f3 f+ m9 x# P8 h7 u
  alert("请输入您的E-MAIL地址");     1 a: w% ?4 y$ L5 n$ ?( s
  document.sform1.email.focus();     ( L( M5 J: ^8 u: M) ^$ @  |3 |: z
  return false;     8 m+ k  w' g6 A2 T+ ^. F1 x
  }     
1 i( K% T) T- H  m% ^2 M) g( \/ Z1 m: O      & X; i0 s) W3 G7 f/ A
email=document.sform1.email.value;      5 u5 m, C% M7 m4 ]3 n
emailerr=0     8 ~; w1 G7 t- n+ r  U% J
for (i=0; i<email.length; i++)     
' D, r1 [* o) D" y  {     9 J1 l, l# m5 m9 @, _" r
  if ((email.charAt(i) == "@") & (email.length > 5))     / m- e4 Z' E( Z2 {6 B
   {     8 K0 R# {' [  \: }; ^
     emailerr=emailerr+1     ! C' r* [  J& O  `+ O' g0 t% \+ ~
   }     
" J' E1 j' W3 ~) J  }     : }* H0 k  ?: t& e6 `/ L
if (emailerr != 1)     $ d6 l% W/ B5 h- z# D2 B
  {     6 y. m. x3 c# ^
  alert("请输入正确的E-MAIL地址");     2 Z$ W) o9 I( u# o4 |8 X& [
  document.sform1.email.focus();     
7 y) S! s4 H) m- j( B2 ^8 [  return false;     & a6 H7 \  u: q* }
  }           
& K9 j  k) Y  I. r2 M6 v8 x! f  R  * j8 D  u, Y3 j
if (document.sform1.checkask.value=="")
5 k- N  H! n4 s/ g  {   w: q- o$ M3 _- G0 Z5 U
   alert("密码提示问题不能为空");     
' P4 {) Z: b+ h$ B   document.sform1.checkask.focus();     & P7 d% M- |7 p4 X- I+ R, r( _  x% L
   return false;  ( U9 q: N, `$ u: }. c- \; A: L4 a
  } 9 E- S5 Y8 b8 I+ h7 O- ]1 m
if (document.sform1.checkans.value=="")
9 F9 B; T4 V6 j. A/ O% r+ K; a' v+ Z  {
5 o4 }4 A# a* U0 ?) e   alert("您的密码提示问题答案不能为空");   K$ Z6 l( z  s; n9 |
   document.sform1.checkans.focus();     & D0 V8 b  _" {/ y, V4 d
   return false;  
; v0 H$ ?7 P* l: T. U9 X: V  }. {' }& o* Z0 i2 |; _4 |6 A. W
return true;     
5 D+ s8 V/ u9 W6 t7 d7 m$ J# ]! @     4 G6 G( Y3 ]9 K/ q6 \
}
9 n, e% Q% }- N$ e</script>

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