PonyTricks
PonyTricks

Reputation: 1798

ASP Classic: Check if string only consists of valid chars

I've been checking all over the internet but really can't find any specific solution of my problem.

How do I check if a string consists of only the declared valid characters?

I want my string to consists of only 0-9, A-Z and a-z

So the string oifrmf9RWGEWRG3oi4m3ofm3mklwef-qæw should be invalid because of - and æ while the string joidsamfoiWRGWRGmoi34m3f should be valid.

I have been using the build-in RegExp to strip the strings, but is it possible to just make it check and return a boolean false or true?

my regexp:

set pw = new regexp
pw.global = true
pw.pattern = "[^a-zA-Z0-9]"

newstring = pw.replace("iownfiwefnoi3w4mtl3.-34ø'3", "")

Thanks :)

Upvotes: 8

Views: 13411

Answers (5)

ipr101
ipr101

Reputation: 24236

Try -

Dim myRegExp, FoundMatch
Set myRegExp = New RegExp
myRegExp.Pattern = "[^a-zA-Z0-9]"
FoundMatch = myRegExp.Test("iownfiwefnoi3w4mtl3.-34ø'3")

If FoundMatch is true the RegEx engine has found a character that is not a-z or A-Z or 0-9 and your string is not valid.

Upvotes: 3

Frank Tudor
Frank Tudor

Reputation: 4524

[a-zA-Z0-9] works...I tried it against your string here http://gskinner.com/RegExr/?2u7c3 and here http://regexpal.com/ ...take the carrot out. I also can't remember the regex engine vbscript uses but that might have something to do with your problem. This also works...

\D?\w

Upvotes: 0

Neil Thompson
Neil Thompson

Reputation: 6425

Rather than replace you can look and see if there is a match on any characters outside the whitelist. The general for each match syntax is here

Upvotes: 0

Doozer Blake
Doozer Blake

Reputation: 7797

You could do a Test which returns True or False

If( pw.Test("string") ) Then
'' Do something
End If

Upvotes: 10

John T
John T

Reputation: 2677

You could do something like:

Set match = pw.execute("iownfiwefnoi3w4mtl3.-34ø'3")

if match.count > 0 then
   ' your pattern matched, so it's invalid
   badString = true
else
   badString = false
end if

Upvotes: 0

Related Questions