Barry Hamilton Barry Hamilton - 1 year ago 104
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, ""));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download