返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
& r# Y, d. O; N* E- j<script language="javascript">     % w% Y2 X1 `/ W+ Z
function IsDigit(cCheck)     
) z8 y  l; m0 X( q9 D {     
% B# ]# L$ F' k# _; _; w return (('0'<=cCheck) && (cCheck<='9'));     " z! {$ C0 R- ]
}     8 N' a) }9 `/ Y5 M5 k
     : v4 p( {" r* I" y" `
function IsAlpha(cCheck)     $ L4 |  d5 y. ^0 q- N) S  @
{     
; |( X1 f3 C1 A& b) w3 b return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
$ w& m4 T& ^# u! g2 e; p0 B }              
" W- R: k, p7 a% a% w      / B# U0 ~: S) I
function IsaNull(cCheck)          4 @3 U: \5 W, Z7 f9 d: c
{         
# z5 F- a* K% J. ^ return(cCheck != " ")         ( P5 E4 X4 i7 w5 J, ^: V1 c
}                              
6 Q# L2 _. U+ v0 d2 }% m& P! Y% K      
) z$ d$ h* o$ Ifunction checkform()     - E  M8 Z5 u' N; p
{ % z% W1 n) q# }8 Y4 }
  id = document.sform1.id.value;     + }) W! s: f( N
if (id == "")     4 s; }% h, h# o7 M
  {     
# A" J' a$ ~5 ~& I; v! e- B  alert("请输入注册名");     ( X1 K4 s' |5 ]$ A2 k, j6 O' G4 \, f
  document.sform1.id.focus();     
. c( P: e( Z; I- z2 B3 |  return false;     $ m/ M$ a* x% Y& [, |" W
  }     
* [: Z/ M* ?  ^! |! D* n     ' Q  |  S$ [# {9 \) x
for (nIndex=0; nIndex<id.length; nIndex++)     6 s  Y. Z" T, V
  {     , k& w& g$ X7 C1 h4 K! j2 T* \; Q
  cCheck = id.charAt(nIndex);     ! }9 ?0 t2 g$ S# L9 R3 O
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     , I" d$ h9 ^7 G
   {     5 ?+ t, m1 C' N1 b6 ~, R/ R
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
/ b4 y/ Y  B6 ]5 ~   document.sform1.id.focus();     
& }! \( Y! E; j0 k# ?   return false;     ! Q. g9 H# N5 A! ~! d8 B3 u. J
   }     7 c& r) a) l9 Y) w
  }
+ ^/ `( H7 C1 G9 b+ f* G- a9 x  chineseid = document.sform1.chineseid.value;     
& d2 s3 c3 T0 p8 y  ^6 k if (chineseid == "")     3 ~( i  G7 W& B3 r& e
  {     
- V" J: r7 Z* o; v# ?% `( b9 q  alert("请输入中文昵称");     
( b1 i8 ~3 ]' r2 _) p- F% k$ m  document.sform1.chineseid.focus();     
7 ^2 S0 [. U: |; k  return false;     $ [& d, |' k- e
  }   
# I" K" p) W6 ~ password = document.sform1.password.value;     
: f6 E3 R! e* b, r if (password == "")     ) _. d! H6 c  L& d" I
  {     
( i! W0 U) }. M% W0 |! g' B  alert("请输入登陆密码");     
) {# e7 W- S( }  document.sform1.password.focus();     
6 J8 ?/ u( O) |; s) z6 Q  return false;     
; M8 a  R1 p8 f0 V  } 5 n8 f  T. U- {5 a1 V7 R
password1 = document.sform1.password1.value;     5 C" z/ \; C- L& W6 Z6 s
if (password>password1)     4 F5 N# P! c1 _; c5 \
  {2 O5 [; W2 {( b2 H
     alert("重复密码与登陆密码不相同");     
: |; _2 F/ y# r2 d% m$ O! t' y  document.sform1.password.focus();
5 I/ d0 }% |' Z, e- L                                document.sform1.password1.focus();     
5 U3 I, v6 w6 i  return false;0 ^+ y. l" M1 U: e) w; S5 F
  }  
9 A0 d0 R$ C0 Tif (password<password1)     / Q9 L" M& s+ e9 N) m7 S
  {3 R/ U/ f) N; O" I! n2 @  I
     alert("重复密码与登陆密码不相同");     
+ O- `* s, P# E6 X4 q3 h2 u7 X  document.sform1.password.focus();
/ ~2 s1 H6 T: ?5 X% W                                document.sform1.password1.focus();     5 f3 a9 z5 i7 t9 D- \+ G
  return false;" ~. \7 ]) w4 S2 u- C) K
  }
7 q6 P' y( z: Z' k3 V1 E, t if (document.sform1.email.value == "")     
) G$ X! \7 p2 o+ q  {     " y$ @. w( C7 h$ t/ E
  alert("请输入您的E-MAIL地址");     ! z* w" {- X0 F& `4 v/ x% H
  document.sform1.email.focus();     4 D! Q2 i2 p7 `+ L: M& Y' J' P
  return false;     
3 U; B* M' N+ \8 J: p! S  }     , C) {8 v' G, [  K5 }0 u+ J
      2 |: C' A* D6 }7 N" {9 u) r
email=document.sform1.email.value;      , ^$ C$ h$ @; m# S, x' W6 A
emailerr=0     0 i6 a! P5 [, p- T
for (i=0; i<email.length; i++)     # q0 Y' Z$ |; J+ g8 L
  {     9 t4 p2 P3 E- C; \0 Z
  if ((email.charAt(i) == "@") & (email.length > 5))       x8 R; g; S, O" A" Z
   {     # Z9 J) m* g) o- v0 @( A) |. y1 T
     emailerr=emailerr+1     
& ]3 @( u3 d2 L7 X: g6 v   }     3 k% V& @6 B; q* a) X, v! ?- D
  }     
6 Q( F& e( L: B0 o6 j' C/ F if (emailerr != 1)     
! @8 \; D, j4 }6 ]  {     
- G. B  Z6 @. k5 L4 l3 `3 N  alert("请输入正确的E-MAIL地址");     ( N9 K9 w1 N! i; n
  document.sform1.email.focus();     
) b5 U4 D! ?' w  return false;     
' G0 y+ \6 I4 w1 f  }           % A9 V4 b0 A/ j9 `9 m
  
9 Z, h7 h* i* k0 R, u( c if (document.sform1.checkask.value=="")
. G# u$ O8 B+ K4 y! V) x# Z- t' q  e  { 7 S! L5 \) c1 V7 s
   alert("密码提示问题不能为空");     0 p, J! p, F/ \
   document.sform1.checkask.focus();     : {, N* {- z* z6 C; ?/ n  g* ]
   return false;  ; ], V, s* ]. s3 q
  }
  f0 L2 n. C' H) R5 N if (document.sform1.checkans.value=="")
! o8 y$ S  Q$ J2 L2 D+ L! z; _  {
1 l1 d5 L6 O. o   alert("您的密码提示问题答案不能为空");
, N8 ]: K2 `# |( R8 @! |/ v* Q   document.sform1.checkans.focus();     8 `4 L- `# F9 p; ]8 v. {- [" a
   return false;  ' H" L2 k. T/ i2 c, E/ Z( m! J
  }
- _( j* G5 S3 N2 w- g* P return true;     6 G* p* @. p, c" W- H/ G# {
     8 D1 k% r2 f4 l! D" z8 y
}
$ O- M: g% B1 _+ ?</script>

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