Validate form so that it does not take blank spaces

0

I would like to be able to validate an input so that by pressing the "space" key there is no effect on the input.

This is what I have

if ($('#correo').val().trim() == "") {
            mensajeAlert("El campo correo es obligatorio");
            return false;
        }

Currently, if the field is empty, it displays the error message.

    
asked by Arnell Vasquez C 15.02.2018 в 17:27
source

2 answers

1

Try this:

$(function() {
    $('#correo').on('keypress', function(e) {
        if (e.which == 32)
            return false;
    });
});

Here you can see the example Fiddle

Taken from stackoverflow in English .

    
answered by 15.02.2018 / 17:44
source
1

Good afternoon Arnell, that code should work without problems, the only thing I can think of is that you do not have a good link to the jQuery library, yet try it in this other way using native JavaScript (I understand that if you include it in a function that you call when you click on the submit):

if (document.getElementById("correo").value.trim() == "") {
    alert("El campo correo es obligatorio");
    return false;
}

I also recommend that before making sure of the operation you use an alert that is sure that it does not fail and it looks fast and simple.

    
answered by 15.02.2018 в 17:56