简体   繁体   English

我的表单没有提交是有原因的吗?

[英]Is there a reason why my form isn't submitting?

I want the form to submit when there are no errors and this is the code I've used to ensure that.我希望表单在没有错误时提交,这是我用来确保这一点的代码。

const Invalid = document.getElementsByClassName('form-control error')
if (!Invalid) {
    form.submit()
}

This is the rest of the javascript code for reference.这是javascript代码的rest供参考。 Whenever there is an error the class name for that input changes to form-control error, so by making sure there are no class names of form-control error I verify there are no errors and the form should submit, but that doesn't seem to be the case here.每当出现错误时,该输入的 class 名称都会更改为表单控件错误,因此通过确保没有 class 表单控件名称错误,我验证没有错误并且表单应该提交,但这似乎没有在这里就是这样。

const Form = document.getElementById('form');
const Username = document.getElementById('username');
const Password = document.getElementById('password');
const Conpassword = document.getElementById('confirm_password');
const Email = document.getElementById('email');


Form.addEventListener('submit', (e) => {
    e.preventDefault();
    checkInputs();

});


function checkInputs() {
    const UsernameValue = username.value.trim();
    const PasswordValue = password.value.trim();
    const ConpasswordValue = confirm_password.value.trim();
    const EmailValue = email.value.trim();

    if (UsernameValue === '' || UsernameValue == null) {
        setErrorFor(Username, 'Username required');
    } else if (!validateUsername(UsernameValue)) {
        setErrorFor(Username, 'Only alphanumeric characters valid')
    } else {
        setSuccessFor(Username);
    }

    if (EmailValue === '' || EmailValue == null) {
        setErrorFor(Email, 'Email required');
    } else if (!isEmail(EmailValue)) {
        setErrorFor(Email, 'Email is not valid');
    } else {
        setSuccessFor(Email);
    }

    if (PasswordValue === '' || PasswordValue == null) {
        setErrorFor(Password, 'Password required');
    } else {
        setSuccessFor(Password);
    }

    if (ConpasswordValue === '' || ConpasswordValue == null) {
        setErrorFor(Conpassword, 'Confirm password');
    } else if (PasswordValue !== ConpasswordValue) {
        setErrorFor(Conpassword, "Passwords don't match");
    } else {
        setSuccessFor(Conpassword)
    }

    if (PasswordValue.length < 8) {
        setErrorFor(Password, 'Password needs to be at least 8 characters')
    } else {
        setSuccessFor(Password)
    }

    const Invalid = document.getElementsByClassName('form-control error')
    if (!Invalid) {
        form.submit()
    }

}

function setErrorFor(input, message) {
    const FormControl = input.parentElement;
    const Small = FormControl.querySelector('small');

    Small.innerText = message;

    FormControl.className = 'form-control error';
}

function setSuccessFor(input) {
    const FormControl = input.parentElement;
    FormControl.className = 'form-control success'
}

function isEmail(Email) {
    return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9].{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z].{2,}))$/.test(Email);
}

function validateUsername(Username) {
    return /^[a-zA-Z0-9_]*$/.test(Username)
}

document.getElementsByClassName returns an Object... specifically an HTMLCollection document.getElementsByClassName返回一个 Object... 特别是一个 HTMLCollection

Objects are "truthy"... so !object is always false对象是“真实的”...所以 !object 总是假的

so, in your code Invalid is always "truthy"... so !Invalid is always false - try所以,在你的代码中Invalid总是“真实的”......所以!Invalid总是false - 试试

if (!Invalid.length)

since that will be true if Invalid.length is zero - which is what you're after因为如果 Invalid.length 为零,那将是真的 - 这就是你所追求的

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

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