Send what is being typed from one input to another in real time

2

Hello, guys! I do not understand JQuery or javascript, only PHP. I am suffering to create an INPUT 1, which in real time, sends what is being typed into an INPUT 2, with no spaces or special characters. I'm currently handling this in PHP. Example:

<form>
  <label>Nome</label>
  <input type="text" name="INPUT1">

  <!-- Recebe os dados do INPUT 1 sem espaços e caracteres especiais-->
  <label>Url</label>
  https://meusite.com.br/<input type="text" name="INPUT2">
</form>

I hope it has been clear and I look forward to community help.

    
asked by anonymous 11.10.2017 / 22:09

3 answers

5

Speaking in a simple way to understand, you can do so.

function trim(str) {
   return str.replace(/[^a-zA-Z0-9]/g, '' )
}

let input = document.getElementById('um')
let input2 = document.getElementById('dois')

input.onkeyup = function(){ 
  input2.value = trim(input.value)
}
<input type="text" id="um" placeholder="Input 1">

<input tyoe="text" id="dois" placeholder="Input 2">
    
11.10.2017 / 22:12
4

You can use regex to swap special characters or spaces for nothing.

let input = document.getElementById('um')
let input2 = document.getElementById('dois')

input.onkeyup = function(){
  let valor = input.value.replace(/[^\w\s]|\s/gi, '');
  input2.value = valor
}
<input type="text" id="um" placeholder="Input 1">

<input type="text" id="dois" placeholder="Input 2">
    
11.10.2017 / 22:16
4

You can do this by using regex to filter special characters

$('#input1').on('change keyup', function () {
  $('#input2').val($(this).val().replace(/[^a-zA-Z0-9]/g, ''));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputid="input1" type="text"/>
<input id="input2" type="text"/>
    
11.10.2017 / 22:20