返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
9 e3 g: A8 t  A, ]- `/ q) ^% A5 V" _<script language="javascript">     
: q0 Q2 P6 ^. T6 w$ {function IsDigit(cCheck)     
; T1 c" l- [0 Z5 ^: w {     7 [! W; V7 s, [% F
return (('0'<=cCheck) && (cCheck<='9'));     7 P' q) g0 S5 g; ~% B" G
}     2 C& N& U4 ~: c  C
     ( W' Y0 [9 ?1 j3 v2 f% S0 F
function IsAlpha(cCheck)       }) f% n3 v5 }% h" u$ f3 g
{     
0 H) T7 H+ M2 k return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     " {; M) q6 E9 ~
}              
0 [0 F/ l3 S5 D4 q+ _      
5 |/ t# |2 d  h# _4 Rfunction IsaNull(cCheck)         
' Y5 K3 @, D( S# a1 G8 Z6 l! H2 A {          " z% W8 Q! B" A
return(cCheck != " ")         / W0 I) |. d6 S; F/ S. A
}                               9 T" J' L; W$ X$ |6 _3 N
      
7 f: p3 Y7 H7 _function checkform()     ! U. X, e* T( S) X, L
{ " Q7 I8 h; _/ x
  id = document.sform1.id.value;     
, h3 e- d9 {. p  G" X if (id == "")     7 Z' A' n! V' z# }! e+ u
  {     
# f& _5 h# a7 l" l% x  alert("请输入注册名");     $ ?8 U$ `+ F' F# E/ S5 @9 F
  document.sform1.id.focus();     
4 Z! i7 A- x2 x' K8 q$ X8 V8 Y  k  return false;     
' b% `4 a9 x# d& x9 J1 y/ R  }     
$ @2 y$ N/ T7 f0 e2 p     % d& l6 j- G% D* X( a4 Z# T
for (nIndex=0; nIndex<id.length; nIndex++)     
/ f3 V# m6 ^  c" t! K  {     
# e/ q  o" m* K5 o! x  cCheck = id.charAt(nIndex);     ) v+ B# P8 S# z/ b) p. `, K+ U
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
# b3 W8 A7 c# ~. h! S$ F9 ^5 G   {     
9 u5 u# t$ T7 M" X- J& M4 ~/ Z3 I   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     9 k3 ?" P" \2 o1 f5 F6 \, E
   document.sform1.id.focus();     ! {7 T; p9 |/ X5 `& L$ ?% [
   return false;     
2 K) D! g" X4 K( `. d   }     ; s  U: n# S7 D+ U; O
  } # v, ^9 H1 L9 x7 m
  chineseid = document.sform1.chineseid.value;     
