【发布时间】:2019-10-25 11:23:08
【问题描述】:
<form>
<div class="form-group">
<label for="add-price">Price
<span class="glyphicon glyphicon-euro"></span>
</label>
<input type="number" class="form-control" id="add-price" value="0" step="any" min="0" name="number" onfocusout="check(this)" v-model="product.std_rte"/>
</div>
<button type="submit" class="btn btn-primary" @click.prevent="createProduct">Create</button>
</form>
上面是问题的 HTML 代码,下面是验证所需的 javascript。
function check(input){
if (input.value == 0 || input.value == ""){
// input.setCustomValidity('The number must not be zero or empty.');
alert("The number must not be zero or empty.");
return false;
}else {
input.setCustomValidity('');
}
return true;
}
代码正在运行以进行输入文本验证,但是在我单击保存按钮后,它允许保存值,当值为零并且为空时它允许它。我想捕获表单,直到正确给出值根据验证,它应该只允许提交正确的表单。
【问题讨论】:
-
@click.prevent读起来就像你在使用 Vue.js。那为什么不加标签呢? -
对不起,我忘了提标签:)
标签: javascript html