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

Replace a-z A-Z and special Characters

Status
Not open for further replies.

wrighterb

Programmer
Joined
Feb 20, 2007
Messages
80
Location
US
I want to replace everything except 0-9 . + -

Everything else gone. This is what I have so far, but its not taking out the a-zA-Z

What am I doing wrong?

function clearChar() {
document.adjustment.adjustAmount.value=filterNum(document.adjustment.adjustAmount.value)
function filterNum(str) {
re = /\$|[a-zA-Z]|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\=|\[|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|/g;
return str.replace(re, "");
}
}
 
Nevermind, it works... must have been cached or something.
 
Why grab everything you want to get rid of when the list of what you want to keep is a lot smaller??

Code:
var re = /[^0-9.+-]/g

**Tested**

[monkey][snake] <.
 
Awesome thanks, less code the better, can I add a limit to the number of + - Signs like so?

function clearChar() {
document.adjustment.adjustAmount.value=filterNum(document.adjustment.adjustAmount.value)
function filterNum(str) {
re = /[^0-9.+{1}-{1}]/g
return str.replace(re, "");
}
 
AFAIK not with the .replace() method.

I don't think it's possible to say replace ALL except one.

You can use the .test() method though to make the user rewrite the expression if there are more (or less) + or - signs than you want.



[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top