0 V" W  @8 j+ b" @* s if (chineseid == "")     + r; K$ n/ C) ^! n
  {     " n. y+ @# e. E& k% a5 v7 G: ?" |
  alert("请输入中文昵称");     
2 Q9 J. o2 Y4 p( G& o" r. j  document.sform1.chineseid.focus();     
8 k# u( C3 _- k. }: ^. k  return false;     
; V, S5 T$ K5 ?- |* ~  }   
9 Y6 v4 f" e0 y" Y& M password = document.sform1.password.value;     
) X) ^1 Z% c/ O( _+ b if (password == "")     
% L4 m' e1 A+ X- T" @5 F  {     6 r3 l" j$ G: S& @4 s* i2 [. [
  alert("请输入登陆密码");     
3 P- b9 w/ ]: \1 @$ H4 i0 Q8 V  document.sform1.password.focus();     
1 Z- P) T* Z/ S0 R2 j: Z' R  return false;     
7 w+ s- N$ k& I3 _  } # q( v. ^! _% P8 A  {
password1 = document.sform1.password1.value;     
4 A, r1 K2 G! \ if (password>password1)     ) ^( Y. I5 K  ?! e  }
  {! l& [6 V8 ?' p- u" ?# Q+ ~2 r' W& c
     alert("重复密码与登陆密码不相同");     
. U: P* }/ l; D: E  document.sform1.password.focus();
) [. T9 {( s1 j                                document.sform1.password1.focus();     
; S$ ?& W( \0 w8 ^' a& q8 \. F6 y  return false;
2 P, f* R8 T6 P. T9 E, O& Y  }  
( r3 x! G6 D8 B( A) W% V1 Cif (password<password1)     
7 E$ ]2 t- I. J& l5 C% `$ o  {: D. C/ L( s. r( i; ^+ }
     alert("重复密码与登陆密码不相同");     
6 Y& ]) |, D- B4 D, I  document.sform1.password.focus();
  H3 V" i0 F4 U) ]                                document.sform1.password1.focus();     
6 a: C/ V$ F7 q( ~& ~! h  return false;
) U% X9 J) X8 [" I  }
6 u, f, P- k7 b; j- |2 U% j if (document.sform1.email.value == "")     
4 i1 @- g9 B& A, k4 N/ Q* j  {     
6 K' J" N$ U$ u: P$ x( @1 q  alert("请输入您的E-MAIL地址");       _8 ]) Z+ o; G3 n
  document.sform1.email.focus();     , z: u+ i8 }9 p' j
  return false;     $ z! s6 x# t5 C0 i
  }     ) ^$ g: ]# \1 T
      
7 T' t0 c, W3 V email=document.sform1.email.value;      
/ Y& L0 W8 f- e5 {" B$ ^' Z" ^ emailerr=0     6 F9 V( l+ v8 i5 h' h! f: c& G3 M
for (i=0; i<email.length; i++)     
+ z! f1 J% ^6 [' U  {     
/ n; R9 ]1 O4 d, D& i) Q4 T  if ((email.charAt(i) == "@") & (email.length > 5))     5 J% ?: M: k% k; b
   {     
2 ?! X; D) B* X& e. `9 \     emailerr=emailerr+1     
* p( U# m+ R5 E; p  V, M   }     ) n7 f1 k+ t9 N) B+ K' M5 M
  }     # u2 K" u9 @( [* Y2 O1 g
if (emailerr != 1)     
& [9 g, y; m% _! g" \4 k  {     ) X/ o3 L2 ~/ t3 \
  alert("请输入正确的E-MAIL地址");     0 U3 y- n0 H3 ]# j: r' W# R2 ~
  document.sform1.email.focus();     3 ]1 I' E% H$ J& s' x- @) k1 x+ E
  return false;       J4 F) j" o6 k3 Z8 i
  }           / _2 Y; m, C4 c8 d; Z& ]
  
( m7 J) g# j: x( _" j! C: [( l if (document.sform1.checkask.value=="") 1 t- M- s9 f. K3 L" g* h* g
  { ; W2 @/ N5 T7 n, s5 G6 E9 c( K5 o
   alert("密码提示问题不能为空");     
7 [+ K3 [; z) d. @: s* e   document.sform1.checkask.focus();     
& ^2 _& G2 i) v   return false;  
5 P$ ]* E, K/ J. v% V2 n  } 2 u3 \6 I, ^/ q  b8 {  W( \! L
if (document.sform1.checkans.value=="")
1 m6 _: F3 a$ J( B% S( l8 I! L  { 9 B: x: m  j3 [. c& o
   alert("您的密码提示问题答案不能为空"); ; [9 H3 v/ T6 t8 z' B& c
   document.sform1.checkans.focus();     
, x. c5 K* [1 f1 K' z( x   return false;  ' K# ]/ [" j9 R: f( T9 r
  }
6 p' L4 n9 b8 q8 V return true;     # _: l" [; Y2 K7 {
     
- v1 c6 l' S1 {; q: e+ L- R} 0 w, Y, |! g" [2 S1 y
</script>

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