返回列表 发帖

一个注册表单验证的js

这个脚本对你可能有帮助:( l  c0 W2 v1 H' u" y
<script language="javascript">     ) V+ B5 [& t4 |* ~- N  n
function IsDigit(cCheck)     
: {) W3 W, F; T- {; ~' k7 \; p. z/ ^: y {     
8 o- Y: f) m# O3 m! r) S7 M return (('0'<=cCheck) && (cCheck<='9'));     
4 ?/ U: h! P" `: t, ^, Q- | }     " h- _3 w) ~  J& \0 Y& L7 U& ^
     & M5 i1 ^5 D; D$ Q& @- z7 G
function IsAlpha(cCheck)     
8 H1 Z1 ~( h. @; M8 E {     - z3 K2 q" @* Q1 P" M' K- [
return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))     1 a8 I! D6 j" Y/ i$ z- U* H
}              ( Y  J& Q3 b% Q/ X1 e9 m
      ' m+ l5 i4 B! B8 h( N# W* q% |& l2 K
function IsaNull(cCheck)          % n$ j0 _2 }) a( e" E* W7 ~! Z  X
{         
% T/ T! C9 L& H' r# E( M, l return(cCheck != " ")         
, b, n% F; y" J }                              
; K9 F1 i% ]! l0 O# I( R. e, M      $ `. t# E* o7 {* G$ L' z% M
function checkform()     / n9 I0 [/ N7 z# `7 e4 S6 i
{
+ M8 ?, z# w; `8 f  id = document.sform1.id.value;     
! ?7 n* o. Y4 h+ E if (id == "")     8 Z1 I1 c8 w+ I! P% H6 X& o
  {     & H, {( ]4 K* H% ]' C9 o
  alert("请输入注册名");     
* o5 @! \0 t& A" E2 S  document.sform1.id.focus();     
7 z1 @& L; u2 W+ W  return false;     , W* y6 y/ A) a# E3 J
  }       r' M; P& U) A0 I
     
3 ^, B1 Z5 L6 x( j' ]% A6 S$ x for (nIndex=0; nIndex<id.length; nIndex++)     9 @; s) k* r1 u1 H& f: B& H4 n
  {     
$ M  S+ A# i7 C2 v% p  R  cCheck = id.charAt(nIndex);     
, v) ^7 B5 b: r9 _6 X: S  if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' || cCheck=='.'))     
1 Y8 @- _. {8 E% R" E/ C   {     
2 }2 l  g) V+ {   alert("用户名只能使用字母、数字以及-、_和.,并且不能使用中文");     
( }6 ^/ L: r/ a9 T+ x   document.sform1.id.focus();     ) t, O6 x- F% G
   return false;     
* w7 r5 d8 g/ u& {5 t   }     # ~% a) w# V1 K0 d3 S
  }   v: K% h  [! S
  chineseid = document.sform1.chineseid.value;     ; }2 V7 ]. G! f+ B0 F! E
