I have a text input and I am trying to restrict it based on the lang
attribute. If the lang
is set to fr
I want to only allow numbers and a single comma in the input and if it's set to en
I want to only allow numbers and a single period in the input. I've got it working for fr
so far, so I'm basically just wondering what the replace
pattern would be for en
.
function formatNum(lang, $this, val) {
let pattern = '';
if (lang === 'fr') {
// allow only numbers and a single comma
pattern = $this.val().replace(/[^0-9,]/g, '').replace(/(,.*?),(.*,)?/, '$1');
} else {
// allow only numbers and a single period
}
if (pattern !== $this.val()) $this.val(pattern);
}
$(function() {
$('input').keyup(function() {
let $this = $(this),
lang = $this.parent().attr('lang'),
val = $this.val();
formatNum(lang, $this, val);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div lang="fr">
<h3>French</h3>
<input type="text" />
</div>
<div lang="en">
<h3>English</h3>
<input type="text" />
</div>