返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
% {% v# m' F+ q( W0 m* g9 s6 X<script language="javascript">     
3 T+ p9 a& J* ]. ]9 d$ _5 d8 c: _function IsDigit(cCheck)     9 J6 T6 B9 w* W" b+ X; o
{     
( o; D3 b% j; b  D7 ] return (('0'<=cCheck) && (cCheck<='9'));     
/ z4 U3 ?% e$ m- A4 e( }: x }     . i6 ~, i% @) E9 J- f" A
     
) |/ R' m+ |( Y; d" q2 kfunction IsAlpha(cCheck)     1 u# t1 z, t/ j& Q
{     
4 Q8 f. y! L5 e9 h, g return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     . M8 s/ ~  l& |( e# z
}              0 n$ R# {4 h6 a. A' I; Q+ n
      
- i0 X5 P9 c, u# Q6 J1 Cfunction IsaNull(cCheck)         
/ z8 Y2 |3 K- j- W" a {          ' M4 e- v( }- r/ P% {+ c  \9 Z
return(cCheck != " ")         
' _# m. h( M$ |1 [ }                               6 G& }+ y" n2 e+ P4 w. @
      ( ^$ z) Z8 k3 h4 V: f
function checkform()     ! }: r( `; H' c+ W! p0 w2 d9 Q3 m
{ 5 \' n( K4 @0 y" ]: F# J
  id = document.sform1.id.value;     
' R3 Y, i( ?4 X2 f, ?6 y$ ~& ` if (id == "")     
: q8 o) G8 |4 g8 ~- ~  {     
# Y) T- L' [# b+ \3 m  alert("请输入注册名");     9 A$ _- x  N/ D8 f( Q% @
  document.sform1.id.focus();     
  z; k; w- L; f' ~( u: A, \8 u  return false;     & q6 p3 `3 N+ m- B
  }     
3 i7 \% i( o7 X9 c     9 m: n) m6 u0 Y8 _
for (nIndex=0; nIndex<id.length; nIndex++)     
- H- t4 ?' r$ y( K, f/ m7 Z  {     
% g. A& v, w5 y8 @. x  cCheck = id.charAt(nIndex);     
/ @) V0 ^4 C( C  O4 f  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     . K# N. r4 @& i2 ~1 A
   {     + K+ O. C+ ~4 m- b
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
: {9 L# T4 o5 h' h) O   document.sform1.id.focus();     
3 w0 A/ V' [  v( q* z   return false;     
' n4 I4 T! l0 P   }     
! y+ K: g7 g! Q. R, G1 T( R  } 4 \; r5 P0 z8 U8 m
  chineseid = document.sform1.chineseid.value;     
1 O3 n6 t2 P+ |, m- j. @ if (chineseid == "")     
: a0 J, f& n7 `& U. D5 _  {     
; E( m. j' Z5 a; L  alert("请输入中文昵称");     3 N6 p: M9 V/ A/ {# Q% y
  document.sform1.chineseid.focus();     . W8 m! z% U8 i/ q
  return false;     
: v( U( b/ I) r$ m9 C$ Z7 N3 k$ n  }   
+ z- B; O9 L, ^7 ` password = document.sform1.password.value;     ) i8 F# ^+ ]8 |
if (password == "")     
# R- W6 D0 B( U3 j1 M  {     2 H: R6 G0 l% n$ [
  alert("请输入登陆密码");     8 M( F+ J' ~( x6 L% ?8 ^2 B9 \
  document.sform1.password.focus();     5 q' C9 {6 X% \3 K0 D: a- {5 u( d
  return false;     $ h# V2 d( \5 u$ Z9 N
  } 8 s  I9 t, K: g6 I0 T: g, p
password1 = document.sform1.password1.value;     " S) c$ @8 a7 b$ S
if (password>password1)     # u( i* _8 h+ w. q2 a
  {8 z7 o- R( w1 h' p5 T
     alert("重复密码与登陆密码不相同");     
+ k; G  U! f& o! c" e  document.sform1.password.focus();
5 f6 |/ N2 q) S4 g* c' L                                document.sform1.password1.focus();     % C1 C- I) d3 {% b
  return false;, A0 \& }) R0 ^, m: d
  }  
' S) S" z) R- A1 j. q- aif (password<password1)     
; a) @, f1 S5 ~8 d  s% R8 S  {
* v" q# Q7 V+ Q$ `( @& V     alert("重复密码与登陆密码不相同");     
7 ^& O8 u- u$ r* n+ n+ t" I  document.sform1.password.focus(); + X; ^1 G% |9 E/ p
                                document.sform1.password1.focus();     0 S$ ~( ]& w" ?1 i4 |# @+ `- M
  return false;2 I9 F6 O! b2 `$ K7 z0 e3 \
  }
# z% C/ K, g+ o- Y2 B% I1 b  C if (document.sform1.email.value == "")     8 g  F' B7 T& r* ?" N
  {     % |1 U4 ~+ C/ I3 b& G
  alert("请输入您的E-MAIL地址");     , ^  S2 p0 s# i7 l9 |5 T
  document.sform1.email.focus();     & u. \. I( [! d* A( y) w  u
  return false;     % h9 f/ A, I( l0 }+ l
  }     
; Z5 K5 J0 a% M3 @      
  E# Y) x/ J* p. @2 o email=document.sform1.email.value;      # K2 ^1 d/ [& f. u8 }9 P! i
emailerr=0       w9 ^* z0 l( L4 O+ v- _5 J
for (i=0; i<email.length; i++)     
  F' Q; I' B3 `1 B+ H  {     
/ o8 Z5 n' E1 p  U# a' d6 I  if ((email.charAt(i) == "@") & (email.length > 5))     + W, {; f$ N" D2 R: i" q1 v& T9 t
   {     6 |8 N% S3 h: a& b
     emailerr=emailerr+1     ; L% H- ]% j! j8 e% ~
   }     
- I' |5 s; p9 k9 E7 ]; T  }     ( ~3 P9 o8 `0 X3 }
if (emailerr != 1)     + d# T, V/ N# X+ v3 @2 {" W
  {     
/ i2 l: Z' t' E0 W5 F" f  alert("请输入正确的E-MAIL地址");     
% ?. k' f8 p* U  document.sform1.email.focus();     
8 Y) A! \% S4 ~& {  return false;     
4 Q" E. \/ n6 b% q  }           ) Q3 c: {- k! O2 W
  
2 a& \2 I5 Q8 Y if (document.sform1.checkask.value=="")
, z% Z$ K; Z% \4 n3 t, d2 a! e3 |  {
/ m6 c- A3 Z7 w6 e4 {   alert("密码提示问题不能为空");     
1 M" X8 h" z7 k/ h- j$ B9 w, K$ D   document.sform1.checkask.focus();     
3 v* q# U' X. }; [2 F' D4 c+ G   return false;  
+ {/ G! x, V# A4 U3 H/ d% w  } ( s2 p) t/ R1 Z0 N
if (document.sform1.checkans.value=="")
! j: O3 ?5 ?& q7 l* [: z& I  {
' g5 A+ Q0 ~  S: O( q" r* S   alert("您的密码提示问题答案不能为空");
4 u$ d4 g5 @  s   document.sform1.checkans.focus();     
. T, |7 p! b) p; k3 |4 ^   return false;  
; v& g: m/ |6 {0 M+ Y! n; m% K; I$ L, F' q  }! F$ ^+ o# R# d2 f: }5 N( b
return true;     
, {  U2 ~) U4 f, ?     0 C1 _% p2 D  W4 f7 }# b1 W
} : }8 {$ Z1 K; R, Q: w, ]7 ^! j
</script>

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