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

How do I check for certain characters and removing spaces

String Manipulation

How do I check for certain characters and removing spaces

by  onpnt  Posted    (Edited  )
just another validation for reading form values.
I see a lot of questions beiong asked regarding these two methods in VBscript.
Checking for characters and getting rid of spaces.
The script is fairly easy to chagne to only perform one or the either or leave as is for both.
I gathered this code from others and myself to create the shortest easiest one I could come up with.
Happy Coding!

<HTML>
<HEAD><TITLE>Simple Validation</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Validate()
Dim Illchars, charcnt, banned, valid, inputvalue
' add characters to the Illchars var within the quotes
Illchars = "'^\/()#~<>|`¼:;!@$*+=%"
valid = True
inputvalue = form1.Text1.value

'check for any value
if inputvalue = "" or inputvalue = null then
alert "please make a entry for submission", vbOK
exit sub
end if

' trim the spaces left and right
inputvalue = Trim(Replace(inputvalue, " ", ""))

For charcnt = 1 to Len(Illchars)
banned = Mid(Illchars, charcnt, 1)
If InStr(inputvalue, banned) > 0 Then
valid = False
Exit For
End If
Next

If Not valid Then
Str = ""
MyVar = MsgBox ("You have entered characters not valid. Please try again!")

Exit Sub
End If

Dim TheForm, MyVar
TheForm=form1.Text1.value
'replace spaces...
Do While InStr(1, TheForm, " ")
TheForm = Replace(TheForm, " ", "")
Loop
MyVar = MsgBox ("Here it is without spaces, as long as you made it this far" & TheForm)
End Sub

-->
</SCRIPT>
</HEAD>
<BODY>
<H3>Simple Validation</H3><HR>
<form Name="form1">

<input TYPE="TEXT" name="Text1" SIZE="30">
<input TYPE="button" name="Submit" VALUE="Submit" onClick="Validate()">
</form>
</BODY>
</HTML>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top