返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
, g* C) g4 ~) a. G<script language="javascript">     & F' d# }' ~  |) N
function IsDigit(cCheck)     
( ~6 R) p* `) c! }, c4 R0 L3 d2 [ {     
- |$ y9 {8 f; C# I0 ~6 c+ D return (('0'<=cCheck) && (cCheck<='9'));     
: F+ \$ m& |5 Y1 J* ]8 x6 J }     
0 i' E4 Y, f! X; ]6 p     
  q; L4 H5 y! Y. ]; ?/ ~0 Efunction IsAlpha(cCheck)     
9 Z, a8 \7 w2 y5 x {     . i+ @- E+ j  ^% h, ]! J8 e' G
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     8 c4 o% ~0 }! O! e' {5 n- O. Q
}              
9 u& F+ Y' E4 t# q; b! I      
  a. n  Q# i& a/ U' j4 \7 mfunction IsaNull(cCheck)         
  M. h: x: m4 [  L! E8 G  x {         
0 D- G2 z1 }) W return(cCheck != " ")         
; N& d4 O! a" r; @) I: o }                               4 X/ T5 x5 I( u( [; h) A" l
      
- r0 o6 u! Y* X& W$ Gfunction checkform()     ( X* u6 {5 E9 t. i- `
{ " T: e+ z7 w# }0 D' j! Q
  id = document.sform1.id.value;     # @) _5 r- l2 E
if (id == "")     & W5 Q% T% \+ h6 T2 W) W0 I
  {     6 t; R5 f% Y8 b
  alert("请输入注册名");     , I, b6 b) ^8 n$ L/ ^+ N0 |
  document.sform1.id.focus();     7 A+ l  s- S- O- V
  return false;     ( s1 r( L- s! m3 O, S3 t  l
  }     
! [  H# C2 @, d3 f2 Y8 p- p     % ^- X( f+ |) l  C+ E
for (nIndex=0; nIndex<id.length; nIndex++)     
1 b$ N3 f: O5 f. g' S6 |  {     
& X! K: [$ ]  y+ E5 _: f! H. z$ a; @  cCheck = id.charAt(nIndex);     6 L) E4 L) X+ Z4 f4 I3 r8 n5 d$ m7 d
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
! _  o2 h6 @* b   {     
# j4 G" W. O6 j7 x7 W8 L   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
9 K7 b2 w: d0 ?* F' i8 @- n4 F# h   document.sform1.id.focus();     9 g, A; M& Z) \" \  b
   return false;     
7 V0 ~$ n6 _2 P" n  d1 m& ~   }     
; S8 }5 }: B+ q9 [  }   I* x0 C, a; O: r; C! S
  chineseid = document.sform1.chineseid.value;     # @! r* @: A" I( }# w
if (chineseid == "")     
7 f' G/ P, @$ |  {       {9 w4 r+ k" `$ Y
  alert("请输入中文昵称");     
( U2 R1 z1 m8 f, S+ J9 T  document.sform1.chineseid.focus();     
' V: g4 }1 t* C# e8 |4 F  return false;     # E7 |/ r" o, [, V" J. U
  }    ! w! r- |, L- R5 x* T: U6 i/ T
password = document.sform1.password.value;     2 H# f" D; H. {/ u
if (password == "")     
: F  T4 E; _4 o1 n4 a4 L# N  {     
' H7 @9 [; o: }. ^  alert("请输入登陆密码");     
. q7 B( r$ a% `3 s+ t! z# z  document.sform1.password.focus();     6 G/ C, g9 k- S7 e# r
  return false;     
2 V4 g8 a9 n; k  n( g& F  }
) |* l$ u/ k: g& g" J password1 = document.sform1.password1.value;     
- A' r. V3 ]8 [$ P6 C# n+ ? if (password>password1)     
3 D. j" |  m- |5 E. E7 m# ]( y  {8 R8 U" F1 K# [4 [0 Y+ s* }) h
     alert("重复密码与登陆密码不相同");     # C# m) I+ G' S% r7 d* l6 g% K# k. ]
  document.sform1.password.focus();
2 c5 l2 a/ P3 w1 E9 p  O( Y                                document.sform1.password1.focus();     ) r' Q- E" g' l
  return false;7 w. W) O$ k, K+ `+ [4 c
  }  
  Y; f0 R, A9 iif (password<password1)     * C5 m; N3 w, V9 ?" [
  {
0 v) r# ~4 l) n+ \7 [2 K     alert("重复密码与登陆密码不相同");     
  J" o& H2 l8 E! g2 g  document.sform1.password.focus(); 3 b3 E: g' ]: t& N# b
                                document.sform1.password1.focus();     
# O% H7 ]* s9 S& y9 W+ R  return false;/ x" w! a) R2 o
  }
* ~  L0 d$ D. V; [6 o- K6 r- j if (document.sform1.email.value == "")     8 G$ @: q9 D3 U! _' i3 s  }$ b
  {     : V) i5 V/ T! v4 I- {; [' }4 }
  alert("请输入您的E-MAIL地址");     
) M1 ]$ X: c0 _& }/ s* R- D  document.sform1.email.focus();     
( T, @5 V& }4 |% T  return false;     . z8 w9 [  r  I- o7 U& M9 G' i! G
  }     " E+ n3 |& u) t; w& J  A
      " X0 x: v7 y* L! W: P
email=document.sform1.email.value;      & {- i( B. g$ p* B: A& \4 q9 x4 n9 e
emailerr=0     
* s2 {+ H6 v& ~ for (i=0; i<email.length; i++)     3 q" Z2 C3 S8 ]& Y
  {     : {( A: X. o" w8 G/ A
  if ((email.charAt(i) == "@") & (email.length > 5))     : U# w4 h7 G* W6 N" Y
   {     # f- D$ C5 Q) L* d" T
     emailerr=emailerr+1     : T7 ]5 `8 q, _  x' M! C
   }     
8 D/ F0 R% U! ?% v  ^4 T. B6 l  }     " v% R/ u: }7 S/ A
if (emailerr != 1)     
5 z' v2 [9 Y. w/ R1 b3 v  {     
/ i, Q# S' v7 x$ T# e  alert("请输入正确的E-MAIL地址");     
2 c3 z2 B: |5 X+ I7 T3 F  document.sform1.email.focus();     4 m% J) ?: j! u- |$ M1 N
  return false;     , ~5 }, _6 G, [/ c4 H5 S+ P1 G
  }           3 o- u2 s( m4 [% a
  
8 e* C4 B! J4 N if (document.sform1.checkask.value=="")
. U2 z& K) Z- C  {
3 P) x) Q# G5 ~% f. `& k   alert("密码提示问题不能为空");     
. m4 D: g% y; l3 A   document.sform1.checkask.focus();     . Q( ?. f! E9 G! ?" w4 r
   return false;  
8 Q+ e" ~8 l0 w: j3 f  }
2 F! u* q. z) Q# T& u! f if (document.sform1.checkans.value=="")
8 s: W2 A, p  ~/ l0 a  { ' p: P' q  i' t, `; U! q, I
   alert("您的密码提示问题答案不能为空");
2 m" M) {& X3 z4 H, }& @   document.sform1.checkans.focus();     
/ g0 n, ?& n$ j! _3 h   return false;  ; a- S/ _; ]' x' K+ f* H% Z$ S
  }
/ T' V1 n* ^* \/ Q/ W( f. V5 i return true;     . ~1 d! r  S6 W1 [4 g( b& W
     2 H/ f5 q3 M! g6 ~: C; S
} - U1 M2 Q- V- [  E# Z7 E
</script>

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