返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
3 p+ z2 B  x# C5 i<script language="javascript">     
, x  O0 E' w- K' Afunction IsDigit(cCheck)     8 N" w2 G. x5 c0 s/ j& h2 R
{     
' X/ `8 Q0 U6 e3 D9 K1 _ return (('0'<=cCheck) && (cCheck<='9'));     2 p5 Q, m* Y3 h8 F; v% U5 r8 c
}     1 ]' p% M* F. W9 A5 t, b
     
, r! r6 ^# V8 ?& R! z/ Cfunction IsAlpha(cCheck)     
) m/ o! d( ~& v7 V2 | {     
7 l9 @7 `, D! P return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     6 g6 P% h4 I5 ]* Y: C) j
}              
& E3 {1 H5 t" f5 n9 A& X1 R      , @9 J9 ?, y3 L" T" z7 v
function IsaNull(cCheck)          % B2 d+ M$ `& G3 I
{          ! _' [0 A* ^7 G6 v% y9 D1 o7 B
return(cCheck != " ")           E0 p( T* \, i: t
}                              
# S& L0 ]) ~4 r. {6 f      8 d: g+ w3 w3 o% l6 O8 A# x7 S
function checkform()     * Y1 S5 b6 X& a1 n0 a' D: }, s
{ ) C) h/ j7 w3 O1 J. u& x& Q6 K* Y7 V( F
  id = document.sform1.id.value;     
' A/ e2 W& L: i8 v- p; L" B0 I! R  e0 i if (id == "")     + U& k& f+ B' y  X8 S5 n  e
  {     ( ~" e# C1 H& }% k4 z" E
  alert("请输入注册名");     
( z0 R! H! @  l- a3 a# B  document.sform1.id.focus();     
& q: X- j$ F! _' [( c$ I& H+ U  return false;     . {! \2 k6 ]( x- X
  }     . w' m$ B$ \1 n& E% u+ `) h! f1 k5 L
     , l" P( ^% I$ g% I2 `8 L2 R& l: [
for (nIndex=0; nIndex<id.length; nIndex++)     6 {) j, B8 b% Y9 h6 m2 g
  {     7 {- u: p; [+ W# [; ?
  cCheck = id.charAt(nIndex);     
3 {2 @7 U+ A" H# A0 V0 d  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
* y9 i, O2 A; ]   {     2 d$ \' ]/ x: u5 u, {6 X( Z4 C' S
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ( [8 a' [' }+ F5 {7 \
   document.sform1.id.focus();     
% E, M1 i3 }5 o4 [   return false;     
4 |' W  M8 G2 T6 q; z   }     9 ?, h8 J' I+ M. [
  } ! ?" n" @3 ~9 {: w/ a- I( D5 f, K4 t
  chineseid = document.sform1.chineseid.value;     ( v3 h# @* V. ]/ Z" V- |
if (chineseid == "")     * g0 k( V3 o" P& a9 ~5 S4 K( i4 x% f
  {     
1 j4 i5 t7 b. {/ h! H, P0 H. a  alert("请输入中文昵称");     1 ?: j2 C7 ?) Q3 w# E! \
  document.sform1.chineseid.focus();     2 O6 t' [  j1 B
  return false;     0 P8 P' u6 {' m! e0 q: L
  }   
) `1 Z" {9 `% B& b1 |: Q. C2 \( u- X password = document.sform1.password.value;     1 Q% H% m) M# l# s& x* c% O
if (password == "")     : A; g; X0 |; g4 }* A
  {     1 \$ c2 y) `% p3 ~4 L
  alert("请输入登陆密码");     ; P# ~' k: j& E2 X. S# P
  document.sform1.password.focus();     
9 S+ @  ?. m$ Y% T4 X  \0 z" T7 s2 x  return false;     
( m8 H! V3 p8 T0 Q4 _  }
6 z4 z( F3 Z, a$ S$ _ password1 = document.sform1.password1.value;     * E" O$ t! j: K! \: Y+ Z0 W/ p
if (password>password1)     : l7 q- _8 ~9 ?
  {" U2 a, l& g3 b" C. S% S- d6 n
     alert("重复密码与登陆密码不相同");     6 e; O' m( l: j; \1 ]9 H
  document.sform1.password.focus(); * }8 I% L5 G/ K  {- e* b. F
                                document.sform1.password1.focus();     
8 `/ q% A# n! W  t3 v( m3 y9 B  b, |  return false;6 q! O+ z: z$ ]
  }  6 T# a( ?( _# n. n/ l0 W( T
if (password<password1)     
3 k$ J5 x: X9 S, S' ~* t  {
; R* o+ u4 H, @3 G     alert("重复密码与登陆密码不相同");     
" a: c  d2 p) l) y  document.sform1.password.focus();
. y, V* o- r0 k3 q7 d                                document.sform1.password1.focus();     
! F  `8 l: b$ L- H) z% T: p/ \  return false;9 c# O* r9 F! J. E0 H
  }
- P8 c( q9 }8 @* s, \) H" H if (document.sform1.email.value == "")     
- C% `; w( |0 b, V0 ^+ `  J$ B  {     
& p4 n( U* `  `* _  alert("请输入您的E-MAIL地址");     
' t; `* t2 E, r  F* R  document.sform1.email.focus();     8 L7 Q: U. O( h9 p  x6 W
  return false;     
4 o: y/ P7 z' r$ x( W0 C  }     # u/ q$ N# [! V# ]
      
+ n( B/ u, y* G email=document.sform1.email.value;      
/ g) T5 k$ ~# @0 U. t& H emailerr=0     
* G/ j9 l, F, G" S for (i=0; i<email.length; i++)     
1 y( N+ y, j8 ]) p  {     
- i$ G/ O, w9 O# R1 j) X* ]& o, }  if ((email.charAt(i) == "@") & (email.length > 5))     3 G* U) T0 f6 z  k$ z) E, m
   {     
' D  z! O! f1 a6 d9 W8 R0 N: x     emailerr=emailerr+1     : O# a3 M' z( H5 ^" i- ?$ A7 K' R5 D
   }     
# T% X. K: }8 C: A$ I; q8 L  s  }     
' a: s# }! T' C+ ^' y& m if (emailerr != 1)     4 l$ H- c$ Y8 q( L  M
  {     ' m" D  S% D9 i8 ~
  alert("请输入正确的E-MAIL地址");     1 Z7 e+ Q5 G1 Q) {
  document.sform1.email.focus();     2 d+ D1 c7 [& }0 Z1 U9 c2 w
  return false;     9 Y( s5 X0 ~9 h5 A* J2 R; E
  }           
  z) R( }2 l. N% R* y( l  ! S) W/ l3 p& M# [
if (document.sform1.checkask.value=="")
; l1 P2 Q6 ^: ?/ z: Q6 _  { 7 i3 o3 D* L, ~9 o8 S
   alert("密码提示问题不能为空");     
5 ]3 H/ \0 ^$ w& L4 S   document.sform1.checkask.focus();     
" b7 C2 h! H) R; o. I   return false;  3 V8 R) J+ J, P% x4 S
  } + Q( [6 }) k6 a5 X1 K4 u, L
if (document.sform1.checkans.value=="") & I6 f# \" X- A" J+ D6 g
  {
; v& U# l' C0 X2 T   alert("您的密码提示问题答案不能为空"); ' _' z! u% |6 Q, ~- @4 g  H" a
   document.sform1.checkans.focus();     
- Y; L. l1 t& J( Q   return false;  
# t3 h: ]% S& t" z. R+ o  h1 ^' B# O  }
  [; {  k3 h% E# i9 [# b return true;     
& G6 b( Z3 Q7 F     : @6 R. Y0 l  V; E5 Q
} 7 R$ o! ?* L$ H; Q
</script>

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