StealthRT
StealthRT

Reputation: 10542

RegEX for numbers only and also mask format for money

I am looking for RegEX for numbers only and also for a money format ($xxx.xx)

I currently use this peice of code for numbers and letters and it works just fine:

function validateForOnlyLettersAndNums(txt) 
{ txt.value = txt.value.replace(/[^a-zA-Z 0-9\n\r]+/g, ''); }

However, when i looked around the web for what i am asking here, i only find stuff like this:

(?<=\s|^)-?[0-9]+(?:\.[0-9]+)?(?=\s|$)

And it doesn't seem to work when i use that in the code here:

function validateForOnlyNums(txt)
{ txt.value = txt.value.replace(?<=\s|^)-?[0-9]+(?:\.[0-9]+)?(?=\s|$, ''); }

Upvotes: 0

Views: 3006

Answers (2)

victorvartan
victorvartan

Reputation: 1002

Try:

function validateForOnlyNums(txt)
{
    txt.value = txt.value.replace(/[^$0-9\.]/g, '');
}

it should replace anything unlike $xxx.xx where x is a digit with an empty string. The only drawback with this one is that it will also accept input like $12..3.5$7.

Upvotes: 0

ccbunney
ccbunney

Reputation: 2762

You have not surrounded your 2nd regex in forward slashes, i.e:

function validateForOnlyNums(txt)
{ txt.value = txt.value.replace(/?<=\s|^)-?[0-9]+(?:\.[0-9]+)?(?=\s|$/, ''); }

Upvotes: 1

Related Questions