返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:: b) ]# e$ j6 o7 |4 D7 f% V, L
<script language="javascript">     
9 m" Z7 n" u0 V1 W1 O$ m' Cfunction IsDigit(cCheck)     
" I, ^0 m2 I& e# ^) G% N' G0 \ {     - G  B; C* d: x3 v" K% Z. {
return (('0'<=cCheck) && (cCheck<='9'));     1 U8 n' x$ [2 y2 }- x( W' }, Z
}     
% v, R- h8 Q; ^) u4 }1 J6 R  O     
' e/ ]1 f6 \9 Q( \function IsAlpha(cCheck)     
' Q$ M+ E0 n* [) `4 Q, J, B {     
$ U& a8 r# j# v return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
: a; }( S0 x3 J. D7 O+ s }              
: W- g. m/ [3 R$ |      
5 S6 H% Q3 W5 Pfunction IsaNull(cCheck)          4 P/ ^! O/ _- H) Y
{         
8 l+ W: Y& ~* k# t: @/ J9 t return(cCheck != " ")         - q! X% G. K7 J8 _
}                              
+ f) C) v: U7 C# g! r      
" Z- w) K" x2 _- j, efunction checkform()     
) E& s1 C, ^) o: r6 B{
( X2 Y8 `5 n5 r; k( f  id = document.sform1.id.value;     
* H: t; x1 L' I- E& T* g( x if (id == "")     ; l# l: w0 @: P( t
  {     * u# }9 }3 z2 ]/ G2 J+ f
  alert("请输入注册名");     
# f7 ?; Y5 K" h& v" B  document.sform1.id.focus();     
# ?1 I) I% |( h  l3 z  return false;     ! z% m3 W8 G3 Z5 z1 g3 C5 {: c
  }     
* V1 t) Y. @8 j8 l     ( P. }5 x7 I4 o. }
for (nIndex=0; nIndex<id.length; nIndex++)     
- ?. i$ U6 m4 X! x" l1 g  {     
* b' x( x, n2 A2 k1 @* I  cCheck = id.charAt(nIndex);     
* S6 I  h9 C. Z( r- O. Y: A9 l  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
1 A3 B6 j; t* K, I   {     * \/ ^4 Z: x4 r9 s# O8 o2 _8 P
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
# q7 b& p& w6 V  ]   document.sform1.id.focus();     " ?) F) }4 u& K6 U! c
   return false;     , U' R/ d" ]' W- L4 M, @8 a5 G) t( F
   }     2 R" r( k" e0 \/ k+ e
  }
! G$ f  }# m% f+ ^' p, `, [  chineseid = document.sform1.chineseid.value;     
9 r! N, B' ?5 ]# r" V if (chineseid == "")     
) @- L& D/ w7 t; a5 Q$ ^  {     9 i! `. E4 V1 t2 Q0 X
  alert("请输入中文昵称");     - x: ]+ V0 \6 R% n' O/ `% d
  document.sform1.chineseid.focus();     % M0 |6 d6 s% W) S
  return false;     # y% Z5 Z: L0 G9 y
  }   
6 I  r$ `- y9 z. p password = document.sform1.password.value;     8 m, e. _; T( i; ?7 H, r6 z
if (password == "")     & m. q+ e" v) `" W, R9 ~% c3 U
  {     6 F# ^' O6 n1 `! j7 \
  alert("请输入登陆密码");     
, C4 R! {8 e( X: q5 P7 K4 f  document.sform1.password.focus();     * D7 O) ]5 L  T" @/ f: x; y! k: Q
  return false;     3 \# k" q7 [- E% J1 H5 J2 v, Y3 x
  } 3 f* e0 ~* O- F
password1 = document.sform1.password1.value;       j; V; j; X1 z7 t' m& D
if (password>password1)     
# o  {/ R% s2 V% Q& u3 y/ L  {
( Q' q3 a; h) Y4 ~     alert("重复密码与登陆密码不相同");     ' D% [1 C4 e( F% K) R9 d5 |
  document.sform1.password.focus();
% X6 B# F) N0 r: C8 w% k                                document.sform1.password1.focus();     
5 e# I  F/ y% n- T( Y6 ?  L  return false;/ B' C. z4 {: T+ u- B* r
  }  
1 B1 U! |1 w" H- a) {( F% Z5 o1 Oif (password<password1)     1 h+ I) @0 }7 _' P
  {9 z. H* T! [+ j$ n& t; E. ?
     alert("重复密码与登陆密码不相同");     
) U8 t" G) P% N  document.sform1.password.focus(); ! c" ^( U! E8 _" Q* |, \* F9 V
                                document.sform1.password1.focus();     ' O0 t- `- B3 T8 F& m" w- o
  return false;& K' X- n" j; h5 ?' s$ Q$ u
  } 8 ?: f: D6 q) [! P7 h* P
if (document.sform1.email.value == "")     
4 w' C& u! Z% p9 ]  \  {     
& y) L, H( t. g" V  alert("请输入您的E-MAIL地址");     
- k& @7 Z+ y, y( j  document.sform1.email.focus();     ; _& d1 W6 r) Z+ C- p9 |
  return false;     + j5 M* H* Y" w5 O, d4 \
  }     
' o+ X/ y, i2 t* S5 v% ?9 a5 l      0 k4 k0 u+ ~. n; V/ ~; K: a9 Q8 W
email=document.sform1.email.value;      3 a$ E8 b0 |- e2 b
emailerr=0     1 L+ q% v0 p9 a( C/ h6 l% L
for (i=0; i<email.length; i++)       D& o# V( w& c8 `. P3 R/ l
  {     
7 d5 ~' ?! l% G6 \/ }- R  if ((email.charAt(i) == "@") & (email.length > 5))     
1 {2 v' p7 n$ H+ T( I1 C6 t   {     # e9 }, o  i: P( g$ S9 ]& R# z
     emailerr=emailerr+1     
8 [8 J7 ]& Y# W* }1 ^4 R   }     
" \4 J) D7 l. p* ^" A& H! z  }     1 C+ F; y* Z' V
if (emailerr != 1)     , l, t" g3 r6 |7 j$ @9 @
  {     3 |0 c9 r* N0 P/ w6 ?
  alert("请输入正确的E-MAIL地址");     
. J8 r4 M" q9 _. P8 b  document.sform1.email.focus();     & U7 b9 K0 @2 E: i: O$ S' P) r# U
  return false;     2 ~% K- b  t) e. o5 Q( [& t5 D0 `
  }           9 ^, W( J( e* D. W$ }& Y( H. F. J& f6 L
  5 u( ^1 d: K3 m" q6 C8 `; o
if (document.sform1.checkask.value=="") + r6 Z2 U  |% X2 P
  {
- i# p- O; N  ~9 h0 v/ R% _& w5 ^   alert("密码提示问题不能为空");     - |4 m9 y" ?6 [  @& ]/ W
   document.sform1.checkask.focus();     
7 A) g$ j5 M+ n& x   return false;  . b3 ?0 J6 `3 ^* i6 S
  }
/ S# T+ j: k$ ?$ t, y( W4 H if (document.sform1.checkans.value=="")
9 q5 \7 Y7 {' P  {
7 a' N) h- F2 E; @9 b' g   alert("您的密码提示问题答案不能为空");
& l* e* P' G$ E' q8 H   document.sform1.checkans.focus();     
1 `* m; Y6 z$ o   return false;  . S: j* c; \# D* p; }$ a
  }0 M* C- D2 v9 U" `
return true;     + r* O8 h+ l6 |( }& ~& g+ V6 ?5 @
     
5 J( H- d* g8 B6 \6 u/ E} # n7 F2 j- I% ~* F
</script>

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