Barry Hamilton Barry Hamilton - 1 month ago 7x
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


Put - and + in the character class.

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