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

Filtering text

Status
Not open for further replies.

Techwarrior

Programmer
Apr 19, 2006
14
US
I am trying to filter the text that gets entered in a textbox that I am using as an input into a table. I have users who sometimes enter GL19302 and others who will only enter 19302. I know their is a way of removing specific letters from a text string because I have used it before, but right now I can't remember how.
 
you can use Left(), right(), Mid() functions to do this...

myfield = 'GL19302'

then Mid(myfield, 3, len(myfield)

gives 19302

-DNG
 
The problem is not everyone enters the letters at the begining. I need a way to filter them out if they are there.
 
Missed the closing paren.
then Mid(myfield, 3, len(myfield))

You can also filter using regular expressions but how you do it depends on how you want the data to present.
Would you be stripping all text characters to leave only the numbers? Or just making sure the string does not begin with GL or something similar?


It's hard to think outside the box when I'm trapped in a cubicle.
 
I am fairly new to ASP programming and from what I can tell about Regular Expressions is that it will only allow the user to enter specific characters. What happens if they enter a character that is not allowed.
 
No, regular expressions can do a heck of a lot depending on how you set them up. Here is a quick and dirty routine to strip all non-numerical characters and print out what is left.
Code:
<%
' Setting .Pattern to "\D" will strip all characters except numbers.
' Setting .Pattern to "[^a-zA-Z]" will stip all characters except upper and lowercase letters.

myval = "ABC345(*6" 


dim objRegEx 
Set objRegEx = New RegExp
  With objRegEx
    .Pattern = "\D"
    .Global = True
    StripEx = .Replace(myval, "")
  End With
set objRegEx = nothing

response.write stripEx

%>


It's hard to think outside the box when I'm trapped in a cubicle.
 
There is a very similar script to the one I posted above on this web site:

It is setup to allow you to pass in the pattern you are looking to match (like "\D" for numeric, "[^a-zA-Z]" for alpha, etc). And they have some good info on using Regular Expressions with VBScript.

NOTE: From what I have read VBScript does not directly support regular expressions and has to have a link to the component set on the system. This may only be true when using VBScript in applications and not with ASP though so give my simple script a try and if it outputs just the numbers then you know you have support and are good to go.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Both of these ideas are good, but I distinctly remember there was a very simple function where you could filter out specific characters out of a text string.
 
You could loop through every character of the string and check if it's ascii value is in the range of a number and if not remove it but it is not really a simpler function and it is quite a bit slower than a regular expression.



It's hard to think outside the box when I'm trapped in a cubicle.
 
In Javascript you can do construct a regular expression on a single line including all parameters and then use one line to run the expression against your value but in VBScript you have to first create the object for the expression then assign the parameters for the object including the pattern, whether it is globally applied to the string and if it should ignore case so it looks like longer code but the function does the same thing.

The function shown on the link above is constructs a regular expression by passing in the parameters and then tests the data and returns a value. This way you only have the one small function and you can call it for many different types of tests instead of having to write a function for each test so it works out better in the long run.
You may not only want to strip the extra characters. You might want to also make certain that the remaining string is not blank and of a specific number or range of digits so you would perform another regular expression against the value once it has been stripped of alpha characters.

Sure, you could write one function to do all of that for that one field but then you would be writing a custom function that is not easily re-used and for all the other fields you have to test you would have to write more functions. With a single function that accepts the pattern to match all you have to do is call that function for each type of test. You would quickly end up with a range of tests that cover all the types of tests you will ever need to do and you can just drop that code into every form you work on in the future without worrying about writing new validation code every time.

It's hard to think outside the box when I'm trapped in a cubicle.
 
I ended up using the Trim function.

Here are the possible entries that can be put into my textbox: Gl13250, gl13250, gL13250, 13250

So I used the following code:

mytextbox.text = mytextbox.text.trim("g","G","l","L")

Thanks for everyones help.
 
And they could not type in any other characters?
What if they typed in xx99999? If they CAN put in something else then they will.



It's hard to think outside the box when I'm trapped in a cubicle.
 
If they put in something else that doesn't match what's in the database then they will receive an error message. I just needed to have something so that if they put in the letters before the numbers then it would eliminate them. The only letters that would ever be entered before the numbers are "g" and "l" if they entered anything else then they definitely not be entering in the right information. Sorry I did give you all the facts earlier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top