返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
0 [* ]" m8 m" B9 P" ?! U6 s' u' V<script language="javascript">     
3 P& q) H1 S7 L0 kfunction IsDigit(cCheck)     # M& M/ ?* C5 Q  k. @4 `
{     
" ?* X  c8 Q/ T1 t; ] return (('0'<=cCheck) && (cCheck<='9'));     
) l6 y$ t, c2 s5 E7 @0 `$ v! e( q }     , X, F& p; j: h6 @* S
     6 j  w$ k9 q( d1 A! i
function IsAlpha(cCheck)     4 t1 h& n4 f7 o7 t/ |1 w
{     
3 Q, [9 K( z4 m& a return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
" ^) W  t3 z5 W0 }3 `8 E }              
3 |1 l) K9 O5 g        [% q/ i9 _" l! k
function IsaNull(cCheck)         
1 H$ ~! f( T* r$ p6 d {          2 ~8 n# z( Z9 }" {7 J7 `
return(cCheck != " ")         " A, R0 |$ J/ w1 {; I* P
}                               3 {2 m; \' k  k) s  {
      ; {! K# v& s9 G+ x$ \! Z
function checkform()     
& i* _" p3 [2 m8 o% R/ Q$ h{ & G+ |# v, }8 ^0 Z! ~
  id = document.sform1.id.value;     
; ~& C- ?2 _! D" i! S. U6 t if (id == "")     
, g& Z+ ]7 P  [1 }/ h  {     
( O9 q' `, Q# ]- e  alert("请输入注册名");     
% \+ k4 [4 {% p3 Q  document.sform1.id.focus();     
- z7 p6 T. S7 a& D- B  c  return false;     % K; h/ c* B# k# a% c2 w2 E
  }     ( s3 A1 o1 n: \9 \( e2 T/ T
     
. y$ B/ W$ C6 g9 K for (nIndex=0; nIndex<id.length; nIndex++)     
- m4 Y% i. F" F2 C/ Q! f  {     : I1 D  d1 _+ q9 F
  cCheck = id.charAt(nIndex);     
& B+ @4 b7 d4 d! K" R  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     + j! X1 O' k1 ]/ U7 ~7 M
   {     9 m' Z3 _6 ?( k3 c! z
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     0 d3 f4 }/ U8 n; U$ b0 T+ V
   document.sform1.id.focus();     ( b1 Z, z# x) O8 o" V+ I' e
   return false;     6 ~: F+ @- [* T* t. w( J5 R* S
   }     
6 ]) R2 D" s& y4 H: f2 s* b- j: p  }
1 t8 l' d$ W- P  chineseid = document.sform1.chineseid.value;     
; c9 r" j5 v4 b  i if (chineseid == "")     
; b7 W: z% F9 r( s$ G4 q7 `: L+ M# ]  {     ' r  w) M- M3 z+ l: D
  alert("请输入中文昵称");     : ~& r* J  @7 |6 W: n) K- Z: h
  document.sform1.chineseid.focus();     : N, c; l  {2 s* y" q9 t
  return false;     
  t2 S! s; r. M  }    # P1 G, E6 v4 y1 J) T% ]8 U
password = document.sform1.password.value;     , @# T. g1 J6 u8 H1 S1 |5 V
if (password == "")     
, L& F, f& @. C4 g' {  {     5 {$ }5 m9 [, v
  alert("请输入登陆密码");     
7 l- F4 F+ z2 g/ |1 H4 N  document.sform1.password.focus();     
$ V6 ?5 L. e+ v7 @/ |& a  return false;     8 k* {6 l3 [. K2 f3 W; [$ b
  }
9 p* M, v% ~6 ?( U password1 = document.sform1.password1.value;     
% h, {! e& F: O" A3 x6 m+ A& G1 d if (password>password1)     ; T& H8 K! h+ {8 c% p# d0 u* M
  {
6 z/ i" ]0 ~& J' K; \     alert("重复密码与登陆密码不相同");     
& N" m# p! a$ ^. s9 `  l  document.sform1.password.focus(); 1 F+ e/ P$ M3 f) [+ x. ^. z) G0 V
                                document.sform1.password1.focus();     
3 T  M: d1 L/ [6 o# ~; ^1 X/ V  return false;2 g# z* d+ o# `( p
  }  6 J5 u9 T0 \4 U/ B/ S4 A4 {+ m
if (password<password1)     
8 [, D3 K1 O8 n1 D% x( w. H* C3 s  {. @/ o3 K2 U  f: A0 m2 H
     alert("重复密码与登陆密码不相同");     ! f1 ~/ J  r3 H) U' X$ H. G
  document.sform1.password.focus();
8 w0 \  F" G' T                                document.sform1.password1.focus();     * B$ f1 @2 \; c4 q
  return false;
' _0 ]# f8 ~. i& I  }
  ?  T/ ^( W2 e7 `/ o: ~ if (document.sform1.email.value == "")     
$ l. H0 V# F+ x  {     
; `0 i) K% t3 o  alert("请输入您的E-MAIL地址");     ( a2 H  U: C3 o$ i/ l) K
  document.sform1.email.focus();     9 ^5 K/ h$ L8 b
  return false;     : P, U5 [; T) z# b9 x/ W' c- y( Y
  }     
2 z% s9 M2 P+ m1 W9 T( |      9 {# {  k4 i8 i: T+ H
email=document.sform1.email.value;      
6 V) v& ~4 ~0 c+ s6 K emailerr=0     ! \/ P3 H0 e  [# T
for (i=0; i<email.length; i++)     ( j+ }0 m4 b8 ~2 R5 _3 M
  {     
) Z4 U  |6 W+ r, y( e. H  if ((email.charAt(i) == "@") & (email.length > 5))     
" g# _) v. j) G3 s   {     
/ d. t* z" g$ o* \     emailerr=emailerr+1     9 ]% a( s1 O5 u( t  e0 z7 s  {
   }     
5 g. `' s3 W5 q' x3 b  }     , m9 m3 w1 T- L" v
if (emailerr != 1)     4 m4 V, {* Y( K4 K+ N% l
  {     
8 N4 @+ O7 l7 I0 P* Q8 Z  alert("请输入正确的E-MAIL地址");     ( E. f: E1 ?, g7 y" A4 H- z
  document.sform1.email.focus();     , Q, s) x7 B$ }# q
  return false;     , |8 q9 l) ~4 \! f
  }           
2 T' w2 O* c# E# z; @1 i- G  
8 G+ x- ~# g7 l3 F if (document.sform1.checkask.value=="")
$ I9 j+ R5 o+ j+ b9 x# V  {
3 V* w" c" m$ O1 z6 M" _   alert("密码提示问题不能为空");     
7 g- Q# S6 O8 X   document.sform1.checkask.focus();     9 c  I# I7 W" h9 T9 ?
   return false;  
, p7 |9 t3 |( Q  }
5 f4 f8 G( ]% U, a, ?0 R if (document.sform1.checkans.value=="")
) I, K- D/ K5 Q& x2 S# Q" U4 Y  { 3 R& z' ~8 v; r0 N% \$ G
   alert("您的密码提示问题答案不能为空"); + E0 n- P* H1 v; Z5 G. B  E
   document.sform1.checkans.focus();     
4 Y- E+ \) }% t# b   return false;  * `; e5 \( l1 @# l' [& n. I
  }
; w, f' X2 c: Y5 \$ x0 t return true;     
5 v9 Y) @+ l1 S8 J3 ?     . `# v8 E" L* N5 w! u8 U
} & G0 p: K  Y1 N( l- ~2 L* s; U
</script>

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