Mobile Form

0

I would like to know how I do a form validation so that I do not leave the fields blank on my cell phones, I already did several modes, the pc works but the cell phone did not work anyway, how would I do it?

<script language="JavaScript">
    function ValidaSemPreenchimento(form) {
        for (i = 0; i < form.length; i++) {
            var obg = form[i].obrigatorio;
            if (obg == 1) {
                if (form[i].value == "") {
                    var nome = form[i].name
                    alert("Os campos chave é obrigatório.")
                    form[i].focus();
                    return false
                }
            }
        }
        return true
    }

</script>

<form name="form" id="form" action="send.php" method="post" onSubmit="return ValidaSemPreenchimento(this)">
    
asked by anonymous 29.06.2016 / 01:07

2 answers

1

Why do not you use jQuery? It will be simpler not to let the input be blank if the user tries to send the form with white fields.

You can use this simple jQuery plugin .

jQuery:

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            field1: {
                required: true,
                email: true
            },
            field2: {
                required: true,
                minlength: 5
            }
        }
    });

});

HTML:

<form id="myform">
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    <input type="submit" />
</form>

Example working with this code: link

Other options: link

Plugins: link

    
29.06.2016 / 01:55
3

This does not work here and causes your code to fail:

var obg = form[i].obrigatorio;

As obrigatorio is not a default HTML attribute, there is no automatic property of the same name in the object. You need to use getAttribute , and if you want your HTML to validate (like HTML5) , you must put the data- prefix in the attribute name, like this:

<input name="bla" data-obrigatorio="1">

Then you can get the value like this:

obg = form[i].getAttribute('data-obrigatorio');

The full function would look like:

function ValidaSemPreenchimento(form) {
    var i, obg, nome;
    for (i = 0; i < form.length; i++) {
        obg = form[i].getAttribute('data-obrigatorio'):
        if (obg == 1) {
            if (form[i].value == "") {
                nome = form[i].name;
                alert("O campo " + nome + " é obrigatório.")
                form[i].focus();
                return false;
            }
        }
    }
    return true;
}
    
29.06.2016 / 03:14