jQuery - How to get the first character of an input?

-1

I have an input that I want to create custom validations, I need to get its value and validate the first character of the input, what would be the best way to do this?

    
asked by anonymous 27.09.2017 / 21:57

2 answers

3

Take the value of the input and use the charAt function with the position of the character in case 0.

var valor = $('#id_input').val();
alert(valor.charAt(0));
    
27.09.2017 / 22:01
3

Version without jQuery.

Because you do not need jQuery for this.

const input = document.getElementById('input1');
const primeiro = input.value[0];
console.log(primeiro);
<input id="input1" type="text" value="um monte de texto" />
    
27.09.2017 / 22:39