How to validate a field in a form

0

I have a form, where I have, for example a name field. Then I have a JavaScript with the following:

function validarNombre() {
        valor = document.getElementById("nombre").value;
        if (valor == null || valor.length == 0 || /^\s+$/.test(valor)) {
            alert('Falta Llenar Nombre');
            return false;
        } else {
            return true;
        }
    }

Now, in real time, how do I call that function? there is an "out of focus" event (as if the user leaves the text box without completing it) Or do I have to call from the submit button?

The problem that I have many fields to validate and how is it handled?

    
asked by MNibor 09.09.2017 в 02:23
source

1 answer

1

You can be guided by this code:

<input type="" onfocusout="validate()" name="">
<script type="text/javascript">
    function validate(){
        alert('here')
    }
</script>
  

If you want to save work you can use jquery and there is a plugin called jquery validate , you will save a lot of work.

    
answered by 09.09.2017 / 02:38
source