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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for invalid characters in a string

Status
Not open for further replies.

VB400

Programmer
Sep 8, 1999
359
0
0
US
<br>I need to check a string field to make sure that invalid characters (e.g. special characters ~!@#$%^, etc.) are not allowed.<br><br>What's the fastest way to do this?<br><br>I can use instr() function to check for each one of the invalid characters but I think that this would be very slow.<br><br>Thanks in advance!
 
The best way I know of is by using the instr() function.<br>It's not as bad as you think.&nbsp;&nbsp;We use it here to look for single quotes in any database string.&nbsp;&nbsp;SQL Server does not like single quotes.<br>If you like, I already have a working function.<br>Gimme your e-mail address & I'll send it to ya.<br><br>Gary
 
<br>Thanks Gary,<br><br>I'm already using instr() -- great minds think alike :)<br><br>But seriously folks, the instr() function doesn't perform badly but since I'm checking for a bunch of invalid characters I use a series of instr() commands.&nbsp;&nbsp;It still performs nicely; however, if there is a better way of checking for multiple characters then I would like to learn it.<br><br>Thanks!
 
You may want to use one Instr() funtion in a loop, utilizing the Chr(i) to identify the list of invalid characters to search for. I think it will be a bit more efficient than using a series of Instr() functions. It sounds like you already have it down, but &quot;a series of instr() commands&quot; just caught my eye.
 
if you want to use just charecters.. a-z, you can check the ascii code of what they entered in the text_change event, and fix it there.<br><br>I used seomthin like this.. <br><br>if asc(x) &gt; 32 or asc(x) &lt; 105 then exit sub<br><br>those numbers are made up since I dont remember what they actually are.&nbsp;&nbsp;it wont help in your case, but i felt like postin anwyas
 
I forgot last time but are you using VB6???<br>Try the Replace Function. It returns a string in which a specified substring has been replaced with another substring a specified number of times.<br>I think this would work for you.<br><br>I hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top