返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:9 m7 G3 ]8 h$ }. O
<script language="javascript">     
. J; Y6 A, x2 W# ?' Efunction IsDigit(cCheck)     7 L" n0 T) v1 p3 m- G4 Z1 \; n; v
{     
3 |; Y, S% l' y3 V* W! O return (('0'<=cCheck) && (cCheck<='9'));     
! ~6 q/ ?% [3 q- M1 Y; W }     
9 E9 U9 J5 H, z* ?% A, n" W     
6 h) Y4 d8 t* e0 N2 [function IsAlpha(cCheck)     - O' f4 w: N/ z1 Z9 \2 u
{     
! A$ ]0 W8 s' U. L return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     8 l. S7 |3 l7 t' @7 o. i5 Y
}              2 k* b1 m5 M0 ^1 l" q! z7 O* @
      
! D4 Q) Q, S3 I+ M& [* S8 sfunction IsaNull(cCheck)         
. t( f) Z6 p3 D" w& f) X {          % }+ K$ f1 ~3 d  v, H9 e9 j0 G
return(cCheck != " ")         
/ [* g$ v, H% P }                               % N) e. Y3 v& v1 v
      - R3 }! @# J7 }1 E' ^- e0 E
function checkform()     
6 f2 H% _- \, Y, y" B0 l3 ~$ ~3 S# y{ - p0 {5 ?6 a, v6 Z
  id = document.sform1.id.value;     % p+ p0 n) Q4 \- z, o
