Hi i have a field that allows only alphabet or numeric in First character, and the rest only allow numeric only, and no allow symbols/space etc.
how to make the jquery/code?
Hi i have a field that allows only alphabet or numeric in First character, and the rest only allow numeric only, and no allow symbols/space etc.
how to make the jquery/code?
Elaborating Jack answer little bit and convert to working snippet.
$('#yourid').on("keyup", function() {
let val = $(this).val();
let reg = /^[a-z0-9]\d*$/
let newval = val.replace(reg, '');
if (!val.match(reg)) {
$(this).val(''); // clear the field if pattern don't match.
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='yourid' />
How above regex work.
^ asserts position at start of the string
Match a single character present in the list below [a-z0-9]
a-z a single character in the range between a and z (case sensitive)
0-9 a single character in the range between 0 and 9 (case sensitive)
\d* matches a digit (equal to [0-9])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)