Barry Hamilton Barry Hamilton - 11 months ago 69
Javascript Question

jquery regex replace non numeric characters but allow plus

$('#target').val($('#target').val().replace(/[^\d]/g, ""));

I use the above code to leave only numeric characters in an input value I would also like to allow '+' and '-'.

How would I modify the regex to allow this?

Help much appreciated

Answer Source

Put - and + in the character class.

$('#target').val($('#target').val().replace(/[^-+\d]/g, ""));