返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
1 J/ B2 Q: @" z  E6 O0 M& P9 E<script language="javascript">     
( F- p" l8 t" a( W* rfunction IsDigit(cCheck)     2 m; H( C; ~% m( Q4 E
{     
) Q1 _/ A. u$ J7 Z5 a0 `' h$ l& q return (('0'<=cCheck) && (cCheck<='9'));     6 R" E' i- V& @" _
}     
/ ]+ x7 L4 _: I. g) \+ ?     3 G1 X+ C4 \, j5 ]+ f1 _- `+ L+ r
function IsAlpha(cCheck)     8 p* T) I) }4 n; c& [  j
{     
4 ]* ^. T/ r  ?4 U" K return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     7 F8 C. ?4 t- g* _2 J
}              * B/ Q) o/ M8 e, J
      6 o* d% f; }# @2 P7 }1 ^  k& m: _
function IsaNull(cCheck)          ; N! `- }0 x+ c5 {$ G+ s* |2 L" e2 o
{         
3 n( w" Q4 ?& Z8 M3 g return(cCheck != " ")         7 T+ f; {9 K3 j
}                              
$ L- E5 F1 _9 a1 v3 W" P2 Z; N      , O4 A. X; n* a! B9 m3 @/ o
function checkform()     - o9 b) x0 `" n, K: B- w
{ 3 [7 K: w/ c, E9 S3 Q. A0 L
  id = document.sform1.id.value;     
* _. _7 x0 P4 j4 m8 g+ o if (id == "")     
+ s  o8 k! _7 f, K  {     
9 B! z7 O( Q5 @; j9 z  alert("请输入注册名");     $ |% y- c- j4 _& l, M! ?& @1 A
  document.sform1.id.focus();     " J0 e9 ^! M$ I1 f1 [1 t
  return false;     
  J3 Q1 G$ a3 x  }     " R  ]& F' g4 Y' D' w+ h) g
     ! q/ B! S6 ~8 P! h8 x; p# U
for (nIndex=0; nIndex<id.length; nIndex++)     & {! {& L. i2 ~1 ]* Y
  {     
; K& x! W. Q/ H: N4 S  cCheck = id.charAt(nIndex);     6 W/ r9 F/ |3 @6 _: {9 F' f
  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     ; s' t5 H) ]- f: V
   {     1 \- X/ C1 W1 l0 Q2 F8 l6 q
   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
8 z$ D+ C' M: s- W3 U1 p/ U  ]6 S   document.sform1.id.focus();     
$ y+ F% _9 `! [% }% h- P6 U; h3 y4 x   return false;     * w1 n' u) h% X# E2 ?
   }     
2 e! X4 \4 I4 n. [7 F2 A/ Z% X5 k  } 7 ~) b8 M* o  h3 F
  chineseid = document.sform1.chineseid.value;     3 v* X* U; @! {% o
if (chineseid == "")     
  v. l9 }' l) K1 }# n9 G  {     7 F- Y/ D) R% Y% l1 b
  alert("请输入中文昵称");     
: b& U& j9 l- l# p  document.sform1.chineseid.focus();     
+ S! |% G+ x; \# W/ X3 S  return false;     " @* A' f8 W1 B! c
  }    + n6 L0 O$ d/ b: U
password = document.sform1.password.value;     7 Q) K! Q" N9 X  ?
if (password == "")     7 [/ \% h1 d+ q$ Y( r& s, n. r8 J
  {     ( {. T. i2 z2 K2 m
  alert("请输入登陆密码");     
0 K. r2 _( l: C  document.sform1.password.focus();     
. m- ]& B9 |2 ~8 n9 ]  return false;     
( U" B. W% U5 G- p. P6 L  }
3 x. J# m  K: R+ T- k! u password1 = document.sform1.password1.value;     , j$ i4 p; N. V# r0 i* T
if (password>password1)     9 l4 O7 S8 m/ x& v% R
  {5 U  K$ Z$ {+ I5 I+ V& }0 j
     alert("重复密码与登陆密码不相同");     + w1 ^4 p. w/ G
  document.sform1.password.focus();
+ B- \7 s: L! B8 e8 g; q; Y                                document.sform1.password1.focus();     ; D2 e4 u2 C3 \; i* s+ f% H
  return false;$ ]% D' j$ B! `- \! B
  }  
5 F  e/ n6 z9 z% ?if (password<password1)     
9 k8 d/ z& M1 E  m) d  {
! U5 i; S4 s' Z. \/ {! ]2 x  o     alert("重复密码与登陆密码不相同");     5 s) h7 b: L7 O8 H; R2 p/ I
  document.sform1.password.focus();
. f6 ~; l  C2 \+ g/ q0 p' N& T# ?                                document.sform1.password1.focus();     
3 f# E! n. N1 m6 M4 k/ P$ J  return false;
% ]7 B/ e. v1 q8 ?, Y  }   Z) J) U7 J0 Z( h
if (document.sform1.email.value == "")     6 }1 f) T/ F4 n/ r; i/ T8 D( x0 q) U
  {     
+ c. P% A0 e' k  alert("请输入您的E-MAIL地址");     * J- s$ w0 d7 F' X
  document.sform1.email.focus();     1 d% G$ }0 u7 E3 {4 d8 P: r! Z
  return false;     5 v) V! O) N$ e. K' Q% `+ V* I: x" N
  }     ) F5 f; ^( E3 }
      
