View Full Version : Replace...statements
tsbarnes
01-08-2003, 05:18 PM
Does anyone know if there is something buit into Asp that will search a string and check to see if it is a number or a letter and if it is not replace it?
I know of the replace function but wanted to get aroung hard coding every possible scenario!
for example:********************************
My string = "123@#$678()(fgh%$#"
Myvar = Replace(mystring, "other than characters and numbers", "")
Result would be:
MyVar = 123678fgh"
**************************************************
Hope this makes sense!
Thanks,
tsbarnes
arnyinc
01-08-2003, 06:15 PM
First of all I would use this short script to get a listing of all the ascii characters values in ASP
<%
for i=0 to 255
response.write i&": "&CHR(i)&"<br>"&vbcrlf
next
%>
Then use the following script to pluck out the certain ranges that you want to replace
Got this from http://www.asptutorial.info/sscript/Removenonalphanumeric.asp
<%
Thetext="#$%a sa&*dd.,;: ggg"
for i=0 to 31
Thetext=Replace(Thetext,CHR(i),"")
next
for i=33 to 47
Thetext=Replace(Thetext,CHR(i),"")
next
for i=58 to 64
Thetext=Replace(Thetext,CHR(i),"")
next
for i=91 to 96
Thetext=Replace(Thetext,CHR(i),"")
next
for i=123 to 255
Thetext=Replace(Thetext,CHR(i),"")
next
Response.write (Thetext)
%>
aCcodeMonkey
01-08-2003, 06:30 PM
tsbarnes,
You can use the IsNumeric(myString) Function to check for numbers.
This example will look a single character, Say I need a string that looks like WWW##S aka wds12$
Examle:
<%
' Long Version
Dim sMySearch ' String
Dim sMySearch1 ' String
Dim bNumeric ' Boolean
' Create Strings
sMySearch = Request.Form("myFormControl")
sMySearch1 = Mid(sMySearch,4,5)
' Check String value
bNumeric = IsNumeric(sMySearch1) ' Returns True of False
If (bNumeric) Then
Response.write("It's a Number!!!")
End If
'Short Version
If (IsNumeric(Mid(CStr(Request.Form("myFormControl")),4,5))) Then
Response.write("It's a Number!!!")
End If
%>
You can loop thru the MID() function to check each chracter in the string. If it's not what you want, remove it with a "" value.
Hope this helps :cool:
landon11
01-08-2003, 08:54 PM
You could use regular expressions but I'm not sure how to use them in VBScript only javascript but maybe someone else does:
re = \W 'Matches any nonword character. Equivalent to [^A-Za-z0-9_]
x = "*()abc123e+=^"
x = replace(x,re,"")
aCcodeMonkey
01-08-2003, 09:43 PM
VBscript doesn't have a RegExpression function
But ASP.NET does
.NET RegExpression References (http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconregularexpressionslanguageelements.asp)
Name Spaces:
<%@ Import Namespace="system.text.regularexpressions" %>
<%@ Import Namespace="system.web.UI.WebControl" %>
Sample Split Function:
Dim aTemp As String() = sFileName.Split("\")
sFileName = CStr(aTemp(aTemp.Length-1))
Sample Control Validator:
<asp:regularexpressionvalidator ID="valPWD1Reg"
ControlToValidate="txtPWD1"
ValidationExpression=".*[!@#$%^&*+;:].*"
Display="Dynamic"
runat="server"
ErrorMessage="Pasword must contain a "Special Character""
Font-Bold="true"
ForeColor="#FF0000"/>
Common Patterns:
Simple Zip Code:
ValidationExpression="\d{5}"
Complex Zip Code:
ValidationExpression="\d{5}(-\d{4})"
SSN:
ValidationExpression="\d{3}-\d{2}=\d{4}"
URL:
ValidationExpression="http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?"
US Phone Number:
ValidationExpression="(^x\s*[0-9]{5}$)|(^(\([1-9][0-9]{2}\)\s)?[1-9][0-9]{2}-[0-9]{4}(\sx\s*[0-9]{5})?$)"
Email Address:
ValidationExpression="[\w-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+"
Hope this helps :cool:
whammy
01-08-2003, 11:36 PM
The above answers are good tries, but not quite the answer tsbarnes was looking for - although landon11 was spot on in his idea, if not implemenation. ;)
Actually, VBScript DOES use Regular Expressions, not sure since which version, but they've always worked for me in the year and a half I've been doing ASP.
This is the script I (already) use for your EXACT criteria... pretty short.
Function ExtractAlphaNumeric(byVal str)
If IsNull(str) Then str = ""
Dim eanRegEx
Set eanRegEx = New RegExp
eanRegEx.Pattern = "[^a-zA-Z0-9]"
eanRegEx.Global = True
ExtractAlphaNumeric = eanRegEx.Replace(str,"")
End Function
Example usage:
mystring = ExtractAlphaNumeric(mystring)
P.S. For many more such useful scripts (including the related IsAlphaNumeric() function), you might want to look at my functions.asp:
http://www.solidscripts.com/displayscript.asp?sid=10
:)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.