Mobile Browser View Disable/Prevent entering white spaces inside input box using Javascript

The below code only working in web view, but not in mweb browser

onKeyPress={(e) => {
  var startPos = e.currentTarget.selectionStart;
  if (e.which === 32 && startPos == 0) {
      e.preventDefault();
  }
}