Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post

    Check, Numbers and Letters Only.

    I am going very slow at the moment, and I can not remember how to check a string to make sure it's only numbers and letters as characters and not other symbols.


    Thank you.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    OracleGuy

  • #3
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    I found it out...

    This will be my function...
    Code:
    Dim Z As String, X As Integer
    
    For I = 1 To Len(Z)
    
    X = Asc(Mid(Z, I, 1))
    
    If Not ((X >= 48 And X <= 57) Or (X >= 65 And X <= 90) Or (X >= 97 And X <= 122)) Then
    
        MsgBox "error"
    
    End If
    
    Next

  • #4
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Originally posted by oracleguy
    http://www.codingforums.com/showthre...&threadid=7472

    Like this?
    Well, I didn't see your post there until now, but thanks for the support!

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a more concise function now - perhaps may be improved upon in the future, but as simple as I know how - however I do like your approach of using Asc() values, Morgoth, that's yet another way to do it! It's probably not as fast though since you're looping.

    Code:
    Function IsAlphaNumeric(str)
       Dim ianRegEx
       Set ianRegEx = New RegExp
       ianRegEx.Pattern = "[^a-zA-Z0-9]"
       ianRegEx.Global = True
       IsAlphaNumeric = (ianRegEx.Test(str) = False)
    End Function
    http://www.solidscripts.com/displayscript.asp?sid=10

    Example usage:

    Code:
    <%
    If IsAlphaNumeric("SDF(**(&") = False Then
       Response.Write("Please refrain from using characters other than letters or numbers in your entry.")
    End If
    %>
    Last edited by whammy; 02-02-2003 at 04:27 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thank you whammy.

    I haven't really gotten into RegExp. I don't really understand exactly what it is.

    Do you hav any tutorials?
    I used two scripts, now three, that use this, and I want to understand what it is doing, how, and why.

    Thanks again.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Some great tutorials here:


    What this does:
    Code:
    Function IsAlphaNumeric(str)
       Dim ianRegEx
       Set ianRegEx = New RegExp
       ianRegEx.Pattern = "[^a-zA-Z0-9]"
       ianRegEx.Global = True
       IsAlphaNumeric = (ianRegEx.Test(str) = False)
    End Function
    [^a-zA-Z0-9]
    • [] - character grouping brackets
    • ^ - when inside grouping brackets, means "NOT" (otherwise it represents the beginning of the string)
    • a-zA-Z0-9 - the characters NOT to match


    the reason I use

    IsAlphaNumeric = (ianRegEx.Test(str) = False)

    is because if you use:

    [a-zA-Z0-9] (notice there is no NOT)

    then

    IsAlphaNumeric = ianRegEx.Test(str)

    will ALWAYS return true if ANY of those characters exist...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thank you whammy.

  • #9
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    lazy way

    an easy or lazy way is

    dim _myInt as integer

    public property isInteger() as boolean

    get

    dim _i as integer
    try
    _i = cInt(_myInt)
    return true
    catch
    return false
    end try
    and get
    set

    _myInt = value
    and set

    end property

    if true then it is an integer , if not ... not

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And how does that account for Alpha characters?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #11
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string

    yes for string you just use regular Expression but for any other format (date ect.. ) you cabn use that leasy way

    or even easier

    Dim MyVar As Object
    Dim MyCheck As Boolean

    MyVar = "53" ' Assign value.
    MyCheck = IsNumeric(MyVar) ' Returns True.
    ' ...
    MyVar = "459.95" ' Assign value.
    MyCheck = IsNumeric(MyVar) ' Returns True.
    ' ...
    MyVar = "45 Help" ' Assign value.
    MyCheck = IsNumeric(MyVar) ' Returns False


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •