Best way to remove punctuation from a string?

Hey,

I was just wondering how I could remove any kind of punctuation mark from a string with the exception of “-” and “_”, if it makes it any easier I need to follow the rules of Javascript variable naming.
http://www.codelifter.com/main/tips/tip_020.shtml

I basically want to prevent the users from typing illegal variable names into a textfield.

Thanks

you could do something simple like this (probably not the best way but it is in my drug filled mind)

dim s as string
...
s.replaceall( ".","" ).replaceall( ",","" ).replaceall( "!","" )

and you can keep chaining the characters you want to remove.

or you could use RegEx to do the removal. Kem would have to give you the code.

You could use the Mask property of the textfield…

[quote=126741:@scott boss]you could do something simple like this (probably not the best way but it is in my drug filled mind)

dim s as string
...
s.replaceall( ".","" ).replaceall( ",","" ).replaceall( "!","" )

and you can keep chaining the characters you want to remove.

or you could use RegEx to do the removal. Kem would have to give you the code.[/quote]
The overhead of RegEx is probably not worth it as I am trying to parse purely the name given rather than a big piece of text. I having written a method to make it quicker for me to replace illegal characters but the ease of using RegEx (because of the large amount of illegal characters) might make it easier.

[quote=126741:@scott boss]you could do something simple like this (probably not the best way but it is in my drug filled mind)

dim s as string
...
s.replaceall( ".","" ).replaceall( ",","" ).replaceall( "!","" )

and you can keep chaining the characters you want to remove.

or you could use RegEx to do the removal. Kem would have to give you the code.[/quote]
Thanks btw

Thanks for the suggestion. I may have a look at this. I have not really used the mask property, as far as memory goes.

It’s not ideal, but you can set the entry to alphanumeric only, for instance. So it would effectively prevent entry of any illegal character.

What makes it ‘not ideal’. Thanks

create a string with all the invalid characters separated by something, split them up into array and the do a for next loop and do the replaceall() on all those value in the array

Probably easier to trap illegal characters on the keydown event of a text field.

so many options to accomplish the same thing

Mask works like Format. So you can choose for instance to let only alphanumeric values in, and nothing else, but it is less flexible than replaceall or a RegEx. Just try and see if it works for you.

Definitely a good piece of the puzzle, but since it doesn’t help with copy/pasted text, it should definitely be done for data entry and user experience, but in conjunction with cleaning the result later.

I’d use a RegEx.

The code to confirm that it’s a legal name (as far as it can) is this:

static rx as RegEx
if rx is nil then
  rx = new RegEx
  rx.SearchPattern = "(?mi-Us)^[A-Z]$|^[_A-Z][_0-9A-Z]+$"
end if

if rx.Search( input ) is nil then
  // Contains illegal characters
end if

I would use that to validate the input and ask the user to fix it if it doesn’t pass. But if you really want to strip everything except letters, numbers, and underscore (hyphen doesn’t appear to be legal either), then this:

static rx as RegEx
if rx is nil then
  rx = new RegEx
  rx.SearchPattern = "[^0-9A-Z_]"
  rx.ReplacementPattern = ""
  rx.Options.ReplaceAllMatches = true
end if

input = rx.Replace( input )

Both of these examples set up the RegEx as Static to reduce the overhead of using them.

I’d do something more interactive & simple NOT let people type in characters that are not allowed
It makes for a better experience than typing something in, thinking its legal only to finally be told “thats not allowed because this character is not permitted” - so don’t permit the person to type it in

Filter right when you have the keydown event
The list is pretty short

You’d also have to trap Paste, as Tim mentioned.

Sure - but do it actively as things are typed / pasted instead of wait THEN validate it

Anyone still hate web sites that you fill in the form, hit “enter” (or submit or whatever) and THEN they say “You must fill in the XXXX field”

And then wipe all the dat a out so you can try again ?
That kind of block mode validation is infuriating - we can do better - and so we should

so what which event should we use to do the validation for the textfield, textarea on typing or pasting?

KeyDown for typing. TextChange for pasting.

If you’re using keydown, then as well as trapping A-Z and 0-9, you’ll need to allow the arrows (left and right to move through the entered text), plus both delete keys, plus space.

The problems with paste have been described here already, so I go with Tim: if you do want to do some kind of validation, use a combination of both keydown and text change.

If you’re trapping people’s actual names, then it’s really hard; people have some really weird names, with odd capitalisation, accents, apostrophes etc. - e.g. James De’Ath, Sebastian Van Basten Battenberg, O’Ha’i Edwards, etc, so this might be a problem which isn’t worth throwing too much time at and just putting in some basic validation (does it contain no numbers and at least one space?).