返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
7 l3 y$ E6 m! v: s% v: m( c<script language="javascript">     8 @; h% \; x, F! N5 H$ [
function IsDigit(cCheck)     : M9 x  u" h5 z, z. d9 ~# u* U
{     
4 T# a- s( E9 G! k return (('0'<=cCheck) && (cCheck<='9'));     ! u3 z3 d- y- [# o/ J
}     & q& E4 I% g2 J
     
( H8 q+ c5 Q4 ]& D" D4 v7 tfunction IsAlpha(cCheck)     
/ [, F2 H/ l2 r {     
: n- @- q  E8 G: W return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
/ g- v& Y  k; ~+ u" [' t! P }              # a! Q7 q. e+ \) e4 B, n' d
      
; ]' N, P( b0 \4 b- |/ Kfunction IsaNull(cCheck)         
6 s$ z/ R- D- J- o {          0 @1 c7 ^5 u+ a9 S) d) ~
return(cCheck != " ")         
9 |% L: M, s$ r4 [5 @" v }                               ' ?2 |$ k, }' r; g
      ; \1 r( r0 y1 ?# `0 u7 f# ?
function checkform()     ! z8 P) Y+ n3 i* ]1 L4 U  ^% D/ `
{ 0 Q: z1 g* ~& s* p# j& Q" f
  id = document.sform1.id.value;     ( j! }  i, ^/ m2 L' p
if (id == "")     * E; K( V+ s; b2 a/ V
  {     ' A& S( o0 }6 ]. D8 {4 p2 f
  alert("请输入注册名");     , A" D! G% u* @' B; b; g
  document.sform1.id.focus();     
* l/ j& b8 {9 K! P9 I6 x/ O+ t* v  return false;     
5 S6 n& y5 E$ f% V6 l0 m  r' B  }     
. h# G: E3 h6 S3 f) c- P( Z     
+ B0 m' ~8 H: e7 U! M for (nIndex=0; nIndex<id.length; nIndex++)     
0 ~( w: W. p4 V1 z, |9 @3 d! u  {     
& U3 F% {2 F# I% q+ v, c5 I) M  cCheck = id.charAt(nIndex);     
9 x: @' w" W( @) ?, a, M/ l9 f7 C6 m* N  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
9 T: e2 p5 o' R4 O   {     
6 b' N6 X8 j' A( ]& e% P   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ) h4 s9 {  t9 l! S$ v: I6 E0 u
   document.sform1.id.focus();     
# P- x& Y: p( G6 n4 S: ], p4 U   return false;     
/ i! Q+ f- I/ s4 |+ z4 `5 Q   }     
! T2 I: _1 g0 }7 Z9 O  u  }
$ K2 E" h* Q, s4 L* S# T" d9 h  chineseid = document.sform1.chineseid.value;     % B0 b. B& z0 b
if (chineseid == "")     
1 {0 n# E9 ?' f4 P1 H' W  {     1 E. @8 C! U2 m0 T# V7 f
  alert("请输入中文昵称");     * d& Z; ], c# I4 n6 }6 \4 |2 J: U
  document.sform1.chineseid.focus();     
1 s% p/ D* q( h: y: U. S6 ?/ k  return false;     5 O/ e7 a* H' z0 }- k6 D0 x, J8 g, z
  }    - \; z3 ^! b3 w" ?/ z* }  b; {
password = document.sform1.password.value;     $ ^; a7 \& C. \4 k
if (password == "")     3 ?" N  R* r8 R) |$ @0 Z
  {     5 `' r) n; Y( i8 D
  alert("请输入登陆密码");     ! [4 y4 `% ?) u2 [' K
  document.sform1.password.focus();     
% H* T- {! P* u; [2 S* m  return false;     ) Y! }9 H0 c9 T4 R
  } 0 x+ S+ a! v9 A5 |+ v) }
password1 = document.sform1.password1.value;     & w* [7 }3 P9 H3 P3 Z* t
if (password>password1)     
/ I: B, e( l/ F  @. m* |' b% M  {
9 C+ q4 c) U: w1 c. h     alert("重复密码与登陆密码不相同");     
6 u9 }5 x4 z  g$ o  u$ k$ S( j  document.sform1.password.focus(); 9 |+ V. s* ?/ d- F
                                document.sform1.password1.focus();     
5 o1 B4 a% i8 ]4 O  return false;! }, b+ M! h# t. [
  }  2 W9 {% j! w5 H* e0 k
if (password<password1)     - r  H# E1 k% Q& b. R, s
  {% }3 r& ?( L8 E
     alert("重复密码与登陆密码不相同");     6 a! w. \- ^9 B% v0 y5 |
  document.sform1.password.focus(); : F1 P  m3 s& R1 N
                                document.sform1.password1.focus();     
$ ^. s! [0 m+ h* `( k5 V3 ~$ g  return false;% b! l8 L+ w+ P3 `/ `1 `
  } 3 N0 U5 X- c9 b8 g% i3 V4 _% e
if (document.sform1.email.value == "")     7 f* Z% R3 B; b, g
  {     
7 H- ^5 A# L+ ?( W6 C. I# y4 }  ?  alert("请输入您的E-MAIL地址");     
9 R1 \$ U2 f4 k  document.sform1.email.focus();     / `( K  i& Z8 H
  return false;     
$ k: s1 O& ?4 ~$ B  }     # Y  H: X' F- K0 P
      
. F- m3 ~* }+ M email=document.sform1.email.value;      ; }; A+ r% K  ]2 T! q
emailerr=0     % |$ I! a4 i; h% E' n; E
for (i=0; i<email.length; i++)     : F: j; E! M+ a% T% [/ O- n
  {     
, x4 r$ m/ Y; t  T( c  if ((email.charAt(i) == "@") & (email.length > 5))     8 B5 D! K3 `9 {& g, M( h
   {     : Q3 l2 d% M' ^
     emailerr=emailerr+1     + e# R0 l/ \2 H2 h! Q
   }     
' v2 \- m" J: b: k  }       D/ e  a* u' Y- H7 C
if (emailerr != 1)     # R5 N" E( W. ?! t6 x! `
  {     / |8 z. o! P9 F# U$ F/ i- P
  alert("请输入正确的E-MAIL地址");     
& a, f0 D( Y) b  document.sform1.email.focus();     
& }5 L4 s- s$ i( V" q4 C  I7 {+ k  return false;     - w8 E9 `: A; ^
  }           3 g& M, V: G* u5 g! u( S# \1 m
  
/ d+ t( t( F* O" J, W if (document.sform1.checkask.value=="")
3 x, |" y! |5 L9 P+ q  {
" ?* L6 J! E: I   alert("密码提示问题不能为空");     
8 Q( W) w  d# R3 K' p% P   document.sform1.checkask.focus();       n" p! F8 ?' ~
   return false;  ) E1 s' _% y; Z$ m" x0 U
  } ! C8 D2 z* P9 a4 ^4 v- |
if (document.sform1.checkans.value=="")
2 x6 G  `# ^5 F9 o  {
" q. H3 K( S9 k# i& b   alert("您的密码提示问题答案不能为空");
* G4 {* o, J: h' r' E, L   document.sform1.checkans.focus();     ' e* M. m0 v& k. L! C2 L# g. l( i
   return false;  
4 u$ v6 o' B! Z3 j! j9 r  }
% i% L. y+ v0 M1 c# T  N return true;     4 S! i2 j0 r" Q" N" f
     
, ?5 q$ @+ M5 i1 K3 V4 @- g}
5 ~, t6 }0 E5 E0 c" ^& t3 j$ R</script>

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