返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
/ D% Q, o0 x% K. r<script language="javascript">     
: n9 M; g( s& m7 E8 Y  o8 J0 ifunction IsDigit(cCheck)     & F$ H! X- P1 h4 W
{     9 `1 s! m7 `, p3 x8 ~
return (('0'<=cCheck) && (cCheck<='9'));     
9 W+ R5 q) k2 E- [7 y8 n1 a }     
6 l0 T2 l* T. n3 p- ^) H     
2 ]9 M* H1 h, ^, _9 C8 a% ~( L2 Mfunction IsAlpha(cCheck)     
# n2 n; U' [) R# Y7 g* g$ t5 S {     
3 W1 [" A& ^( G return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
/ p4 l  f9 q9 A0 ? }              4 t) @, t" q9 N: T' x
      ' l  a  H" ~9 E. F( {$ F
function IsaNull(cCheck)         
: S2 N6 k, b6 z* a {         
/ F# G& w1 U: n0 R- i3 S' F, W return(cCheck != " ")         0 h! z  [  G  {( D; j7 O1 j# r; _
}                               ! d8 R: A% K0 E( V$ k
      
/ y* f6 u( Q4 n3 o* e4 T- Bfunction checkform()     
! l/ r( D- F, ]$ _5 A* L{ 2 H1 Y' t. T" J/ O( F0 J
  id = document.sform1.id.value;     
7 m! `& y2 ^+ O: j' } if (id == "")     
9 E1 U' b1 b3 a; {! H  {       z! M) z3 _, u4 e8 s$ `3 ^
  alert("请输入注册名");     5 s1 z: d: K8 [/ G. u! y( Y- E% X
  document.sform1.id.focus();     2 V; \7 v2 v* y" w6 Y5 J
  return false;     - q2 f. |) a% F- v/ ^: P& C' s8 E
  }     " j) [0 [' n  L
     " X9 y5 ^8 u+ x& B8 F7 S- Y, H
for (nIndex=0; nIndex<id.length; nIndex++)     : q- J! J/ U/ {: d4 G$ @& A! B; \
  {     
0 H9 s& n4 C' M0 O  cCheck = id.charAt(nIndex);     
9 ?  d9 |0 i" O' m! ]  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     ; n+ F3 K& p, M5 g+ v( V
   {     
6 l! U& U' c. O% M2 y, V9 c   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     ( ]- N2 {9 V: Z, g
   document.sform1.id.focus();     
4 R, Q- ~# {4 q   return false;     - _7 k& n0 T9 A0 K* E. O5 H) [$ b
   }     # l5 n4 v( G% g& i1 d% ~- k9 G
  } - s7 l- M0 ^! e; C
  chineseid = document.sform1.chineseid.value;     
  `" B9 S) g, x6 \! b if (chineseid == "")     
0 b: I, Q2 L+ N  {       `! e0 [9 ?$ i5 G/ e9 ~
  alert("请输入中文昵称");     
! M6 ~9 G& l% w, q" R0 j( V  document.sform1.chineseid.focus();     
9 Q0 P* p6 G4 o* Y  |$ A3 |/ x4 M' |  return false;     ! E! {1 C* z! F1 v1 u1 `
  }   
" G. y3 H: T* s3 \2 L; G( i password = document.sform1.password.value;     
5 G$ V% P/ b. G0 R9 h+ e- S3 O" } if (password == "")     
. K3 g( w  P8 u1 C% A4 H  {     ; h, G% w5 {7 R
  alert("请输入登陆密码");     
# }: u+ H7 M- @# i- C4 E  document.sform1.password.focus();     $ d% c3 Q3 N% c
  return false;     ! C2 T& [# X* G( v6 z
  }
' h6 }+ ?5 x2 |% j5 Y0 `# @1 x password1 = document.sform1.password1.value;     9 \  j. \2 U; a$ d- x/ N) g1 k) S# B5 u
if (password>password1)     0 C4 _0 W/ N0 B+ j/ z& e$ T0 i' z# F
  {$ R. O8 e  m: D
     alert("重复密码与登陆密码不相同");     
) ?. N; v2 L' a' H: S- y7 A8 X0 U  document.sform1.password.focus();
3 ?3 {( r: q! F8 ~, N& {                                document.sform1.password1.focus();     $ J- n" y) @# Y, F  Z, y6 q' p
  return false;
  ]6 v% ~/ w& Y0 b, J/ Y  }  
" i3 s3 B: |% H! rif (password<password1)     
( E$ X0 `6 _$ c* x  {8 D4 M: @3 P/ r+ ~1 c! x4 e, [
     alert("重复密码与登陆密码不相同");     & t0 b0 E; c8 f
  document.sform1.password.focus(); + Q) ?% `+ r, N  B
                                document.sform1.password1.focus();     
% w( d: k6 W1 v. N  return false;
  y  K$ G7 [4 d- j( ^. i' O) f  }
5 d- J4 q/ B" A) k1 c" G if (document.sform1.email.value == "")     
0 C# d0 o$ X4 u& `/ ~  {     ! H2 H+ m6 ]2 l1 V; x2 C, \
  alert("请输入您的E-MAIL地址");     
* N$ l) E' V; W% c+ q3 V7 `  document.sform1.email.focus();     4 p) J* r! b" C. P
  return false;     
  Q8 |0 ]4 @& |7 z$ g4 f" q' m# {  }     
0 o: ~0 g) J) K" ~$ h      ! s$ U, X3 i. |+ K# w4 L6 K
email=document.sform1.email.value;      3 a4 C8 h* _8 I( c8 \4 d$ m2 W
emailerr=0     * z1 r# f0 s  o6 d$ [
for (i=0; i<email.length; i++)     
( p, ~, K; u% F; C. Y, r& Z' o+ p  {     / Q  O- b" K3 O+ p& F( |+ [
  if ((email.charAt(i) == "@") & (email.length > 5))     
( L; M& _6 B9 o& ^* N   {     4 E' H# K9 m" h5 |) X
     emailerr=emailerr+1     
, q$ W1 h, X: @# P/ x" i1 `$ N   }     ; s2 e7 s) c  `+ P
  }     
8 s; [* C3 R9 R( e3 b4 B0 C2 T: D* L if (emailerr != 1)     
, m3 j2 f* Y% _, j/ Q  {     
" \/ k8 c; `. e! N  alert("请输入正确的E-MAIL地址");     
, e3 j1 U3 y8 i3 y# B! C  document.sform1.email.focus();     
. I# j$ J1 Q! R7 t  return false;     # r. w+ S. f9 x9 L3 V* ?
  }           ' `. @* e2 L0 `, l  X& t$ x4 S
  
; X. F* u  a5 l7 {4 ? if (document.sform1.checkask.value=="") ) j: `1 U/ O/ _8 Q' i
  {
9 ]. ]% A0 r  k! g3 r   alert("密码提示问题不能为空");     5 G8 G1 X7 e& Z2 ~$ J1 E0 q
   document.sform1.checkask.focus();     
4 G8 J5 x' Y; ^) E- ^; U. G   return false;  
) m- o8 Q3 v. F2 v" [  } + I# Q4 C' A: |
if (document.sform1.checkans.value=="")
3 ^' o( S( W4 M0 K  {   Q! l7 c+ L* y6 ]/ Y
   alert("您的密码提示问题答案不能为空");
- d- G7 a3 U5 p' ~   document.sform1.checkans.focus();     
. H8 ~$ S' T: f" }( W   return false;  
4 s( c6 r/ H; J' i1 B* b8 A  }
7 B( I- V! n" I7 L. O0 V return true;     
- I3 u0 ?( W7 D; l& n' y     
5 X2 W  ~  f1 ^& _}
! k; R' Y+ R$ K( G& p</script>

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