返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
& b; |  _3 Y1 N* i9 ~! Q<script language="javascript">     " m# I! D8 T( H1 N; T4 X9 E
function IsDigit(cCheck)     
% C1 x/ `4 z7 V0 n {     
, ?5 e+ X! h, U0 `" @, U return (('0'<=cCheck) && (cCheck<='9'));     
/ z6 I* q; i) C4 E! H4 y+ ` }     * ^" L5 G  e  d# g4 I
     4 r# H8 Z7 P0 b; K
function IsAlpha(cCheck)     
2 h7 Z7 P3 x- P. [ {     
  p0 \) r; o6 k2 E' Z( U! O return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     & U' h- [& n/ Q4 x
}              
+ {7 T1 C: Z, @' D8 J% B% N# ?3 k' @      
$ o7 A  G! A- ]: v5 O) B! Hfunction IsaNull(cCheck)         
5 J& m! }; h- d {         
% F, O1 z- k9 h/ h& y9 p return(cCheck != " ")         ; Q8 x) ]. g* Q: p3 |
}                              
: z5 v$ O; U1 P5 h+ c' G0 ?- I      
+ r+ p& R* b# G) B; u8 e! w! ]9 Gfunction checkform()     & b- {# i: ?/ J5 \
{
6 c- }* K6 ^. G# t  id = document.sform1.id.value;     + p+ H% A# e0 f1 k
if (id == "")     # r1 S, Y- l) m; x
  {     
' I+ n5 M# N  P  alert("请输入注册名");     
7 j% G; Q+ Q9 [& E  document.sform1.id.focus();     9 V6 m6 w/ N0 o$ c0 f( ~
  return false;     
5 ?" f0 Q4 ~, x8 H  h  }     
: z% z, A) k+ l/ k2 I     
2 c: T8 [; w# h7 g% r for (nIndex=0; nIndex<id.length; nIndex++)     
. {% I( `& D2 U  {     
: `+ f9 R& l. ?( S# R' b  I  cCheck = id.charAt(nIndex);     ; S* Q& f2 ?5 n
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
. l' H  a  u) m   {     
0 T1 y6 \# ]+ z' e2 l   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     - r4 K" i3 `7 ]+ f2 @! G$ X
   document.sform1.id.focus();     3 A& Q0 O; |& c. y( C1 h
   return false;     ! o) b4 ~& u) M: {; P' U2 c: k
   }     
' A+ u0 |& ?/ `. b  } % M/ V0 k0 U1 ~3 u) v$ ?$ x
  chineseid = document.sform1.chineseid.value;     
1 ~; n7 ~6 p$ B# [' E% M) D% G if (chineseid == "")     
6 B; ^: t; |; V3 M6 b; c, v; H  {     
# n4 w4 S8 r6 m2 U2 K0 n! A, D% [" c  alert("请输入中文昵称");     3 O3 @$ y6 @# e$ b7 m
  document.sform1.chineseid.focus();     / N3 V- w. ]& }8 V, L
  return false;     
: c" I" }3 l- c  h0 y  }   
$ A/ D+ b" N: P. i/ M% O' P password = document.sform1.password.value;     ( k7 }- T- }* |+ m
if (password == "")     / Z+ x7 p1 Z! ^1 A4 G# o# Z
  {     ! J6 [$ f+ b; I. h
  alert("请输入登陆密码");     3 Z* f3 w  ?. r8 w' U
  document.sform1.password.focus();     
4 O0 M% ~# m  o7 F  return false;     
. [: `/ K4 e5 I  j/ ?) P  }
6 Z0 ~2 M5 Q8 p3 I& e password1 = document.sform1.password1.value;     , C+ r( ^. b1 t3 f
if (password>password1)     / S; y2 {% o/ e" Y  }: O% t9 ?
  {; T! ^$ ^' g6 V: R2 s6 P. r
     alert("重复密码与登陆密码不相同");     
$ y# |, k5 t$ }  document.sform1.password.focus(); 7 [4 w" }/ L0 J% L. q0 D/ v
                                document.sform1.password1.focus();     ( L* k' z+ l) |+ ~4 B( A( h
  return false;
. p' E) x; I/ Q2 l! Y  }  
( U+ ~0 l: e6 X: Z% mif (password<password1)     - H& b2 U) u4 C+ g4 L+ J
  {
, g/ `4 W* y3 N     alert("重复密码与登陆密码不相同");     5 l  W. c$ |5 f; u, u& q
  document.sform1.password.focus(); 4 Y  J. C& A5 g
                                document.sform1.password1.focus();     ) c: G5 t) d- X; K
  return false;) [. s% Q, s! p6 ^8 {8 X2 S% o8 ?
  } 1 N% L7 j- `) I" z6 q  q( _  m4 x
if (document.sform1.email.value == "")     8 p0 h' r$ A+ \  P5 p* t7 t
  {     
0 O9 O) @, V$ Z" I( |9 n& R  alert("请输入您的E-MAIL地址");     
% e* E3 Y1 M& o# Y; n* e  document.sform1.email.focus();     
6 O, n6 L+ B6 `; m  @" l! `. K. O  return false;     ( D4 |/ F) n" C, j5 ?3 G, l7 t
  }     2 e0 ~' G" J; f6 J( t$ p
      
; r$ i1 W7 U0 X$ q- w email=document.sform1.email.value;      - k" o) H% d+ q' w9 X% X
emailerr=0     
2 J2 T) _' V$ w' [2 u) s for (i=0; i<email.length; i++)     
  O5 b* T4 s9 ]) b% M$ N) t  {     
5 l& d( a+ N6 C7 G6 Y+ x  if ((email.charAt(i) == "@") & (email.length > 5))       Q4 n, F' k7 o' z( d7 p
   {     ; D* L4 W) s+ E) n
     emailerr=emailerr+1     
4 B) M: D1 W" l+ d2 [7 ]   }     
3 o- P9 W9 b' [; [4 p) S/ K  }     " M7 M2 a5 {4 q% K# h# Y1 P
if (emailerr != 1)     
' |9 D4 b; @: x8 z# H  P* L  {     : a; q" E$ p8 S
  alert("请输入正确的E-MAIL地址");     . Q- T; m# q5 v1 J0 r
  document.sform1.email.focus();     % r4 Q+ }, n. b" j3 N; v' O
  return false;     
0 y2 P) d$ N9 ?/ R! N7 u/ K5 g  }           7 a) k3 q6 B6 z8 k# h+ l
  
1 K% l0 R. B. Q7 T if (document.sform1.checkask.value=="") 2 q: B, `% B' ^/ u; l
  { 0 s( w" b. C# O6 V& H
   alert("密码提示问题不能为空");       j. }* O. A' L' R/ B
   document.sform1.checkask.focus();     
1 o9 a; p% k. s# C1 m, i) {( b   return false;  4 n. Z. H+ c: L8 `% g& a
  } $ {' l% I' e$ i  n' y) u
if (document.sform1.checkans.value=="")
$ u& V* O4 d. c+ N% R) k; K( s  {
: \8 }* z. Z  P/ N   alert("您的密码提示问题答案不能为空"); & P& q* D' p, L/ o- {2 f: P
   document.sform1.checkans.focus();     
& b( q: L- _$ u5 x' b/ }, D' O   return false;  
# B0 o9 K2 y% e. d  }/ h4 V2 k6 Z$ w; D
return true;     : L# `2 j3 o  l- w" Q. L6 n
     
6 a0 Q; G- r4 Q/ Q8 ~} 1 |0 x0 o1 w" r9 |! d
</script>

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