获得本站免费赞助空间请点这里
返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
: v4 v- G) U- @. B+ W4 j<script language="javascript">     
1 H: Y) W1 R0 r. ffunction IsDigit(cCheck)     " h& l! Z/ ~( [4 I: F" L
{     
1 z5 ~" M! u! _ return (('0'<=cCheck) && (cCheck<='9'));     & t4 C2 W* |- p' {* x
}     
; ^" Q$ p1 ^& K' j, R     & p0 a8 t1 y4 M- O- g
function IsAlpha(cCheck)     
3 q8 l, u, r& x {     
% P* \$ b& Z7 Q8 H5 b  [' J1 W return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
, e* {( u3 s0 A1 z' d1 ~# o }              
. L' ]5 E! h) b( ~$ z      
$ ]! I: D) W8 G- x" ~9 \3 H% ufunction IsaNull(cCheck)          9 C/ U, b$ o5 Y) n( H" y: K, h& T3 @
{         
/ m. l3 @$ U# s6 q  O8 I return(cCheck != " ")         
! X+ J2 e, X7 ?* ]0 p" J- k  A) B. F$ l }                              
' h0 ^$ T4 I  W2 L; k' n      " l# b, |2 E) A
function checkform()     
4 {. L  b. h, Y+ T3 _{ ' ^! ^8 ^" n- e7 `! Y/ D: W
  id = document.sform1.id.value;     0 M7 a& }6 f* v- [2 d7 b7 s
if (id == "")     
  h  g6 ~7 ]# J8 A9 W" E( |- }( g0 S  {     
: I9 ^. ^; D5 }3 _8 A- f  alert("请输入注册名");     
1 ^5 v0 z8 L6 r; w" [* ]+ A/ u  document.sform1.id.focus();     * H# Q, K7 Q! D; ?7 S: j
  return false;     
' l  |: V! E3 Z1 V- n$ e2 }  }     
+ I, ^- ?& O- O     % E# Y1 s. x: c2 F
for (nIndex=0; nIndex<id.length; nIndex++)     
: w6 r* A7 h4 Q5 }  {     + C) e$ i1 r- L9 F$ R! m: p
  cCheck = id.charAt(nIndex);     
6 t* _# ]" O; L* I* N: }  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
4 S* V, v  ^9 n, E: u2 [( _5 o   {     
/ N  c, h9 _. t" r2 {4 U   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
: b: ]* {% d  c. M   document.sform1.id.focus();     8 k7 A  }( X, C8 s
   return false;     
, k4 Q* `6 D% t. Y3 p4 w; m" c7 ^   }     
% d5 W  z# k+ X& l  }
$ Q; I% c4 m& g7 W. x  chineseid = document.sform1.chineseid.value;     ; L* x) y! f, u5 G6 C0 t
if (chineseid == "")     8 |  Z5 H) v8 v2 V4 [
  {     ' }# j& F* ~* o) y2 ]
  alert("请输入中文昵称");     " T7 k  J( a% b) \6 W7 N9 t
  document.sform1.chineseid.focus();     
5 F; R, e: s$ G( ]1 w  return false;     
% E. @3 f) m/ g$ E2 A  }    4 ]. A6 E4 s7 |/ y! t
password = document.sform1.password.value;     ! [( s: h) F0 c% b4 _* ~
if (password == "")     
" S, O9 m6 ~, m4 E' `  {     
8 ]8 l- E7 V+ k# O+ Y0 h% |9 C  alert("请输入登陆密码");     
# R, y/ ^2 b" r; `/ w  document.sform1.password.focus();       x* W  Z0 I0 a3 J9 J
  return false;     7 I+ D6 s8 C- H& e$ O. R5 n4 v
  } 4 ^* k5 g0 r/ f$ l$ I
password1 = document.sform1.password1.value;     
; D& z/ O( R; J$ x if (password>password1)     ; C( W7 d' C0 [4 |5 |+ i6 ^
  {
. ?7 |  l. _1 j8 T5 n     alert("重复密码与登陆密码不相同");     
5 Z' W" _) |, t1 h0 l5 U2 ]8 _  document.sform1.password.focus(); : X5 g3 A5 J$ Q0 a" |, i& o- x
                                document.sform1.password1.focus();     
+ K6 P- d- C' |3 S# v# }2 o  return false;1 E* U( k9 H/ T2 o6 d; H
  }  
5 i& h3 U3 q0 k6 M' x) Fif (password<password1)     4 k0 u# h; O7 ^5 ~
  {
6 B9 z, Q; t4 [- U     alert("重复密码与登陆密码不相同");     5 f& w  k6 U6 Z9 _
  document.sform1.password.focus();
& a; T& n$ M  {                                document.sform1.password1.focus();     
: \9 r3 \  }7 B" \! ]1 W  return false;
* @2 ^7 y6 X& n& Z" Z  } / F8 |  E2 ?! ^9 }9 Q0 c
if (document.sform1.email.value == "")     + O/ d0 u+ N  |. ^) E
  {     ) d$ b$ l/ ?5 o  ^
  alert("请输入您的E-MAIL地址");     
) t$ ~8 [. t: n! {  document.sform1.email.focus();     
: m$ F6 C: D/ P3 \3 A  return false;     & R6 `, A! e! o% L9 A
  }     
' D0 e: U$ G9 B! N      : O& z# B* k/ l7 z. C
email=document.sform1.email.value;      5 B8 Y) D% v2 I- z
emailerr=0     0 B6 m$ y8 N, R# i2 V, |
for (i=0; i<email.length; i++)     
; g7 t. p5 j6 J) w  {     9 L6 U: N5 \; z" @! x& T5 t
  if ((email.charAt(i) == "@") & (email.length > 5))     $ o7 ?0 o+ c+ J1 u# Y; B7 w
   {     / F' ?6 z3 f7 F5 T7 a$ R9 _9 v0 A
     emailerr=emailerr+1     
5 G9 O  G- e$ p2 T5 M   }     % ?6 A" A8 w! v& H+ d
  }     
8 ~5 q* S, V9 K if (emailerr != 1)     
) K& G% b" E, _  {     ) o- ?  q1 J4 s
  alert("请输入正确的E-MAIL地址");     
' q% r) O/ a, ]- c  i9 w  document.sform1.email.focus();       K6 K" `" F/ _; Y/ W+ v
  return false;     * i9 e. M' P. H$ E* j, [
  }           6 F5 w! x: K2 y: t* h: v
  
! i) N7 v8 ]& T8 P; \) Y  i if (document.sform1.checkask.value=="") * o, ^; I, P% l+ R7 J- J
  {
1 b, J2 L6 E/ K% f3 H   alert("密码提示问题不能为空");     ! D5 @/ P. G0 _, N
   document.sform1.checkask.focus();     ; d  `  x# S; K  L; \
   return false;  3 Q  S+ j2 @; t# ^: i+ ?
  } ! Q# a  `  A) X+ p! C: H
if (document.sform1.checkans.value=="") / L/ H, ^$ o, x
  {
0 S. e& O' x) Z   alert("您的密码提示问题答案不能为空"); 5 q2 }; ?# E7 q! L
   document.sform1.checkans.focus();     
9 k7 m9 f2 r% d6 ^2 g8 Q7 u3 |   return false;  6 i- ]6 \5 W) A) d, R
  }
! E- ?% g$ g6 L) K$ t; G return true;     + S5 y8 p3 b8 s% X
     
$ [' q, F3 W' \}   M" w4 ?# A  n9 y: G0 B
</script>

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