0

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?

rzl21
  • 43
  • 1
  • 6
  • Create a javascript file, and reference the file so that it interacts with your HTML and add the code there. – Dvid Silva Jul 10 '19 at 04:50

2 Answers2

0

Very simple regex:

/^[a-z0-9]\d*$/
Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
0

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)

Community
  • 1
  • 1
Shree
  • 18,997
  • 28
  • 86
  • 133