返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:  s1 b" w+ p) `1 \9 z. o1 |7 t5 f% ^
<script language="javascript">     % ^" G7 `0 `* @& M
function IsDigit(cCheck)     
1 Q/ H' M7 ^5 J% s6 n9 n% k7 \# i {     
+ s% m2 s/ ~5 l* Z' B* p return (('0'<=cCheck) && (cCheck<='9'));     
* P- Y7 `$ N+ t* {4 L  S! i& E }     $ E% ~( }, w8 R  k6 @+ Q- o
     
/ ^  H, P* a7 M$ N0 ]function IsAlpha(cCheck)     
: v3 H- X) X' u! G0 Y {     
8 ^- D& R' H; t1 s( F return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))       c$ K% o% M8 i& h( L
}              
  F9 ~$ T. T: Q5 P      
+ N+ X" g8 `. C' r( m$ e$ M0 V/ rfunction IsaNull(cCheck)         
2 i* D& o! J9 m' ^ {         
' J; _: P6 D( s4 u; H% R/ [# n+ c return(cCheck != " ")         
5 V# s4 l+ A; T }                              
& c; v* A3 _# E      0 o1 r" i# K4 {
function checkform()     
% j0 P1 X' R- R{ 6 ]% R$ e  i* Y
  id = document.sform1.id.value;     
9 A5 K6 h# [! O: x* V if (id == "")     
# \( q5 h& N% p  {     
# `! r5 T9 n- A  alert("请输入注册名");     
7 w+ z' @8 d' Z. \, h$ A' G" K0 x  document.sform1.id.focus();     
! z8 v0 q* j& ^+ Z# X  return false;     
9 I, V* p. r3 ^$ X, w+ g' T  }     
/ `% Z$ x; J+ n% A+ C: x     
4 t! W/ D" J7 E; }  j; A6 J for (nIndex=0; nIndex<id.length; nIndex++)     6 D. c+ T6 W" M# b+ C* y* _2 I
  {     5 e" u9 ^+ K' K- t+ y
  cCheck = id.charAt(nIndex);     
, L: k# [# s* H5 r9 [  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     , Q5 ]2 k  w2 H6 j# `7 P
   {     
) r4 n3 s6 ^$ X3 M! f   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
9 c' m" V0 v3 y! V. i6 _6 g; y# j   document.sform1.id.focus();     
9 o2 g/ d7 Y* w- k" T   return false;     
$ n" w+ p* a. F6 c   }     ) M$ I! b5 L8 I4 N1 l+ P
  }
# q2 t* K6 l/ ^3 K) L& p* }  chineseid = document.sform1.chineseid.value;     : W- F5 P9 L% g4 ?& W+ c
if (chineseid == "")     
3 W) L" n1 Q7 J% q  w3 ~( `' z0 T  {     9 o/ l9 p0 R# c( [  ?
  alert("请输入中文昵称");     
  e% u0 o  t& Q* \6 ]! t  document.sform1.chineseid.focus();     
4 D/ T0 @  v# c: Q  return false;     7 W2 m: S, o8 T' `  V$ d
  }    ' @: i. s% f- B
password = document.sform1.password.value;     ! e2 G) Y1 T" [! V0 Z% C: J9 r
if (password == "")     
( Z1 r9 a/ m4 l; c. S" i1 G  {     
% Y" V, {+ o% P1 X! U  alert("请输入登陆密码");     3 i1 a! |) ?; n0 L
  document.sform1.password.focus();     
% O9 a) z1 |& u: n% B" N  return false;     
: O# B0 u- K- n0 t: |2 G  }
, d0 V8 g3 |3 D4 f9 A; T/ @ password1 = document.sform1.password1.value;       e; M0 m3 D% n, K" f; `! S
if (password>password1)     
! {4 ]* W; C: Y$ U- L0 ?  {
& Z% `$ n, |8 O3 @. ~7 _     alert("重复密码与登陆密码不相同");     ' A( }* m) b. S* m0 {% M; x
  document.sform1.password.focus();
: M# X' y, m3 }, y, w2 ^% r                                document.sform1.password1.focus();     ) W1 S, T2 ~1 E
  return false;
- T8 Y4 f: e- p  p) Q  }  / g2 f) `; O7 |! w; _0 a- a
if (password<password1)     7 k/ ^" w% H' L
  {1 ?& v% A# t( _; R
     alert("重复密码与登陆密码不相同");     
2 j+ \9 K/ u' g" R# _( o2 X* f  document.sform1.password.focus(); / ?8 z; x- @) r
                                document.sform1.password1.focus();     : ^& N% m" U2 y1 b& U* I, O
  return false;$ f, M0 _6 f: c# F8 q2 Y
  }
7 C* U8 M: @% J if (document.sform1.email.value == "")     $ A" `- k4 |5 O2 B" [8 G7 n8 F
  {     5 A# U% z! n; E7 k7 E& F0 ?
  alert("请输入您的E-MAIL地址");     ! d* R* Q+ i6 A" f- q
  document.sform1.email.focus();     6 o" d7 X+ i6 V9 h. e
  return false;     
! Y, h+ o5 o. J5 K( V$ S# Z) _1 S2 r  }     
9 J) l# n1 w# u4 _2 R6 e      
4 ~/ }" C! y( n( R email=document.sform1.email.value;      
4 w1 S+ q# h" \9 n* B& H9 I emailerr=0     
. F5 N1 `7 Z" f9 v for (i=0; i<email.length; i++)     
3 {) T% ~4 }! f' a  {     / G9 G5 k! Z& F! ~, m8 k" h" q! r
  if ((email.charAt(i) == "@") & (email.length > 5))     ! S6 \7 x' S% b! M
   {     " B8 ?' x8 h$ F
     emailerr=emailerr+1     # }& o, s+ E1 y, X: d+ [
   }     2 s9 _- Q6 [& M' [( l/ P7 g8 f& j
  }     
) R. f0 s4 n3 [5 e0 _ if (emailerr != 1)     
- Q  d9 q. |, q0 h3 S* r3 O" H  {     * H- Q! m+ t$ q0 q
  alert("请输入正确的E-MAIL地址");     
- g6 X# M/ }6 A% i: ]: q4 W  document.sform1.email.focus();     " J7 c- P  d, o) a3 e" Q6 j
  return false;     
. @& q0 P, k# ^3 W% j7 l' |7 L  }             e0 e/ N5 O0 s! M0 r. \
  5 s( b& l. D9 C" Q! F0 f
if (document.sform1.checkask.value=="")
5 N$ S( Y, P3 G4 x  { * s; N- V: ^$ ^
   alert("密码提示问题不能为空");     # p9 P" k% A/ B( [, j) i8 l
   document.sform1.checkask.focus();     $ b+ H* E$ }' V/ Y* v+ u9 U9 ?+ a& u
   return false;  8 j  X9 B9 T; v, P9 U* |
  }
; ?3 \4 O" p7 R! |; ? if (document.sform1.checkans.value=="")
/ n" F3 D. G8 i4 v  {
% c) u, U* @6 x0 J, ?+ n3 T9 V   alert("您的密码提示问题答案不能为空"); 1 ]! T% p# d0 r7 v2 y
   document.sform1.checkans.focus();     
9 {( @* X0 }& P' d   return false;  $ P9 V* O: \* k( V- T' a2 d: W
  }
2 h9 U6 i- X: `3 W/ W return true;     
* Y; j+ r' h4 n+ G3 J) A" h     0 d; \8 Z5 |9 [" Y, K$ ^! P
} ) K3 J# O9 t5 s; X
</script>

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