Link to home
Start Free TrialLog in
Avatar of turbosig
turbosig

asked on

vbscript - regular expression

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
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

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>
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>
You can see that my example is to prevent special characters only as well as <space> and <cr>.
Avatar of turbosig
turbosig

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of kyo3eb
kyo3eb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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