1

I have an input field and i am using the following code to prevent users of adding white spaces only at the beginning of the text. How can i make it also so if someone copy and paste text that has white spaces at the first place of the text in the input to remove those spaces?

Here's my code:

$('.locField').on('keydown', function(e) {
  if (e.which === 32 &&  e.target.selectionStart === 0) {
  return false;
  }  
});
.myform { 
background:#eee;
padding:15px;
width:100%;
}
.locField {
width:80%;
padding:12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="myform"><input placeholder="Try to add a space at the beggining..." type="text" class="locField"/> </div>

PS: To make my question more clear, please do not suggest me to use the ".trim" function as i want to remove the white spaces ONLY at the beginning of the text.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Designer
  • 875
  • 7
  • 26
  • duplicate https://stackoverflow.com/questions/360491/how-do-i-strip-white-space-when-grabbing-text-with-jquery?rq=1 – CWill Sep 25 '18 at 19:11
  • 1
    It's not a duplicate. His question is about the `.trim()` js method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim <-- removes whitespace from beginning and end of string – Iskandar Reza Sep 25 '18 at 19:20
  • @I.R.R. exactly. I want to remove white space ONLY from the beggining. – Designer Sep 25 '18 at 19:21
  • 1
    Oh if its ONLY the spaces before the string, assuming `str` is the string, this the code: `str.replace(/^\s+/g, '');`. Here's where you can play with Regex to test: https://www.regextester.com/ . That regex means `match if it begins with one or more whitespace characters, replace with nothing'. – Iskandar Reza Sep 25 '18 at 19:25
  • @I.R.R. thank you for your help. This worked perfect. – Designer Sep 25 '18 at 19:32

3 Answers3

6

ok based on the suggestion of @I. R. R. this is the code that worked for me:

 $('.locField').on('keydown', function(e) {
  if (e.which === 32 &&  e.target.selectionStart === 0) {
  return false;
  }  
});

$('.locField').on("input", function () {
  $(this).val($(this).val().replace(/^\s+/g, ''));
 });
.myform { 
background:#eee;
padding:15px;
width:100%;
}
.locField {
width:80%;
padding:12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="myform"><input placeholder="Try to add a space at the beggining..." type="text" class="locField"/> </div>
Designer
  • 875
  • 7
  • 26
0

Listen to the input event, and then trim the spaces at the beginning with a replace using regex.

const input = document.getElementById('test');

input.addEventListener('input', (event) => {
    input.value = input.value.replace(/^\s*(.*)$/, (wholeString, captureGroup) => captureGroup);
});

Here's a fiddle that demonstrates it

Jack
  • 804
  • 7
  • 18
0
$("input[type='text'],textarea").on("paste", function (e) {
    var $self = $(this);
    setTimeout(function () {
        $self.val(($self.val().trim()));
    }, 200)
});
sukumar2267
  • 130
  • 7