Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

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
0
turbosig
Asked:
turbosig
  • 3
  • 2
  • 2
1 Solution
 
mgfranzCommented:
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
 
kyo3ebCommented:
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
 
mgfranzCommented:
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!

 
turbosigAuthor Commented:
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
 
kyo3ebCommented:
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
 
mgfranzCommented:
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
 
turbosigAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now