返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:$ Z! g( N5 v6 e5 _( f
<script language="javascript">     
$ e2 I7 H( Z7 z; H' ~* e/ B7 Sfunction IsDigit(cCheck)     3 D% c7 G) }4 K: d
{     2 ]% I5 Q$ j$ M! D: t* j  u6 e
return (('0'<=cCheck) && (cCheck<='9'));     5 g6 R' n& z9 o7 G' e( a, N1 u
}       \% Y# @/ q7 j
     
( t6 S5 I9 ~+ e( y2 tfunction IsAlpha(cCheck)     ' w( g7 n5 V! o/ U9 h, a
{     ' t' m3 T, H( L, u  f
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
+ h( g3 V9 _# c& a9 T4 j }              . M( V7 e% m7 J1 O
      * e( ~* `5 e5 S7 ~$ j+ R
function IsaNull(cCheck)         
& X. V( c3 t0 x {         
# t; c4 L: ?( {1 A& P: _- Y return(cCheck != " ")         * P# C4 E- K# J( m1 b" X' o
}                               0 T3 o% T) n8 f! V" p
      
2 h6 r' m5 [, B8 f9 @9 B- H( {function checkform()     
% m$ `- |5 |: o& g5 S8 P{
6 t6 n% ]9 ], J' b. x* z5 \  id = document.sform1.id.value;     
* \. t9 P8 X6 U8 [ if (id == "")     
8 }9 ?% a8 T* W9 f) k3 |& I) R  {     8 p6 i/ O% G$ o- C- b: r1 A
  alert("请输入注册名");     
" W; Q% T7 ^! c7 n, g% g; f  document.sform1.id.focus();     % J) x4 x  `( I6 P5 g! x3 ]
  return false;     
6 K& X& h$ d) o1 v: s& H  p0 Q  }     2 L+ w' {. s( W# Q' V# o' {
     % _# c  }4 D! G% a! E. f
for (nIndex=0; nIndex<id.length; nIndex++)     
# _- p1 U5 X, H2 e$ u/ z  {     5 ]: [' b& _% z! P
  cCheck = id.charAt(nIndex);     
; x. r$ u; j  n7 M+ b/ t7 N  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
+ y: h, k" C- ~+ R   {     ) U- ^0 d& i# j9 Z- T7 _5 k) D
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     3 }  [3 [# p, n7 l1 b& i
   document.sform1.id.focus();     
: ~2 ^5 r5 z9 o: H7 I; m   return false;     
) N2 E3 h8 \  M) r$ h   }       f; e3 X$ K. M4 L
  }
1 A7 L$ g- c* }' b+ M9 |+ s3 `  chineseid = document.sform1.chineseid.value;     
9 @: s8 D/ [  i5 n3 H: t if (chineseid == "")     
( M0 d3 z/ a+ Q4 E, z' A  {     - G& T6 u+ v6 h8 H# B
  alert("请输入中文昵称");     : _+ f7 `: b/ {1 T& u7 n2 B. F; E
  document.sform1.chineseid.focus();     
' G, c  @1 U3 p  return false;     
1 g$ G) ]( [' U1 D) z: c  }    - ^& j5 R3 [8 R
password = document.sform1.password.value;     
3 W& @* ~7 [% ~! E if (password == "")     $ Z, W% X) ?) W0 v' L
  {     
) `! f$ {  A" u% v# _9 E  alert("请输入登陆密码");     , S- b& R! l3 {; f) b- R4 R
  document.sform1.password.focus();     
& ^( r* f$ t2 Y& x- J  return false;     
% b+ e% U; W( w8 I, o6 D: Y: t  }
: K$ ^, C: m4 x2 ~' [ password1 = document.sform1.password1.value;     
  v/ Q: N) W$ D- B9 x9 N+ k if (password>password1)     / {& o! [+ H$ C, p2 V
  {
& D- n' y$ ]& d# e. P     alert("重复密码与登陆密码不相同");     $ I% D5 f1 E; O; G% E) j8 J' R
  document.sform1.password.focus();
3 L' ~, p. y  K) C# }                                document.sform1.password1.focus();     : b: j) b. m  D
  return false;
. L6 Y4 ^. R9 g# w  }  . {/ x2 ?9 S* i% z* t
if (password<password1)     ! s' G6 d) W0 O) `; e
  {
+ o. W2 d* j# H$ \  ?# v: ]8 j     alert("重复密码与登陆密码不相同");     8 Z6 w; k. D& [' a
  document.sform1.password.focus();
0 n0 K! w# _  S# i4 c/ l                                document.sform1.password1.focus();     & z! j1 d& s6 |
  return false;8 |: m, {8 d! o; b& Q8 b" s
  }
+ E. b3 g  w7 k' l, I if (document.sform1.email.value == "")     9 ~* c& Q3 D0 B+ ]8 I6 X
  {     ! T5 t) r! X' I: v
  alert("请输入您的E-MAIL地址");     
+ f) e$ M) K: Q, g9 i  document.sform1.email.focus();     
4 p, d! C7 v# d& ?  return false;     
% s3 L- W3 [; g2 [1 d; V  }     
7 _0 O8 u" _8 p  D1 X      8 ?7 I. H7 q) q4 v# A; P0 V! E, ]
email=document.sform1.email.value;      5 Y  s9 N' w$ S0 c+ L+ ?
emailerr=0     
! ~9 Q! H7 o- c for (i=0; i<email.length; i++)     9 V3 C4 p& ]0 S! O* b
  {     
1 i4 i8 |5 N8 |- d  if ((email.charAt(i) == "@") & (email.length > 5))     
: K; |2 n+ ~: J' |   {     + U- t" D2 b0 Y3 c& [9 Z
     emailerr=emailerr+1     
2 [1 e$ W6 w2 P" B' `- c: U   }     
4 w9 w2 s/ m2 W4 A. j) M  }     1 i; D, m0 i4 u- ~- g
if (emailerr != 1)     % h4 Q# \" M& q+ _
  {     
5 ?  E9 B9 Q  t1 B8 Z& ~1 i  alert("请输入正确的E-MAIL地址");     7 o  s( a+ r; H' M; E* Y- C7 t
  document.sform1.email.focus();     ( n0 I- I. d3 p* C8 H
  return false;     2 J: n9 s; H1 ~. V# P3 r
  }           
; J3 X; q6 Y6 C* b  
. q' h! c- F/ C8 W if (document.sform1.checkask.value=="")
! g/ r) D# j7 C. q: e  { 8 U& }, `, d1 Y9 V8 ^8 T' K
   alert("密码提示问题不能为空");     
: I! e1 F- e$ Z; D   document.sform1.checkask.focus();     
! c! e2 A4 C5 [7 }( o" a   return false;  7 |# e0 v/ ~4 i# `: I8 i
  } - x" w9 C4 R7 R
if (document.sform1.checkans.value=="") + C) @+ L* D8 @) ^" s
  {
/ u; b: K- V5 `% f; R; R   alert("您的密码提示问题答案不能为空");
% ^2 k' F5 M! r: w& S   document.sform1.checkans.focus();     
' |/ ^0 L# t7 f2 _- D- j   return false;  9 E5 I/ s2 b& c1 s
  }
$ a8 G; z+ d" h3 Y1 i& F return true;     
' c# j" P0 R/ i4 I/ u7 h( g) \     
$ T3 H8 z1 y0 r: @# I6 r} 5 [6 v. ?$ I7 |" _& |
</script>

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