Reputation: 1819
I have a regular expression I'm using to autoformat a date field in a text box. I am replacing every non digit value the user attempts to enter with an empty string. How can I restrict them from entering non digit characters BESIDES the slash character ( / )?
i.e. Users should be able to enter in any digit and a slash /
var val = this.value.replace(/\D/g, '');
val = val.replace(/^([\d]{2})([\d]{2})([\d]{4})$/, "$1/$2/$3");
Upvotes: 1
Views: 2348
Reputation: 30985
You can use a negated regex character class like this:
[^\d\/]
Quoting your code you code use:
var val = this.value.replace(/[^\d\/]/g, '');
^^^^^^^ ---- Replace everything but digits and /
Upvotes: 2
Reputation: 1074
If you wish to replace everything but a digit [0-9]
or a slash [\/]
then should use something like [^0-9\/]
or if you prefer [^\d\/]
Upvotes: 1