返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:* x/ \4 T8 b$ y& M8 u: P& W9 F/ K9 Y
<script language="javascript">     
0 B' Q/ O( K3 d  qfunction IsDigit(cCheck)     ; f8 T8 {# r  J8 w" A8 J% A
{     
3 S% y: W3 U! W% K return (('0'<=cCheck) && (cCheck<='9'));     ! a* G4 k$ e( y) y  N# Z
}     
9 S. U! `3 o& }+ I, f; a     
3 S$ f: z+ u9 d' U; Tfunction IsAlpha(cCheck)     ! K2 h8 E8 I7 m7 [! G* C0 B. a
{     ( F; f5 w  Z3 X% F
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
' N" X4 T, X; N- [7 Z3 h* b }              
8 R. l& }5 p' K      
2 a- ]- T( i7 y: C+ ^- Ffunction IsaNull(cCheck)         
2 M  \/ X1 {# I4 |! b7 @- O {         
' f# X' \8 e. U) n: A, R' s2 D return(cCheck != " ")         
3 V2 a  `; _- C% w# O }                              
- S8 g+ _9 ~6 A4 Y8 o! C5 B      
6 d0 \' }" u4 r1 C; J$ [function checkform()     
1 X( G4 |. S- r- B5 S& F8 V& d! G{ ! k4 F& u9 a4 t4 X+ \3 q9 I; t
  id = document.sform1.id.value;     
% y( @& G6 f/ b, ^6 D" n if (id == "")     7 q0 X: X% d% T* w
  {     1 ]4 n9 T) Z1 T: r: N
  alert("请输入注册名");     ! c. y. o: B5 v8 @: M, x9 h
  document.sform1.id.focus();     6 e( O5 d. O& z0 x, G7 V2 x1 t/ {+ p' o
  return false;     
% ]: s2 d% r7 Q8 e4 q  }     
6 W. q1 N0 g) W9 w8 O     
" E5 s: \+ X" O" o* o) _+ ~0 X& j for (nIndex=0; nIndex<id.length; nIndex++)     
/ Y; G- l! _) P9 ]+ u) `* F  {     
7 n! @# n- G3 x! W7 G: }* ~" o  cCheck = id.charAt(nIndex);     
7 \* ]' Y7 q$ c: `9 c  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
& N6 e2 A8 O9 G; Y# M   {     
5 @" a( f; ^1 W/ J" @   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     , Z* t) Q% d& E
   document.sform1.id.focus();     & H: v# |5 [# ~) P' ]; U
   return false;     ; s* U2 s4 B) {/ \
   }     4 n, e) T4 e& u+ c
  } : ~' R8 y- ^( J5 d8 j& r: x  C% ?
  chineseid = document.sform1.chineseid.value;     
$ v/ ^, L5 C3 _) m  V1 m1 R& T if (chineseid == "")     
" D) Z, }8 k, ~  {     5 z1 X( }4 o2 t2 F2 j8 X
  alert("请输入中文昵称");     
$ G* J% W) |1 s8 V  document.sform1.chineseid.focus();     " l3 j  {2 Q$ s" Y! P
  return false;     ; g! _7 D. k/ l2 i/ n# j! ]
  }    - r3 t: n7 y/ u8 H% t$ p0 }
