驗證字符串中是否包含有效字符

字號:


    在常規(guī)輸入表單的使用中時,經(jīng)常會要求用戶只能使用A~Z、a~z、0~9內(nèi)的字符,那如何實現(xiàn)在服務(wù)
    器端的驗證呢?
    下面是一個用于以上驗證的函數(shù):
    <%
    '**************************************
    '文件名: Asc.asp
    '描 述: '驗證MyString是否含有有效字符A~Z、a~z和0~9
    '**************************************
    Function TestString (MyString)
    Dim TempStr, Length, Result, I, Char, Ascii
    TempStr = TRIM(MyString)
    Length = Len(TempStr)
    Result = False
    For I = 1 To Length
    Char = Mid(TempStr,I)
    Ascii = Asc(Char)
    '判斷字符的ASCII值
    If 47 < Ascii < 58 Or 64 < Ascii < 91 Or 96 <Ascii <123 Then
    Result = True
    Exit For
    Else
    Result = False
    End If
    Next
    TestString = Result
    End Function
    %>
    該函數(shù)的調(diào)用方法如:
    TestString(MyString)
    如果MyString是要驗證的字符串,則函數(shù)會返回一個Boolean值:True是指字符串在所有的Alpha
    值之內(nèi),F(xiàn)alse不是.
    具體用法舉例:
    <%
    '**************************************
    '文件名: MySTring.asp
    '描 述: 驗證MyString是否含有有效字符A~Z、a~z和0~9
    '**************************************
    dim str
    str = request.form("test")
    if TestString(str) then
    response.write "恭喜,字符串合法!"
    else
    response.write "里面有非法字符串!"
    end if
    Function TestString (MyString)
    Dim TempStr, Length, Result, I, Char, Ascii
    TempStr = TRIM(MyString)
    Length = Len(TempStr)
    Result = False
    For I = 1 To Length
    Char = Mid(TempStr,I)
    Ascii = Asc(Char)
    '判斷字符的ASCII值
    If 47 < Ascii < 58 Or 64 < Ascii < 91 Or 96 <Ascii <123 Then
    Result = True
    Exit For
    Else
    Result = False
    End If
    Next
    TestString = Result
    End Function
    %>
    <form method="post">
    <input type="text" name="test">
    <input type="submit" value="submit">
    </form>