2 f' d0 |1 S* v! s3 |$ z( p email=document.sform1.email.value;      
- I/ I, S# G0 |$ {1 n emailerr=0     
1 Z- Y4 J  c3 g; `. f" [ for (i=0; i<email.length; i++)     
0 i8 j# b4 a; A3 r: d# s7 l  {     6 {5 ]& E) K, `7 Q7 o0 P: D+ R
  if ((email.charAt(i) == "@") & (email.length > 5))     
* M( `. C$ l0 h   {     
$ A! P' }( L) ^     emailerr=emailerr+1     
9 F# P, f' O6 W- m  S. f$ o! G- W   }     
' A% q7 v1 T, n" W$ U7 [  }     0 u5 o' K& F4 |6 {* {! Z/ X
if (emailerr != 1)     
2 w7 S9 s7 _1 Q+ L4 h$ v' g# L# Z+ F  {     
+ p) x6 c' s# c9 M$ o$ l  alert("请输入正确的E-MAIL地址");     0 c) }7 N1 ?( P7 f* G
  document.sform1.email.focus();     
7 x9 `/ P. G! l# L2 L4 V' E+ \. Z4 x  return false;     6 W) O4 i0 m6 `  h2 _- @$ ?
  }           0 x7 ?! k: S2 U1 d9 l4 q
  
/ P! q' Y! F0 p3 g# X if (document.sform1.checkask.value=="")
; ], ]- P. ]: M; }  {
& _) B$ ?% j. \* r9 X  E( g   alert("密码提示问题不能为空");     
- ^" z0 t7 t  }6 ?) B" J6 X' {, D   document.sform1.checkask.focus();     - a  {( X, S! U' Q. T
   return false;  
) ~* E, ?( K) B; A  } $ O5 n4 p  i1 `! ~7 T2 \7 g
if (document.sform1.checkans.value=="") $ s' w6 A0 R) p4 n, j1 N
  {
# v' T4 g$ ]# W   alert("您的密码提示问题答案不能为空"); ; b) T3 s3 L: s, s
   document.sform1.checkans.focus();     
. o3 @0 I6 B6 z% I" L' y   return false;  9 L* W2 D$ D- Y1 \3 o/ v" g0 p
  }& Y; q# w4 j4 s# f
return true;     7 }  R0 b. a  {( w4 w, O% `4 Q
     9 E/ R5 m6 W! i* {/ ~/ v* u: T
}
+ ^/ w2 o9 ~$ `</script>

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