if (chineseid == "")     
) Q2 k7 a$ W) h+ w5 V  {     7 M0 t: Y1 Z, G; ^, U$ H% [
  alert("请输入中文昵称");     
* L& ^0 p' U, o9 C1 l, \  document.sform1.chineseid.focus();     * Y9 Z+ Z; y# R
  return false;     ! a1 I: |6 f+ K: a+ {# ^
  }   
2 L2 {0 H5 h8 l( n7 M- Z! |! G password = document.sform1.password.value;     
. K0 }- I3 A7 @- ` if (password == "")     
2 L! p/ @. X, i$ x) h- N  {     2 g7 m, m! f. [  T- o: i
  alert("请输入登陆密码");     / {% o, d6 U. G$ r
  document.sform1.password.focus();     4 E# h7 |: y4 u4 |+ j2 L
  return false;     
' l' B( T7 F2 c) n, n2 x  }
9 N' `9 l' |5 w. Y password1 = document.sform1.password1.value;     
! b7 T# t% [6 K, W; } if (password>password1)     9 B: h5 b. ]1 i) r! ?0 g
  {4 n* p1 X( ~) I, g' l8 m. [
     alert("重复密码与登陆密码不相同");     4 _" o6 h/ S; n: I6 _  R: N' ^
  document.sform1.password.focus();
1 Z) p  u* k- ^" W: O, c4 c1 C9 i2 l                                document.sform1.password1.focus();     
0 C$ J5 j" _$ z. S  return false;
9 D( j" I  n$ f  }  
/ \# U  B) O4 H/ [. Tif (password<password1)     . N9 K* [/ D+ c( g/ H
  {0 T$ h3 b, J8 `
     alert("重复密码与登陆密码不相同");     : J# y+ i; P+ P9 X7 \( N2 ]5 s
  document.sform1.password.focus();
, J" T( \$ V. e8 h7 N! n8 y                                document.sform1.password1.focus();     
' r# s7 v' Z5 l! o  return false;
# V! r- Y0 J8 O: ^0 r/ o+ Y  } : R* m5 U( K" z
if (document.sform1.email.value == "")     # l& O$ `) P1 h& w' b) m
  {     
  C" C+ R* p" L% m$ w6 ^  alert("请输入您的E-MAIL地址");     3 ]7 M7 h9 A1 v0 d1 K$ r
  document.sform1.email.focus();     & _9 K# P4 W2 S: J) y
  return false;     1 [$ r  g( ^7 {3 x" K- j) {* ]" |
  }     
5 \1 f& p( j2 y+ |5 l6 s1 P* m      ; r9 f& l  A) i6 J7 ]0 L
email=document.sform1.email.value;      0 f6 p7 F7 q" q: o# X
emailerr=0     , {" A  L( S( A1 |' R
for (i=0; i<email.length; i++)     
& z1 ]' K/ ]( l1 U& [3 a  {     
7 s" q, ]9 Y, ^& T1 H$ ~! j  if ((email.charAt(i) == "@") & (email.length > 5))     3 Q. N! K* Z9 x8 F# m8 D
   {     6 k& e9 A4 A0 p8 i% i
     emailerr=emailerr+1     
. k" k0 R9 s3 A& `% k" q   }     1 f6 K% j  g# L8 E1 i- p
  }     + O+ @1 z& \9 e
if (emailerr != 1)     9 P2 P  G: S8 r( j. o" E
  {     & t2 j' R1 ~/ F6 X
  alert("请输入正确的E-MAIL地址");     1 G) R# a" t7 d0 ^3 o2 w- o- c
  document.sform1.email.focus();     * g9 P6 I/ P2 m% x# H+ J
  return false;     ( p4 d& M* P. e1 o& k
  }           
6 B9 G1 B' ~" s' a- Q5 D  
# ]& T+ }6 \! s$ a; v if (document.sform1.checkask.value=="")
- t& {8 M+ t0 ^) P2 f  {
; j+ a. q0 A1 [" a) w8 N   alert("密码提示问题不能为空");     0 z, x) ]4 u' [: \& _7 e
   document.sform1.checkask.focus();     $ x" Z4 m6 o1 H( @0 j+ n8 i
   return false;  1 E) P9 F0 p( w' s* k
  } : X" `2 ?$ I/ C3 n
if (document.sform1.checkans.value=="") 0 _7 Q8 T2 U. N( P3 H5 Z
  {
% [) N' H/ J6 t; e6 d8 C   alert("您的密码提示问题答案不能为空"); 6 g  b2 B5 z7 N6 D% J
   document.sform1.checkans.focus();     
5 E$ W. y( T9 U9 r9 N: I! K' Y   return false;  
+ ~! e: f  E. x; A# S8 c4 I  }
& [1 z( d; z$ S) B return true;     
5 j) s% p& W* l/ @8 E1 q3 o3 ]     
0 [& |2 l+ q# Q* W6 i6 s} 0 H4 s" Q& d7 d
</script>

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