if (id == "")       k- ^+ R/ e9 A* K/ i
  {     
1 @0 _; K4 T" C5 i$ B+ b% N0 B: g  alert("请输入注册名");     . S; k8 ?6 G4 K8 W5 m% M5 ~/ S, U
  document.sform1.id.focus();     7 c! E1 n* G" D% U$ l8 R
  return false;     
* ~/ R0 n/ P  ~8 p% r  }     ' d9 R# X) A  H% d% J; e
     7 z) [5 f& D* ~5 k& b( u  E5 X  x
for (nIndex=0; nIndex<id.length; nIndex++)     
8 k, M# n# e. Z7 K( P& r  {     
4 D0 B+ t  c9 {7 n0 s/ n% F5 h  cCheck = id.charAt(nIndex);     
0 U( x. C: s' h1 w  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
# i/ x1 S" _9 K+ H# t4 `, H. O* c   {     
; O- Q9 ^8 t& g' t6 t0 R3 D' g2 v   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     , i2 O6 v; u7 r- Z+ v
   document.sform1.id.focus();     
7 \: P) x( W0 F# `. R* O   return false;     5 H8 J4 c. C6 T
   }     * h3 I  U0 z  \0 y7 y$ Y% K
  }
* S: ]7 s, R$ p5 e5 t  chineseid = document.sform1.chineseid.value;     
& t' H. ?0 m9 c if (chineseid == "")     + f* `& w! e, Q* Y/ x: m" _4 J
  {     " ], q, A: w  K$ }
  alert("请输入中文昵称");     
2 \* J* r( i% n7 t  document.sform1.chineseid.focus();     ' T, o$ g5 W5 ^- Z( F6 t0 y
  return false;     
* O# ~- D" l! W  }    / ?+ |, t# x8 n0 G% ]3 o
password = document.sform1.password.value;     ! A% k) L1 q* o% N4 W
if (password == "")     ) t! }* T! t4 z, F
  {     ! K! L8 {3 H$ Q8 U; G
  alert("请输入登陆密码");     
/ `  ]$ o1 G: X% w9 v2 p  document.sform1.password.focus();     
+ A$ a6 W" t9 M& G$ h/ W5 k9 V  return false;     " S; u1 x( m5 ^  v' ]. ^0 F
  }
% W# H  @' i0 [0 I2 l" n password1 = document.sform1.password1.value;     ! r2 a6 b% W. |9 j; C
if (password>password1)     
# y- b% H; @. \7 M* a6 W' L  {
- k/ ^1 K4 a' s+ Q1 s     alert("重复密码与登陆密码不相同");     
: ~  F* b5 X7 i  document.sform1.password.focus(); ) |* P" l% r  L; H: B
                                document.sform1.password1.focus();     / V, O9 ]2 ]& S) f0 D! |
  return false;8 G0 X* G1 |; Y0 V+ c" Z" f
  }  9 J5 \, S% G' g" u  d* B1 O: L
if (password<password1)     0 e" y7 a! d! z6 L
  {
1 P- g2 H, n/ {; c     alert("重复密码与登陆密码不相同");     - Y% H, W4 R8 K6 }! F& A
  document.sform1.password.focus(); 4 i* ]  V* ]( K  |/ g2 A+ G
                                document.sform1.password1.focus();     - l% z( k; ~7 F9 S5 t/ ?, I' g
  return false;
% X+ E" P' V2 `$ p  } 8 v& d, r- i3 H8 W5 o' X# |4 L: Q. u
if (document.sform1.email.value == "")     
- X: h8 ]- _7 T1 R* U5 w  [  {     9 h& w  r7 r: h/ o$ I" K: q4 c( M
  alert("请输入您的E-MAIL地址");     " r5 \$ r& C' \$ V2 l# n. I& m
  document.sform1.email.focus();     ; M; |% {+ N+ f5 w( y
  return false;     
8 P( q7 n2 M. \7 G  C  }     6 k* `$ F. w# c$ L
      5 @# \8 E( }. D
email=document.sform1.email.value;      
2 V# U6 A/ k  A% X% @, p6 b+ p emailerr=0     
; @; }6 C* y# f/ l8 S0 | for (i=0; i<email.length; i++)     
- {* W$ B; I& f8 \) }7 e  {     7 \- L, r$ b. G* U0 c! X/ V
  if ((email.charAt(i) == "@") & (email.length > 5))     + M4 `: N& o$ j
   {     7 i# g* Z( H; P( g0 \
     emailerr=emailerr+1     + ?" C, O/ E& V1 ^1 h
   }     
, s4 X# B4 e& e/ X) q9 D  }     
7 P9 ?' Q/ D4 Z7 ]  H. r& `! t if (emailerr != 1)     3 q. j/ V5 |  R  W7 A
  {     
9 \- v# y: k: W  alert("请输入正确的E-MAIL地址");     # s9 c5 D0 n4 j% W' R
  document.sform1.email.focus();     
! l+ o- G' d6 z7 v  return false;     1 m2 {& e; b( t- N: y, y
  }           
# ~+ P1 b' q2 [! @' q; r9 K4 H5 m  # I% P: S* \/ @0 r0 O9 H
if (document.sform1.checkask.value=="")
( X0 N6 J* y, h1 D) i  { 1 x# p/ k5 \) o) H# o9 o2 P
   alert("密码提示问题不能为空");     
+ K* C3 e1 g. B- i# @' S   document.sform1.checkask.focus();     & i9 f: p6 ^( |8 M/ C. Q8 D) T6 i5 h
   return false;  
1 T! `" u6 d: G  }
2 }( ?1 M% f, l* }4 U( j if (document.sform1.checkans.value=="") ( O4 b  w9 n) ^% G
  { 3 V/ g) r+ |& G8 `
   alert("您的密码提示问题答案不能为空"); * n6 B0 o. u6 r- k- P" W- p4 Q
   document.sform1.checkans.focus();     $ T5 O1 o% s$ H  T$ ~
   return false;  9 r% f; m" Z* i+ Q, N; _
  }
* @% N7 w# J2 \ return true;     % H7 j9 H$ V, M" E: D3 w3 J. E2 D
     ( T1 }' ~2 N) O* K/ D! C
}
. n% X/ q2 p( S8 L8 m</script>

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