HTML5表单验证仅在我提交表单时有效吗?

And*_*ili 2 html forms jquery html5

我是HTML5的新手,我有以下疑问.

在页面中我有这种情况:

<div id="inserimentoVariazioneAnticipo" class="row">
    <div class="col-md-5">
        <p style="line-height: 200%;">Inserire la variazione dell'anticipo:</p>
    </div>
    <div class="col-md-7">
        <input id="variazioneAnticipo" class="rightAlligned form-control" style="width:50%" type="text" type="number" step="0.01" />
    </div>
</div>

<button id="confermaAnnullaTrasmissione" type="button" class="btn btn-primary">
    Conferma
</button>
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到我有一个inputtype="number" step="0.01"设置的标签,因为它必须采用十进制数字.

正如您所看到的,我没有<form>标签,因为我不必提交表单,但是当confermaAnnullaTrasmissione单击按钮时我必须执行jQuery功能.

问题是,这样做,在我看来,HTML5类型验证不起作用,因为我可以在我的输入标记中插入任何类型的值.是我还是错过了什么?验证仅在提交表单时有效吗?

如何在不提交任何表格的情况下解决此问题并验证我的输入?

Mar*_*ude 5

两件事情:

首先,type="text"从您的输入中删除.你正在混合type="text"type="number".

其次,您可以使用HTML5 Validation API验证所有表单或单个表单元素.

 document.getElementById('variazioneAnticipo').checkValidity(); 
Run Code Online (Sandbox Code Playgroud)

如果验证成功与否,该行将返回.

查看有关验证API的这篇有趣文章:

http://www.html5rocks.com/en/tutorials/forms/constraintvalidation