JAVASCRIPT

bootstrap 5 自带form的validation功能。 首先要实现验证的前提是设置input属性为required。通过添加form两个class来实现自带的验证,一个是needs-validation 还有一个是was-validated。第一个是提交后验证,第一个是提交前验证。任何一个class布置到form的class里,同时要添加novalidated属性,这个属性可以避免html5自带的required验证,代码如下: <form class="form-horizontal was-validated" novalidate id="domainForm"> 如果是要实现submit提交后的验证,也就是needs-validation,要添加一段js到文件: (() => { 'use strict' // Fetch all the forms we want to apply custom Bootstrap validation styles to const forms = document.querySelectorAll('.needs-validation') // Loop over them and prevent submission Array.from(forms).forEach(form => { form.addEventListener('submit', event => { if (!form.checkValidity()) { event.preventDefault() event.stopPropagation() } form.classList.add('was-validated') …

window.location Cheatsheet #codenewbie#beginners#webdev#javascript Looking for a site’s URL information, then the window.location object is for you! Use its properties to get information on the current page address or use its methods to do some page redirect or refresh 💫 https://www.samanthaming.com/tidbits/?filter=JS#2 window.location.origin → 'https://www.samanthaming.com' .protocol → 'https:' .host → 'www.samanthaming.com' .hostname → 'www.samanthaming.com' .port → '' .pathname → …

Top