【发布时间】:2021-02-04 04:41:58
【问题描述】:
我需要根据我的要求自定义引导表单验证。这是我的表单的 HTML。
var form = document.getElementById('sellingForm');
form.addEventListener('submit', function(event) {
const contact = document.getElementById('contact');
if (form.checkValidity() === false) {
if (isNaN(contact.value) || contact.value > 10) {
event.preventDefault();
event.stopPropagation();
}
}
form.classList.add('was-validated');
}, false)
<form class="main-form bg-white needs-validation" novalidate action="submit-sell.php" enctype="multipart/form-data" method="POST" id="sellingForm">
<input name="ProductName" type="text" class="form-control" id="name" placeholder="Samsung mobile phone" required>
<div class="invalid-feedback">
Please provide a valid title
</div>
<input name="contactNumber" type="tel" maxlength="10" class="form-control" id="contact" placeholder="0777123456" required>
<div class="invalid-feedback">
Please provide a valid contact no
</div>
<input name="password" type="password" class="form-control" id="password" required>
<div class="invalid-feedback">
Please provide a valid password
</div>
<input name="repeatPassword" type="password" class="form-control" id="repeatPassword" required>
<div class="invalid-feedback">
Passwords did not match
</div>
<select class="form-control" id="category" onchange="selectCategory(event)" name="category" required>
<option value="" selected disabled>Select a category</option>
<option value="sell_vehicle">Sell Vehicle</option>
<option value="computer">Computers</option>
<option value="mobile">Mobile Phones</option>
<!--Further options-->
</select>
<div class="invalid-feedback">
Please select a category
</div>
<input required name="price" type="text" class="form-control" id="price" placeholder="120 000" required>
<div class="invalid-feedback">
Please provide a valid price
</div>
<input required class="form-check-input" type="radio" name="negotiablity" id="negotiable" value="1">
<label class="form-check-label">Negotiable </label>
<input required class="form-check-input" type="radio" name="negotiablity" id="not_negotiable" value="0">
<label class="form-check-label">Not Negotiable</label>
<textarea required name="description" rows="5" class="form-control" aria-label="With textarea" placeholder="Enter the product description here"></textarea>
<div class="invalid-feedback">
Please provide a valid description
</div>
<input type="file" name="image" class="custom-file-input" id="file-input" required multiple name="picture">
<div class="invalid-feedback">
Please select a valid image
</div>
<button class="btn btn-primary" type="submit">Post Free Ad</button>
</form>
我已尝试自定义文档中给出的引导 JS 代码。
var form = document.getElementById('sellingForm');
form.addEventListener('submit', function(event) {
const contact = document.getElementById('contact');
if (form.checkValidity() === false) {
if (isNaN(contact.value) || contact.value > 10) {
event.preventDefault();
event.stopPropagation();
}
}
form.classList.add('was-validated');
}, false)
但它似乎不起作用。表单在未经验证的情况下提交。我需要帮助来验证以下内容,然后仅当用户输入有效时才提交表单。
- 用户已填写所有必需的输入。
- 产品名称不得超过 25 个字符,也不得少于 10 个字符。
- 联系电话不得为数字,不得多于或少于 10 个数字。
- 我应该检查输入的密码和重复的密码是否匹配
- 我应该检查所选文件是否为图像(jpeg/png 等格式),以及是否插入了最少一个和最多三个图像。 这些是我需要的主要解决方案,因此我可以应用于其他领域。 请不要使用 Jquery ..
【问题讨论】:
-
您的输入没有限制,这意味着它们不是
required或具有检查值是否有效的pattern属性,例如电话号码。没有它form.checkValidity()check 将永远为真。 -
@ChrisG 好的。我想我已将示例修改为最小
-
只需添加 event.preventDefault();在 const 联系人声明上方@RifkyNiyas
-
@EmielZuurbier 好的。我添加了约束并且工作正常。如何检查密码和重复密码是否匹配
标签: javascript html forms validation bootstrap-4