返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
7 G* D! k8 G, o" \, C<script language="javascript">     7 D1 T% z$ ~! t- Q! U
function IsDigit(cCheck)     % `4 F3 d5 |) P# F1 D* v$ G3 ]
{     , O$ f5 S5 r+ s: C8 T
return (('0'<=cCheck) && (cCheck<='9'));     
% h, o* R7 D" ^! T$ X3 n2 g# r }     
" X' ]9 D) V0 K. e9 ?     4 A- H9 J8 T, ~8 l
function IsAlpha(cCheck)     5 C5 `& j, n4 z
{     
0 @/ g- f. S2 a return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     7 D( _/ [8 a: }5 X
}              4 J  I! z& H3 ^8 h1 V5 Y
      % W6 f6 r9 @7 S
function IsaNull(cCheck)         
9 e  @0 l+ r8 Q+ Y. r# x' O& h7 H {          & F& Q, L/ `* w9 |2 H
return(cCheck != " ")         
" p( v9 w) D. ~8 g3 B3 W }                              
$ l- ]0 B/ g& M/ p0 {% Q      ) r& _9 G0 G, E$ U! g. V9 h. p7 s  k
function checkform()     2 W5 ?- B$ q# R& x/ f& P0 \7 Y
{ 0 i5 T' W/ w% ~: M7 c1 J9 g, r
  id = document.sform1.id.value;     
" w# ~, G* @! q: X6 k  e if (id == "")     
/ w7 F* f- @% ^; Y  {' ~9 F  {     " v0 G; y4 I- ~; m2 N, n6 J8 W
  alert("请输入注册名");     
  P3 h0 w- d; B3 v6 Y  t0 R  document.sform1.id.focus();     
2 v4 ]5 u# g& E) t! e8 a8 V  return false;     
3 [6 x$ N1 @' D$ a  }     2 R$ [8 y" G/ M1 |9 [
     
' G3 ]* u; K, l7 a& B' t for (nIndex=0; nIndex<id.length; nIndex++)     ' B# `) z5 C2 J9 r4 N
  {     
% I: C/ {4 r: A2 [1 _  cCheck = id.charAt(nIndex);     
& w) W, ^1 t/ M& e  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
1 d& r$ u& f$ S. d   {     
0 e3 S! W* M1 J5 H+ Y   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     % _4 V7 y7 u  n6 F
   document.sform1.id.focus();     
9 z/ r- W5 N4 E: W5 X   return false;     & q  C% w/ Q! F8 `6 W, v
   }     0 b- n: g; x# f  B" L6 _$ A
  }
! w4 O/ q9 o: ~" l  chineseid = document.sform1.chineseid.value;     
1 K2 Z# T: A* P6 F6 b if (chineseid == "")     
0 l$ q; Y% r8 A- ~5 V  {     4 d, c+ I0 P5 t. o; @- I& h7 L
  alert("请输入中文昵称");     
3 T) w! C- z' C  z  document.sform1.chineseid.focus();     9 }9 q* R/ A2 ^" y6 F
  return false;     
) A; u3 A5 J3 W) v/ t  }    ' w4 ~: v: }  o, ^# M
password = document.sform1.password.value;     ( t, f1 ^$ S. A4 C3 G/ i" [/ C
if (password == "")     
5 K8 r" E$ G- @% a) l  {     % T! y* Z$ I- ?' v* o5 h  S& K
  alert("请输入登陆密码");     
7 R  T8 S: C) c5 A& g  document.sform1.password.focus();     ( C0 Z% J! K7 ?% J
  return false;     % `6 J  x( @# [3 ]1 K$ E5 A
  } $ ?: `: F4 ~% \5 b2 [* H* i2 G
password1 = document.sform1.password1.value;     4 B3 O4 p6 m8 T/ S, ~
if (password>password1)     + Q% C8 i/ Q: m! L1 n5 Q2 A
  {# w" a4 O" k- c, W( ~" S
     alert("重复密码与登陆密码不相同");     + C6 R+ W8 ^6 i- s
  document.sform1.password.focus(); ( n% H1 }- B3 K9 |2 d$ Y# _: Q
                                document.sform1.password1.focus();     
' ~2 G; [) S; `9 Z5 [, D4 Y  return false;5 ?3 r) x7 t1 P* y* r6 W
  }  
. z# v5 L3 Y! Z9 I# gif (password<password1)     : R9 F" [5 g4 ^# `
  {
# ~% W- `: T3 H2 w' ]$ O+ e9 L1 U/ W     alert("重复密码与登陆密码不相同");     
! h. }" f; K, f* x+ |  document.sform1.password.focus();
, q1 A% y' T0 t0 D; K$ x4 n                                document.sform1.password1.focus();     ' l% D, \! p- i  k
  return false;. X8 p& c* |9 Q' h; E! q% O; q
  } 8 `: }) @1 V  y; W' Q: {% z
if (document.sform1.email.value == "")     8 w* R& V( k0 f! r, h: [0 Y
  {     
2 M7 L1 d* K' q& C$ m7 a: u  alert("请输入您的E-MAIL地址");     
: [$ Q- q' [1 p  j  document.sform1.email.focus();     9 L1 |. h7 [$ G9 J$ R0 T
  return false;     
) f' r7 p/ `) o5 u  }     
' R& P* S. T2 w4 _. P% g      
) w0 U2 z. r# P1 p2 |, B email=document.sform1.email.value;      1 T  p7 X, f% }5 E4 \) }$ h8 O
emailerr=0     * c! g# G# C: d4 n7 D7 t; G
for (i=0; i<email.length; i++)     
2 X5 Q3 ]3 e: {1 a( q7 k7 X  {     2 P! {3 I7 s5 {/ M
  if ((email.charAt(i) == "@") & (email.length > 5))     7 s  N) U3 z* \
   {     5 ^3 C5 ^' t, N3 s
     emailerr=emailerr+1     
$ q5 ]/ F2 A5 }$ b: \" C   }     
) X7 @& G" N, ]2 U* e  }     : \7 g$ v5 l8 G" Q
if (emailerr != 1)     
4 @; _5 z5 u2 p  {     
; ^9 o2 C2 [- d( S0 R. d: {* Z  alert("请输入正确的E-MAIL地址");     
2 H9 |7 D5 N7 f& Y  document.sform1.email.focus();     
( Q1 k0 ^1 L9 f4 Q  return false;     6 d& ?/ ~* U2 a! N( N1 J" h
  }           , \/ e& ^' p0 l" H4 B1 f& \# `
  4 m$ ?( Y. ^- r
if (document.sform1.checkask.value=="")
& u) I4 ?7 J, O; C3 a3 u  { ' s8 N* y' A0 Z% h& ^
   alert("密码提示问题不能为空");     
' r5 u) g  m" m$ \2 |$ N) p   document.sform1.checkask.focus();     * ~. a1 ?* ?; X( H
   return false;  
. i. T  P: X" d( B. D/ u/ `1 M1 H: K  }
, P" }2 ~4 R9 k0 D. \ if (document.sform1.checkans.value=="") , y$ G  n9 _2 j4 V" _: T( b
  {
8 K/ q! A  G7 a- C2 G   alert("您的密码提示问题答案不能为空"); 4 M& j, V1 N" N/ O1 o) R
   document.sform1.checkans.focus();     6 N- I) p! V: I# a# F
   return false;  5 b4 b& U+ s# d  \+ {- e& E
  }
6 E: l4 x' W8 ?, {, G return true;     * h0 c# t9 r+ S
     5 N* q8 Q% v0 U" Z, |
} 6 D9 O% r: H+ h. i* v. N3 g9 S4 {, _
</script>

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