Form for mobile phones

Asked

Viewed 271 times

0

I would like to know how I do a form validation so that do not leave the fields blank on mobile, I have done several modes, pc works but mobile did not work in any way, as I would 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)">
  • Can you show the code you’re using? Set also the default number you want to validate.

  • this one, edited the post !

  • What is this property .obrigatorio? Is a id or name? where is the HTML(s) form(s)?

  • the form is down friend, the point is I do not want to leave blank, do not send the email or pass page if you have any blank input from that page!

  • Do not use stacksnippets unnecessarily, they are not to format but to execute js+css+html codes that do something, if not to do anything use normal markup. Read HELP (http://answall.com/help/formatting) and learn to follow what is said on it, don’t kick things around without being sure how they work. Take it as a constructive criticism.

  • @Leonardoribeiro adds more information to the question, as it is I still have doubts about what you want.

Show 1 more comment

2 answers

3

This doesn’t work and makes your code fail:

var obg = form[i].obrigatorio;

Like obrigatorio is not a standard 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 (such as HTML5), you need to prefix it data- in the attribute name, thus:

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

And then you can take the amount like this:

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

The full function would be:

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;
}
  • I think your answer is good and valid, but I’m still wondering if that .obrigatorio will not be name="obrigatorio" input... https://jsfiddle.net/uaht4r3L/

  • 1

    @Sergio Context tells me it’s an attribute, but I could be wrong.

1


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

You can use this simple plugin jQuery.

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 : http://jsfiddle.net/xs5vrrso/

Other options : https://jqueryvalidation.org/validate

Plugins : https://jqueryvalidation.org/category/plugin/

  • 1

    Using jQuery just to "don’t let the input go blank" is like killing a fly with a cannonball :)

  • @Sergio Sim I know that using jQuery for this simple problem is not necessary,but I thought it’s a possible solution and it will work properly,but I know you’re right in this,hahaha.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.