返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:
( Z3 ]9 [! C% m8 |8 O" a<script language="javascript">     
) r! c2 e0 A3 q$ gfunction IsDigit(cCheck)     
1 C. r- I# s/ W) C0 B {     % }  q( j- Z, R$ ^7 m- [3 u7 k; `
return (('0'<=cCheck) && (cCheck<='9'));     # k' v4 }! d" Y; P
}     
( C0 p! S, U' z  H" `4 {2 w! A/ g" G3 h     : m8 U. L6 N' P5 k; \) E
function IsAlpha(cCheck)     0 F" m# ^5 e& R
{     
% H3 |! K7 ]4 w; J: }' X return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     
; Y8 W! s1 r: S( n! X+ s }              
0 }) V1 J$ b) A( E% t" J      
* _' a4 y. s3 Zfunction IsaNull(cCheck)         
5 g- m2 `; n2 m1 H. }9 g {          : w6 s. ^' E( _9 J6 G0 i
return(cCheck != " ")         2 E$ a+ @/ @! |/ Q. C* v" D
}                              
* O4 {) |' l7 f      ; X, K, ^0 t+ {* m& k. k
function checkform()       Y/ M: i8 [7 d2 ^
{ 2 j) H' E: f- Z
  id = document.sform1.id.value;     3 r7 l4 F) B/ H+ t" ~+ e
if (id == "")     
2 t# l% T! l" Z9 o3 `1 L! }  {     
2 r) I" Z5 X7 ?! i7 i; c  alert("请输入注册名");     * j. V1 Q7 h/ F
  document.sform1.id.focus();     
2 h+ X: \5 Y, Q  return false;     - Y: \2 w% O/ g0 \
  }     . P' j- `8 E  a/ I
     
! |  p/ r% X- M6 m" R' W for (nIndex=0; nIndex<id.length; nIndex++)     % s: L) X7 z  N  \: P4 j& ^' {0 ]
  {     
! ~8 o8 t7 `* p  cCheck = id.charAt(nIndex);     
4 x- i1 q& M# L0 @  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     3 D5 h2 Q# i* z! L& A4 O( |; h  K
   {     
8 r; m  B: \+ L   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
. F- G* S4 W" u, y7 h+ v( r   document.sform1.id.focus();     3 a( U! l, B( i6 p
   return false;     
5 b1 V# q% K2 v   }     : }% `' b3 E9 Z. a
  }
. h2 D5 Z% c- [- S. b7 l  chineseid = document.sform1.chineseid.value;       y( i6 n' s, J- T
if (chineseid == "")     0 {6 m9 `0 k5 d" s1 ]; ]
  {     0 |. |2 `$ E! I# V" V* Z8 ^
  alert("请输入中文昵称");     
  ^1 v1 ]6 |+ ~9 H2 u7 a  [9 E  document.sform1.chineseid.focus();     : }7 R. `6 c) ?; x4 h
  return false;     ( |1 [/ z+ z  F3 s
  }   
6 M2 P. u6 i/ N- n. a+ s' A password = document.sform1.password.value;     
1 l/ C1 O' {! ~4 L4 @$ A, } if (password == "")     
9 k* O# P! I$ c+ f- _! |  {     3 D( @2 L) `, Z6 W# h
  alert("请输入登陆密码");     
9 N: W( }8 j: t" u  document.sform1.password.focus();     
* J+ l$ f  g" f9 K& W* M5 U  return false;     
+ p4 _& [- ]- D- Z* I! s6 C' I8 z6 D* }  } - C* I& C6 b  t5 g+ M
password1 = document.sform1.password1.value;     
3 S$ |, \9 a! ]. f if (password>password1)     $ U0 N5 t- I, h5 k3 V
  {$ A; A: l  I6 f4 R
     alert("重复密码与登陆密码不相同");     
0 h! D  c; G* x& z2 h' Q  document.sform1.password.focus();
7 ]# W* v" w4 a+ @! Y5 E                                document.sform1.password1.focus();     
$ U- H. T2 c0 s2 s) r0 Z! g7 I  return false;
! s/ e1 l5 z# j* e# w7 Z& e- k2 s  }  
- M' |9 K9 m$ d5 B+ vif (password<password1)     : H5 U" C% @. @. m( i7 M
  {& ^/ w4 M, X; g' k- C  f
     alert("重复密码与登陆密码不相同");     ; ?; p" V; y0 V/ _. K
  document.sform1.password.focus(); / v. H  a, O3 @  @! q: }
                                document.sform1.password1.focus();     / U  U! W1 J0 c, W
  return false;
. G7 ?4 S  S4 ~, u( A* i7 E3 f  }
! p' c$ I# ?, t; b; |  d, h% y if (document.sform1.email.value == "")     % p9 x0 R7 E, f; v, j; L; _
  {     
' B5 g6 L% t4 P" z- c6 T  alert("请输入您的E-MAIL地址");     
: B4 m- R! [3 k! E  document.sform1.email.focus();     
! g9 g7 v" r' U( ?  return false;     
# i1 M1 C4 F6 r  }     
  n# [* P' _  f+ I      
& w& @; C& j9 M) T7 g- H email=document.sform1.email.value;      
2 w( }9 B; G( |  c emailerr=0     5 K% f; u: L2 e0 ^
for (i=0; i<email.length; i++)     
4 U( [5 A' t9 T3 s$ ?7 {  {     # D$ v) U; c% M: w: F: z; \
  if ((email.charAt(i) == "@") & (email.length > 5))     
* N7 @+ T! t: @5 l. }   {     1 b  E5 b) I7 {% C. j
     emailerr=emailerr+1     
) U7 g# k- f: X1 v9 S- x; q: v+ Z3 W   }     $ S4 h# l) q! P3 \6 y+ A# S# H
  }     5 x3 S  t9 n5 z. P1 S$ g: M
if (emailerr != 1)     % v0 S1 x8 F6 f  w
  {     / A% J3 J' U& F
  alert("请输入正确的E-MAIL地址");     9 V$ g: `2 M5 @
  document.sform1.email.focus();     
4 Z' s7 i/ k( R' p/ V* ~9 v  return false;     / I( [% p# c2 X% G& c
  }           2 i' s9 D( q( T  ^- e
  , O: ^+ w3 O- D: ?
if (document.sform1.checkask.value=="")
0 p* z# b) s: K; e) |  {
& t4 }7 N1 v: [0 E. Q! }5 F, O# }& _   alert("密码提示问题不能为空");     
$ S+ a% [; ~3 c# _0 d3 }- Y   document.sform1.checkask.focus();     
$ I" j( {% _' O7 v) G$ j   return false;  1 N$ E4 e% \4 m: g  \4 ~3 L( z
  }
$ W; ?. n$ J0 `( y5 }3 c' K if (document.sform1.checkans.value=="")
+ b- w/ w- A; @/ t* |' w& \* S4 z  {
- w! l. y6 @. ~# O9 u- I" }   alert("您的密码提示问题答案不能为空"); $ P/ _8 {! o9 n+ ~# @# R2 v3 P2 o
   document.sform1.checkans.focus();     
) T* \- x1 [! E$ E5 K+ l* G% d  p) I   return false;  $ t* x8 k# g. O' w, _3 H: u
  }
$ P7 m/ u, l: }2 w& r% s9 s7 E return true;     
6 i, g" L5 I. U, r7 L  o8 y     
$ Z6 |/ d3 P# ^% x. \! T9 i% ?} ; s5 F- ]! ~; H5 e' u
</script>

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