简体   繁体   English

验证形式中的模态窗口

[英]modal window in validation form

It's a snippet of my code. 这是我的代码的一部分。 I need to alert the successfully submitted form using modal window (plugin). 我需要使用模式窗口(插件)来提醒成功提交的表单。 how can I realise it in my code below ? 如何在下面的代码中实现它? I tried to paste function in { if else } construction but don't know if it's correct. 我试图在{if else}结构中粘贴函数,但不知道它是否正确。 Please, help !!! 请帮忙 !!! I'm a noob in frontend, will be grateful with your support :) 我是前端的菜鸟,感谢您的支持:)

var btn = document.querySelector('.btn');
btn.addEventListener('click', checkForm);
function checkForm() {

    for(i = 0; i < inputs.length; i++) { 

        for(y = 0; y < errorsText.length; y++) {
            if (inputs[i].value == "") {
                inputs[i].style.borderColor = 'red';
                errorsText[i].innerHTML = 'This field can not be empty!';
                errorsText[i].classList.add('error-color');
            } else {
                inputs[i].style.borderColor = 'black';
                errorsText[i].style.display = 'none';
                // alert('The form is successfully submitted!');
                // return false;
            }

            if(!first_name.value.match(nameReg)) {
                error_email.innerHTML = 'Please, enter correct first name !';
                error_email.classList.add('error-color');
            } else {
                error_email.style.display = 'none';
            }
            if(select.value == 0)   {
                error_select.innerHTML = 'Please, select a country !';
                error_select.classList.add('error-color');
            } else {
                error_select.style.display = 'none';
            }

            if( birthDate.value <= today ) {
                error_birthDate.innerHTML = 'Please, pick a correct date';
            }
            if(!male_sex.checked && !female_sex.checked) {
                error_sex.innerHTML = 'Please, choose an appropriate sex!';
                error_sex.classList.add('error-color');
            } else {
                error_sex.style.display = 'none';
            }

            if(!email.value.match(email_exp)) {
                error_email.innerHTML = 'Please, enter correct email !';
                error_email.classList.add('error-color');
            }
        }
    }
}

I guess you just need to use preventDefault 我想你只需要使用preventDefault

 var btn = document.querySelector('.btn'); btn.addEventListener('click', checkForm); // Pass (event) function checkForm(event) { // Your code ... // Prevent the default button click action event.preventDefault(); // Run your alert alert('it works!'); } 
 <!-- test code --> <form id="register" action="#" method="POST"> <div class="input-group"> <input class="form-control" placeholder="value" name="value" type="text"> <span class="input-group-append"> <button type="submit" class="btn btn-light">submit</button> </span> </div> </form> 

Otherwise, please show your full code. 否则,请显示您的完整代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM