基于AJAX的表单验证--clanY’sBlog

来源:互联网 发布:怡合达选型软件下载 编辑:程序博客网 时间:2024/05/21 13:26
导读:


新用户中心的用户注册将采用无刷新操作,借鉴一下方案。


基于 AJAX 表单验证

validate.html





AJAX with ASP: Form Validation








      新用户注册
      
                  action="validate.asp?Type=submit">
    
        
        用户姓名:
                       onblur="validate(this.value, this.id,'txtUsernameFailed')" 
 
               value="" />
        
        
  
        
        性别:
                        onblur="validate(this.value, this.id, 'selGenderFailed')">
          [选择]女        
        
        
       
        
        邮箱:
                       onblur="validate(this.value, this.id, 'txtEmailFailed')" 
               value="" />
        
        
          
        
        电话:
                       onblur="validate(this.value, this.id, 'txtPhoneFailed')" 
               value="" />
        
        
        
        
        
        注:所有信息必须填写。
        
                       class="left button" disabled />
      
    



validate.asp




<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Session.CodePage=65001
Response.charset = "UTF-8"

If request("Type")="submit" Then
response.write request.form("txtUsername")
response.write "注册成功"
Else
fieldID=request("fieldID")
inputValue=request("inputValue")
result=""
If inputValue&"" = "" Then
  Select Case fieldID
    Case "txtUsername"
      result="用户名不能为空!"
    Case "selGender"
      result="性别不能为空!"
    Case "txtEmail"
      result="邮箱不能为空!"
    Case "txtPhone"
      result="电话不能为空!"
  End Select
Else
  Select Case fieldID
    Case "txtUsername"
      result=Validate(inputValue,"^[/u0391-/uFFE5]+$","姓名必须为中文")
    Case "txtEmail"
      result=Validate(inputValue,"^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$","非法的电子邮件格式")
    Case "txtPhone"
      result=Validate(inputValue,"^((/(/d{2,3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}(/-/d{1,4})?$","电话号码输入有误")
  End Select
End If
response.write result
End If

Function Validate(v,p,e)
  Dim objRegExp, strResult
  strResult=""
  Set objRegExp = New RegExp
  With objRegExp
    .Global     = False
    .IgnoreCase = True
    .Pattern    = p
  End With
  If objRegExp.Test(v) = False Then
    strResult = e
    Set objRegExp = Nothing
  End If
  Validate = strResult
End Function
%>

本文转自
http://www.51city.cn/blog/user1/11/archives/2006/4129.shtml