JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" id="foo" value="" />
<input type="button" onclick="testreg()" value='test' />
JavaScript
function testreg(){
chaine=document.getElementById('foo').value;
alert( chaine.match(/^[0-9]{8}$|^$/)?"ok":"KO")
}