?
Solved

vbscript - regular expression

Posted on 2003-02-27
7
Medium Priority
?
586 Views
Last Modified: 2008-02-01
I need a regular expression pattern that satisfies the following.

First: The scenario,

I have a text box:

User must type in at least 1 character which is Aplha or numeric (A-z,a-z,0-9)

Now if they type in:

A. a dash and a space = no good
B. all spaces = no good
C. hit enter 3 times = no good

So basically I have to check the string to make sure it is has at least a length of 1, and that at least one of the characters is (A-z,a-z,0-9)

Thanks,
Chris
0
Comment
Question by:turbosig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 8035473
Try this;

<script language="JavaScript">

function betterThanSpencers() {

if (event.keyCode == 13 || (event.keyCode > 31 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;
}
</script>
</head>
<body>
<form>
<input type=text name=comments onKeypress="javascript:betterThanSpencers();">
</form>
<input type="text" name="moreComments" onKeypress="javascript:betterThanSpencers();"
</form>
</body>
</html>
0
 
LVL 1

Expert Comment

by:kyo3eb
ID: 8035559
Hope this helps:

============================================================================================================================================================

<form name="frmTest" onsubmit="return CheckMe(txtTest.value)">
     <input type="text" name="txtTest">
     <input type="submit" value="Submit">
</form>
<script language="vbscript">
Dim Strikes
Strikes = 0
Function CheckMe(strValue)
     CheckMe = false
     textValue = trim(strValue)
     if Strikes < 3 then
          Strikes = Strikes + 1
          if len(textValue) > 0 then
               for counter = 1 to len(textValue)
                    if instr(1,"abcdefghijklmnopqrstuvwxyz1234567890",mid(textValue,counter,1)) > 0 then
                         msgbox "Everything's OK."
                         Strikes = Strikes - 1
                         exit function
                    end if    
               next
               
               msgbox "Invalid Characters." & vbcrlf & "Strike " & Strikes
          else
               msgbox "Textbox is blank" & vbcrlf & "Strike " & Strikes
          end if    

          if Strikes = 3 then
               strikes = 0
               frmTest.txtTest.disabled = true
          end if    
     end if    
     
     
     
end Function
</script>
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 8035618
You can see that my example is to prevent special characters only as well as <space> and <cr>.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:turbosig
ID: 8035734
Ok, I am not sure you guys are getting this.

1. it MUST be vbscript
2. it MUST be a regular expression pattern

This is what the question title states, both of your ways are acceptable, but not what I am looking for.
0
 
LVL 1

Accepted Solution

by:
kyo3eb earned 60 total points
ID: 8035884
Took out the validation, I think you can handle that
========================================================

<form name="frmTest" onsubmit="return CheckMe(txtTest.value)">
     <input type="text" name="txtTest">
     <input type="submit" value="Submit">
</form>
<script language="vbscript">
Dim Strikes
Strikes = 0
Function CheckMe(strValue)
     CheckMe = false
     textValue = trim(strValue)
     Set strRegExp = New RegExp    
     strRegExp.Pattern = "[a-z]"
     strRegExp.Global= true
     strRegExp.IgnoreCase = false
     set strFound = strRegExp.Execute(textValue)
     msgbox(strFound.count & " characters found")
     strRegExp.Pattern = "[0-9]"
     set strFound = strRegExp.Execute(textValue)
     msgbox(strFound.count & " numbers found.")
end Function
</script>
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 8035890
Why does it have to be RegExp?

Surely you can use this;

strJunk = "abc123"

If (Len(strJunk) - 1) Then
    Response.Write "String is to short"
Else
    Set objRegEx = New RegExp
    objRegEx.Global = True
    objRegEx.IgnoreCase = True
    objRegEx.Pattern = "[A-Za-z0-9]"
    a = objRegEx.Test(strJunk)
    If a = True Then
        Response.Write "The string has a bad character"
    Else
       Response.Write "The string is clean"
    End If
End If

0
 
LVL 2

Author Comment

by:turbosig
ID: 8036089
Well I figured it out from kyo3eb's code, there were a few things that it didn't do, but I filled in the blanks.

Function CheckMe1(strValue)
    CheckMe1 = True
    textValue = ltrim(rtrim(strValue))
   
    If len(strValue) < 2 Then
          CheckMe1 = False
    Else    
          Set strRegExp = New RegExp    
          strRegExp.Global= true
          strRegExp.IgnoreCase = false

          strRegExp.Pattern = "[a-zA-Z]"    
          set strFoundChr = strRegExp.Execute(textValue)
               
          strRegExp.Pattern = "[0-9]"
          set strFoundNum = strRegExp.Execute(textValue)

     End If
     
     If strFoundChr.count = 0 And strFoundNum.count = 0 Then
          checkme1 = false
     End If    
         
     If checkme1 = False Then    
          msgbox "nope"
     Else
          msgbox "yup"
     End If
end Function
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 informatio…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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