password = document.sform1.password.value;     % @$ B$ N) s- D/ v
if (password == "")     / z+ L& c2 N' T( m2 z" I6 X) u2 O
  {     ! v/ g7 ^3 m5 C- F( H' {9 O
  alert("请输入登陆密码");     
+ |' Z6 ]( {) s+ p; W  document.sform1.password.focus();     / q# r2 G$ s# z# ]; I
  return false;     ! A3 A- @" B" ]* N8 f7 M0 u' P+ A
  } ( q# K( p7 w: Y9 q
password1 = document.sform1.password1.value;     
( y) a8 H/ g* A$ y/ t if (password>password1)     
, n( x3 X9 T- K  {2 X8 z6 C: @. h
     alert("重复密码与登陆密码不相同");     ! ^: d5 H7 d' v6 k
  document.sform1.password.focus();
, r- ~0 a" W- @. s# P                                document.sform1.password1.focus();     
& a6 z2 J" }2 }& c  return false;* f1 R" K/ H( W% x- h  ]7 p
  }  & d- A, h- M+ W1 S5 e7 p
if (password<password1)     / r8 U, s6 G5 F8 }, O+ E
  {
/ O2 o$ A* b3 ^  `: D; l     alert("重复密码与登陆密码不相同");     $ F' |7 z, x' E( ^$ x$ [( o" k
  document.sform1.password.focus();
3 s; h- P" K4 [7 q5 t                                document.sform1.password1.focus();     . P/ a6 S- n8 ~* ?
  return false;
2 p' l3 Y6 i6 Q) {% H& R: C  } * Q/ ~- Y+ {9 f: {
if (document.sform1.email.value == "")     7 [1 O% ~; Y. m" k. N, ?
  {     
  l* l# A9 u5 T, N4 B7 [& M  alert("请输入您的E-MAIL地址");     8 B8 ~2 e( A  D. Q7 ^
  document.sform1.email.focus();     
/ F8 k0 |8 m7 Y" n  return false;     
9 U, V' ]! ?9 U7 E  S8 C& W& b  }     , T& M' g, {/ ]4 m/ w+ H
      
: X2 o; M& X$ z. c0 t5 \ email=document.sform1.email.value;      
4 N9 l) L; G4 a- E emailerr=0     ' ~8 `$ f# F0 f" X7 i' [
for (i=0; i<email.length; i++)     
$ R* R  @4 X9 F/ ]+ C+ h/ B  {     
% L; n' r% G$ F* U8 Y  if ((email.charAt(i) == "@") & (email.length > 5))     : S5 e8 ^9 b- r
   {     
& w& v6 g8 Q/ W9 w/ o8 V! G( E  R     emailerr=emailerr+1     
6 {0 p8 @  N. q4 C2 {   }     
' `5 y: J2 ]8 F4 _" R  }     - y, x- L. E% f5 F3 E/ S
if (emailerr != 1)     
. q2 y# o, U/ E0 z& x- y7 W  {     
4 B( L& S& x+ r* F# E  alert("请输入正确的E-MAIL地址");     1 N  Z3 |, r' b0 K- t0 U% k- ]
  document.sform1.email.focus();     # z( \* \5 O& p, S; a  ^
  return false;     1 e$ P0 Z- m/ Z3 _3 P" j
  }           " I9 }) j0 w. B( j
  
/ K7 d* @# p0 m4 p& v' n0 ^- Z if (document.sform1.checkask.value=="") 5 p: y7 C$ [  ^$ O1 E
  { 9 A& }! c4 f7 P, Q. `8 |9 j* j+ G; k% _
   alert("密码提示问题不能为空");     
0 R/ Y- ?4 z* w( \+ {- [5 h3 H   document.sform1.checkask.focus();     1 y# D9 ~: F2 U% K5 ?% c- v
   return false;  
! G" ~& Q. I4 R& E  } 5 p8 L( _' ^$ W7 T5 J7 n
if (document.sform1.checkans.value=="")   s5 i/ v; X' ~' }3 h) d; @7 ?& ~
  {
2 F4 q8 s0 p) ]% [   alert("您的密码提示问题答案不能为空"); # I1 C# k0 ?* d' w3 i7 S
   document.sform1.checkans.focus();     # M6 m" D: e/ f- T3 D& e
   return false;  4 h# t' G) I# n* ~
  }+ V' ~( {7 f5 U  T! T1 x5 J+ [
return true;     
) _+ y& E3 k' o     
- c! L: |0 v+ }}
, [) t0 m' E: t' j  ]! Q! h' M( K</script>

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