JSFiddle - React, Tailwind, and code Playground

by James Anderson

HTML

<p>You can't put spaces at the start:</p>
<input type="text" id="noSpaces">

JavaScript

function noPrecedingSpace ( eID )
{

    var elmt = document.getElementById(eID);

    elmt.addEventListener("keydown", function(event)
    { 
        var strg = elmt.value;
        var lastChar = strg.charAt(strg.length - 1);

        if ((lastChar == " ")||(lastChar == "&nbsp;")||(strg == ""))
        {
            return event.which !== 32;
        };
    }); 

};
noPrecedingSpace ("noSpaces");