返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
) A  \( F$ q* a( t# ?6 m" G1 A<script language="javascript">     
& D" P* P2 Q/ z5 ?. Bfunction IsDigit(cCheck)     
; X) P! V/ U/ p( A* C+ B0 e1 _, } {     
% b( t. G6 E! h+ o. y  h& E return (('0'<=cCheck) && (cCheck<='9'));     2 j# y" U3 s% ?5 b4 h' L
}     , {( z  H! n# T0 o& F
     
% @3 T( l  s2 g7 v7 f. O  Yfunction IsAlpha(cCheck)     
, H- u7 s/ |3 g; |" ]7 ]8 M {     
1 i+ E3 Z) j- S2 d3 _, c% V  t return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
+ d& C0 E$ r; u  j$ [ }              
0 J( \3 F: E( N  `      
+ H! u& ^* k1 K* Q& jfunction IsaNull(cCheck)          - j0 r2 Z/ D1 o! k
{          2 N! o9 G% B5 J7 T$ ?+ D
return(cCheck != " ")         
1 ?. \: j4 `" C' F1 u1 O7 F: p }                               ! x2 s9 q* G& q/ \* q( f
      * X/ Q1 B, c% P$ ^
function checkform()     
, d. S; w# b% `* E% l* ]{ ) B# M. F$ h: ?$ @2 U8 a* j
  id = document.sform1.id.value;     
$ q5 w8 i$ u+ `7 r& P. Y if (id == "")     . a" K. u' U  A) }# c6 A# T  j4 g
  {     1 r* }  `8 b) C# p8 Q$ U4 ?
  alert("请输入注册名");     
: p* H* \" g: j9 Q  document.sform1.id.focus();     
" _: y# E$ C9 W2 X9 t6 a/ z  return false;     0 t( ]; x. [% m/ ]
  }     . a3 ~7 ]5 h# G, w; _
     . n% g/ b" d1 o: f. ?% y# @
for (nIndex=0; nIndex<id.length; nIndex++)     
4 I% H; K% P5 u: z3 L, m  {     & ]" y. Q: j5 m, R$ _. I# }
  cCheck = id.charAt(nIndex);     
. B; C  _, h/ g* R- W. I% \  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
# @. k( o# p9 {3 m  J0 P: Q* \   {     
! U& t7 _% B7 b, T   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     8 `+ [1 g8 U$ H' w0 t  K
   document.sform1.id.focus();     7 F* p5 Y6 I" }2 }" H6 w# n
   return false;     
' v, m$ h+ J1 W8 w   }     ) _# _7 s6 J2 c, I9 L
  }
9 z! d- K- J( o. \: ^: f  chineseid = document.sform1.chineseid.value;     
0 g% }8 d) }& j) g) @3 q) d( P if (chineseid == "")     
% W5 n9 G& h; o  {     : e' W7 ^1 c- A5 T5 t8 T
  alert("请输入中文昵称");     
7 K" s1 b' M4 c* w2 z* b0 ?4 g  document.sform1.chineseid.focus();     
# e9 \5 T  C6 w4 ?0 K  return false;     ( H! c0 B* ?4 P
  }    6 T0 c5 ]2 S, |& E" N& L
password = document.sform1.password.value;     ' k# F- L; i: U/ A, ]
if (password == "")     : E; K. e3 @9 j
  {     
  r/ P% h! G! w. z$ d9 \$ L) U; e  alert("请输入登陆密码");     
8 W* C4 O8 t- t0 t2 M5 [; u  document.sform1.password.focus();     
0 b  l5 w, _1 D" m+ {  return false;     & f& X0 A0 u0 d3 p( G
  }
# E4 J7 Z) y4 I  X password1 = document.sform1.password1.value;     
* U* h- R# q0 i* }% U if (password>password1)     5 ]% a, ?5 N& \% p7 J0 j
  {) M7 C: {* ^# H, h0 g7 g8 g2 {
     alert("重复密码与登陆密码不相同");     
7 r) f, H, j% W' i  document.sform1.password.focus();
# x9 \! W& v% l! @  g* x; m* W" m                                document.sform1.password1.focus();     
: N' W6 `) e0 I0 r* A  return false;, N# C- |& i* a. t! f
  }  
% I2 y4 W8 C, w0 o9 O5 jif (password<password1)     : P% }- E' q9 N  }
  {- D0 t/ }9 e) [4 ^4 b$ E* {
     alert("重复密码与登陆密码不相同");     3 y0 J, R- z8 v/ C
  document.sform1.password.focus();
" f. [  `5 Y. g9 B                                document.sform1.password1.focus();     6 c. d' X+ L/ _# z
  return false;
" Q& \/ @( m% E: p/ E  }
, z' p% n7 A3 Z0 ]+ p% _6 e8 [: h if (document.sform1.email.value == "")     2 r+ O2 Y' H6 G+ o# w
  {     $ A/ r) z8 f) X9 {9 n
  alert("请输入您的E-MAIL地址");     6 `1 s9 v3 ^2 a* g
  document.sform1.email.focus();     ' y* ~# R7 m# ?- V5 C
  return false;     : u, |2 x1 t$ e3 g
  }     
0 U: P) l3 E) C( D. U$ Y      
- i. [1 X& W  v! V6 ]/ s  Q4 q email=document.sform1.email.value;      ! V0 A2 W0 ^# v; d
emailerr=0     ( ]6 J* `9 x2 N, O* c8 d8 @' D% ]# ^
for (i=0; i<email.length; i++)     
  b7 C0 U' E& r' y  {     ) ?8 w& ?+ t5 G' t# J7 x
  if ((email.charAt(i) == "@") & (email.length > 5))     % b* N* F3 a4 M% P2 B8 E! ]
   {     1 u9 ?% l- O* ]' g( C! Q) ?
     emailerr=emailerr+1     $ R# h6 v0 |: U$ X: @
   }     * s: |1 H- Z  k, z
  }     5 \- b) E6 p+ [5 F
if (emailerr != 1)     ; {* ]  k1 N9 T2 v" ]3 g8 K
  {     ! R" O7 @2 _2 m. D
  alert("请输入正确的E-MAIL地址");     $ J4 q! g: ]) B9 ]' D; y
  document.sform1.email.focus();     
% i& G3 N) E, u+ G& y: b2 k  return false;     % C, u5 p1 w; s1 p7 _3 t8 G; x! i
  }           
4 v+ B& Y$ m! ]$ O1 x, p. P  + d7 S9 h* [: q8 B6 h
if (document.sform1.checkask.value=="") 7 M  I  c* N# v5 m/ a7 c" t
  {
4 f6 e/ N! K2 m/ d   alert("密码提示问题不能为空");     
4 v( X+ B) K- S! V/ }, [& d; P   document.sform1.checkask.focus();     
& E  |  @2 G* Y   return false;  # y$ n! n( Y# i) n
  }
! h( ^) z: H2 w# e/ i( t" k& K* m if (document.sform1.checkans.value=="") & C5 m; W7 a# |& Y: s
  { 4 S# f  r9 f, R) J' v0 }
   alert("您的密码提示问题答案不能为空"); % z* y, K6 ^; E8 K, N3 ^: e
   document.sform1.checkans.focus();     : w( Y& m* G4 b4 J2 S1 F. h- U
   return false;  ( ~: G6 V$ e4 Q$ Z) a+ n1 d
  }
% [% V) i- \! ^4 ?. }. I return true;     , r/ _- `. y* Z/ J; t
     
4 i4 K  E2 D! I7 [3 x9 ^} $ }9 V9 G0 f7 I
</script>

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