If string contains 5+ uPPerCASE characters then redirect

Posted on 2009-04-28
Last Modified: 2012-05-06
I need to check a string to see if it contains >5 UppER CASE characters and or if it contains numbers. IF either of these are tru then redirect to a new page.

Please help
dim regex

set regex = new regexp = true

      regex.pattern = "[^A-Z]"

set regmatch = regex.execute(strFname)

if regmatch.count >=5 then

  Response.Write "<script type='text/javascript'>alert('5+ Uppercase characters');</script>"


    Response.Write "<script type='text/javascript'>alert('Your Good');</script>"

end if

Open in new window

Question by:pGustafson
    LVL 9

    Accepted Solution

    Does this do what you need?
    Dim hasNumber
    Dim has5Caps
    Dim capCounter
    hasNumber = False
    has5Caps = False
    capCounter = 0
    For X = 1 to Len(Fname)
      If (IsNumeric(Mid(Fname,X,1))) Then hasNumber = True
      If (StrComp(UCase(Mid(Fname,X,1)), Mid(Fname,X,1), 0) = 0) Then capCounter = capCounter +1
    If ((capCounter >= 5) OR hasNumber) Then Response.Redirect("targetPage.asp")

    Open in new window

    LVL 9

    Expert Comment

    Quick explanation if you are interested....
    Lines 1-3: Initialize variables
    Lines 4-6: Set default values for these variables
    Line 7: Start of For Next loop. Goes from 1 to however many characters in the string.
    Line 8: Check the character at position 'X' in the string. Is it a number? If so, make our hasNumber flag true.
    Line 9: Take the character at position X and make it an upper case.... compare it to the original character in position X. Are they a case sensitive match? If so, increment our counter (capCounter) keeping track of capitals.
    Line 10: End of loop
    Line 11: Check the capCounter and the hasNumber flag. If the flag is raised or capCounter is greater than/equal to 5, redirect client to a new page.
    LVL 9

    Expert Comment

    I couldn't resist. I re-wrote this a little bit. Makes it a little more streamlined. It also breaks out of the loop at the soonest opportunity possible, that way if you feed it 100 characters, it doesn't necessarily have to check them all. The two control variables (Max number of caps and target redirect page) have been moved to the top to make them more visible and easier to change.

    Dim exitPage : exitPage = False
    Dim capsCounter : capsCounter = 0
    Dim maxCaps : maxCaps = 5
    Dim targetPage : targetPage = "targetpage.asp"
    For X = 1 to Len(Fname)
      If (IsNumeric(Mid(Fname,X,1))) Then exitPage = True
      If (StrComp(UCase(Mid(Fname,X,1)), Mid(Fname,X,1), 0) = 0) Then
        capsCounter = capsCounter +1
        If (capsCounter >= maxCaps) Then exitPage = True
      End If
      If exitPage Then Exit For
    If exitPage Then Response.Redirect(targetPage)

    Open in new window


    Author Closing Comment

    Thank you so much... That worked awesome.

    Author Comment

    That's awesome... I wonder if there is a way to have it check multiple strings for a total maXcap number of 5
    LVL 9

    Expert Comment

    You could put your string in an array then iterate through the array. Each item in the array would be matched against the For...Next loop.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now