JS regex to replace any non numeric char or decimal

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

JS regex to replace any non numeric char or decimal

Post by Burrito »

Code: Select all

<script>
function removeIt(obj)
{
	obj.value = obj.value.replace(/(\D|^\.)/,'');
}
</script>
that gets any non-numeric char (including a decimal point). I need to keep the decimal point.

so:
$53 would change to 53
and:
$53.00 would change to 53.00

etc.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

/[^\d\.]/
or

Code: Select all

/[^0-9\.]/
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

w00t.

thanks
Post Reply