Strip white spaces on input

Viewed 44975

I have a field that does not need any white spaces. I need to remove any as they are entered. Here's what I'm trying... no luck so far

$('#noSpacesField').click(function() {
    $(this).val().replace(/ /g,'');
});
4 Answers

We can achieve the desired outcome with pure javascript.

Input

<input type="text" id="whiteSP" onChange={ (e)=>removeSpace(e) } />

Js Function

function removeSpace(e){
    let val = (e.target.value).trim();
    console.log(val);
}

However on a form submit button clicked

console.log( ( (" test case ").trim() ).replace(" ", "") )
Related