Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking string for certain characters

Status
Not open for further replies.

jmurrayhead

Programmer
Feb 13, 2004
47
US
I want to be able to check strings for invalid characters for my login system. Let's say I have the string
Passcode = request.form("Password")

How would I be able to check for / ? ; : ' " . , etc...?

Thanks
 
use instr or regex

example :
if instr(password,"?") > 0 then blah


[thumbsup2]DreX
aKa - Robert
 
You could also do it client side with something like:

<SCRIPT LANGUAGE="JavaScript"><!--
function validate(string) {
if (!string) return false;
var Chars = "0123456789-";

for (var i = 0; i < string.length; i++) {
if (Chars.indexOf(string.charAt(i)) == -1)
return false;
}
return true;
}
//--></SCRIPT>

<FORM>
<INPUT TYPE="TEXT" SIZE="12" MAXLENGTH="12" onChange="if (!validate(this.value)) alert('Not Valid')">
</FORM>
 
A regular expression would still be more eficient then either of the previous answers. Off the top of my head you could do something like:
Code:
Dim regex
Set regex = New RegExp
regex.pattern = "^[^/?;:'"\.,]+$"

If Not regex.Execute(yourString) Then
   'they have an invalid character in there
Else
   'everything is great
End If

Set regex = Nothing

Basically that pattern says:
^ - Match the beginning of the string
[] - Match any in this Group of characters
[^] - Match any char except in this group of characters
+ - match one or more times
$ - match end of string

Or in english, match the whole string (everything between beginning and end) and one or more characters not in the set /?;:'".,

This also means you get a len > 0 check builtin since we used the + for one or more characters. If the Exexcute comes back true than it is valid because none of those characters were in out string, if it comes back false then it is invalid because it found one of those characters in the string (or the string was 0 length).

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Need an expensive ASP developer in the North Carolina area? Feel free to let me know.


 
Tarwn, I agree that regex would be the way to go. I made some changes to your code and here is the result:
Code:
Dim regex, yourString
yourString = "Hello"
Set regex = New RegExp
regex.pattern = "^[^/?;:'\.," & Chr(34) & "]+$"

If Not regex.Test(yourString) Then
   WScript.Echo yourString & " has invalid chars"
Else
   WScript.Echo yourString & " is ok"
End If
yourString = "hell""" & """o"
If Not regex.Test(yourString) Then
   WScript.Echo yourString & " has invalid chars"
Else
   WScript.Echo yourString & " is ok"
End If
Set regex = Nothing

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Doh, should've noticed that lack of escape on the double-quote, I must be getting forgetful in my old age ;)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
Need an expensive ASP developer in the North Carolina area? Feel free to let me know.


 
Happens to the best of us. Even the mighty T. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
wont this also work???

regex.pattern = "[^/?;:'\.," & Chr(34) & "]"


Known is handfull, Unknown is worldfull
 
Thanks everyone...that was more of what I was looking for. Does the job perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top