返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:- w( L# S+ }- B: d$ d
<script language="javascript">     
7 D4 n3 ]8 M1 o1 A# o  rfunction IsDigit(cCheck)     $ |# {* T) E2 O" s
{     
8 |' K( b: m! i$ ^; N2 N+ N return (('0'<=cCheck) && (cCheck<='9'));     
- X% w( s6 `0 c }     ! k; u6 A, U6 T, E6 ^4 R
     
* \3 ]! \( t5 c5 K8 N+ m2 Sfunction IsAlpha(cCheck)     , M7 i. j  h# {9 E) x/ o
{     
$ O) s% ^1 V& [' x5 m/ ~4 B return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
6 M) ?  ?; o, Q9 H; V$ J# C }              0 o; U* f6 _: c* ]& z
      
9 d5 d% \$ f8 S  k( c# Gfunction IsaNull(cCheck)          7 r6 y( J: }1 R& r  O) C8 w
{         
# m* Z5 g$ R& `, G return(cCheck != " ")         & ]1 R: \2 M# S4 u" Y9 m
}                               ( M6 `8 W" ^2 S8 z; d
      ( k6 {* K+ Y" s! g2 ^% Z
function checkform()     
' \9 u5 P6 z6 t' C" v: a{
, d! E' `% H* f0 m& _2 c  id = document.sform1.id.value;     9 M9 R. F6 s! \" J' S
if (id == "")     
% E4 b3 W3 U6 d  {     
: H2 c5 J2 U5 w4 \7 @* E$ ^  alert("请输入注册名");     
( W( ]* y& t$ m- s  document.sform1.id.focus();     % Z  |5 Z# i5 }. a
  return false;     
' ]" h- h* }' Q' V8 H  }     ! n- Q/ F" j; ?
     
& @# V4 r0 [( L* ] for (nIndex=0; nIndex<id.length; nIndex++)     ! o5 x$ ^  _5 v; q; N; K
  {     , C1 }8 N0 e. n) k  P7 t
  cCheck = id.charAt(nIndex);     
! u: v8 C, B+ X0 r7 G  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
2 w% n- B/ Y7 N, ]8 j( q/ \- z. [   {     
) B6 ^1 [1 f/ i9 ?   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     + T$ Y% o2 c9 Z0 U: m- w: k8 k5 F
   document.sform1.id.focus();     ' ]2 f3 ~  s6 V$ t9 Q. y& U
   return false;     . a/ ^3 N' v& |3 R% @9 d% @5 m) W% h" c$ ^
   }     - ]$ w4 B' t; A1 ^' ^0 H' ^
  }
6 D% |( _% f2 O! z5 j4 z! V6 _  chineseid = document.sform1.chineseid.value;     
* r* F8 i9 D/ I( A if (chineseid == "")     
1 A1 g1 K) C9 P5 w  {     2 I5 }. i$ u6 D6 G7 f3 N, g
  alert("请输入中文昵称");     # h* V7 k% a8 D- I
  document.sform1.chineseid.focus();     ( Q( S& ?4 }7 j) y4 ~, j8 r
  return false;     - T3 |5 Z; R3 n+ ~6 Z# [5 @& r
  }   
  C: }1 J7 g, V. I4 b4 H password = document.sform1.password.value;     1 F* N2 c( `* Q: k# @) F
if (password == "")     " L. B% U5 H& r# C- C( J
  {     2 W2 ~. n9 d" G' P
  alert("请输入登陆密码");     1 [+ ~! O4 n) E) o4 N* E
  document.sform1.password.focus();     
- B: L  f9 R- K4 u  return false;     + z* V% T! Q9 X& a- N# L
  }
2 P$ v& M) J: d password1 = document.sform1.password1.value;     
% G# c: S" z5 h' G if (password>password1)     / i  ~7 c" _: i$ o& S, n5 Y
  {
& f7 h( N1 B- b8 v- c+ y     alert("重复密码与登陆密码不相同");     
% g+ V& k2 V7 L: f% ~/ ^+ w& |  document.sform1.password.focus();
3 r2 i2 y1 ^) T" L2 Z: N                                document.sform1.password1.focus();     5 u( J$ U7 @0 {* ~
  return false;2 P) Y5 o% k* k/ u" m6 N5 V/ }
  }  & S& n5 ~  s3 P2 ?4 b: f
if (password<password1)     / W! _0 _1 j2 z" i- z# Q& ^
  {- x) j( r/ M) J* J
     alert("重复密码与登陆密码不相同");     . ^- O. K" E. P( e* ~* C! V
  document.sform1.password.focus();
0 w& I0 X* [: W( F+ r3 L                                document.sform1.password1.focus();     
2 I2 x5 |, c1 j6 _6 j  return false;) R0 c; \* j" ?* o8 Q8 w. L# p
  } : K# S( `" f6 g
if (document.sform1.email.value == "")     , x4 ~, ]6 _5 D) Z! E% p# _
  {     $ }2 x- @0 b* [$ X2 c+ G% S" M+ F
  alert("请输入您的E-MAIL地址");     
- f  l  r, q; E% A* k  document.sform1.email.focus();     0 ?8 G' A, m% ^9 ~
  return false;     
/ N* D0 Y: }! d; |4 r  }     6 o: z5 u8 \% ~4 k. R
      % _8 E) I$ I1 [2 @# f
email=document.sform1.email.value;      * B4 z' s$ Y; G' x, Q
emailerr=0     
/ n& m3 i- i- j# M for (i=0; i<email.length; i++)     
2 w7 `0 a1 j0 P3 V" l; C3 ?! b1 D/ s  {     
$ d. B: O0 p7 z  if ((email.charAt(i) == "@") & (email.length > 5))     , U" w1 @, u: s3 b% @5 S( l
   {     
- r2 H8 M& U+ u- G4 W* }     emailerr=emailerr+1     % ]$ x- H' u- N$ ~* b/ ^
   }     
' j2 y3 i" B1 K& Y. y  }     1 D9 }* z; V2 _- e% B2 N  F" o
if (emailerr != 1)       |1 A7 t3 e: A2 S
  {     
! b6 T: w9 O7 \0 T  }* f; p7 V# k  alert("请输入正确的E-MAIL地址");     ' c3 K; c4 _5 F: A' R
  document.sform1.email.focus();     
$ O0 M3 e  I* p, h7 `, P" F: m) t  return false;     
1 A! G" N. ]* d3 v8 s5 d( S  }           
; o# T4 M" y/ ]# z% Y( x  # K  }* l6 j2 [) C
if (document.sform1.checkask.value=="") : V$ @8 h0 s; j; Y
  {
! _- W$ ]- V* T$ Z   alert("密码提示问题不能为空");     : Y$ g! u3 p. Q; U! E7 [
   document.sform1.checkask.focus();     
- m6 A" s* g& N- R: I, G; l   return false;  
9 u+ X; O2 p' _1 t8 v7 ^8 s  }
2 |" P0 F; i% ?. d if (document.sform1.checkans.value=="") 3 _7 Z$ Y; U* A4 v9 n
  { 9 q. d; ~* D/ U8 x" N3 ~
   alert("您的密码提示问题答案不能为空");
  F0 K: E5 P9 |6 r1 W0 t2 }6 Z7 a# s   document.sform1.checkans.focus();     ) B( w6 X' D) y, q+ J2 |9 m
   return false;  
7 E( K" H1 f& }  G) v" \: F0 l  }6 C6 ^$ N+ q3 H$ C2 K" R% `8 G, y
return true;     . Y; j4 H/ S: q7 G! K
     , C, E% X% X, W  I7 X
}
" B+ \1 }& s) Q</script>

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