简体   繁体   English

设置来自Ajax的自定义HTML5字段验证消息

[英]Set custom HTML5 field validation message from ajax

How would I implement a custom error message coming from the server to display like one of the default HTML 5 error messages. 我将如何实现来自服务器的自定义错误消息,以显示为默认HTML 5错误消息之一。

I'm currently here. 我现在在这 However the problem is it only displays the message on the second submit and won't change even if it get the okay from the server(since the submit event will only be fired if it validest correct) 但是问题是它只在第二个提交上显示消息,即使它从服务器上没问题也不会更改(因为只有在最正确的情况下才会触发Submit事件)

putRequest.onreadystatechange = function() {
    if (putRequest.readyState == XMLHttpRequest.DONE) {
        if (putRequest.status == 200) {

            var json = eval("(" + putRequest.responseText + ")");

            if (!json.success) {
                submitInput.setCustomValidity(json.message);
                submitInput.focus();
                return;
            }else submitInput.setCustomValidity('');

            // go on

        } else {
            alert('An API error occurred');
        }
    }
}

submitNew.addEventListener('submit', function(e) {
    e.preventDefault();

    putRequest.open("GET", "index.php?add&inputURL=" + submitInput.value, true);
    putRequest.send();

});

The problem is that at the point where the submit event is fired, the form has already been validated . 问题在于,在触发提交事件的那一刻, 表单已经被验证 If you want to do it yourself you have to do it before. 如果您想自己做,则必须先做。

This means chaning the event submit to click on the button 这意味着更改事件submitclick按钮

    putRequest.onreadystatechange = function() {
        if (putRequest.readyState == XMLHttpRequest.DONE) {
            if (putRequest.status == 200) {

                var json = eval("(" + putRequest.responseText + ")");

                if (!json.success) {
                    submitInput.setCustomValidity(json.message);
                    submitInput.focus();

                    return;
                } else submitInput.setCustomValidity('');

                newResultAnswer.innerHTML = json.message;

                newCreateView.style.display = "none";
                newResultView.style.display = "block";

            } else {
                alert('An API error occurred');
            }
        }
    }

    submitForm.addEventListener('submit', function(e) {
            e.preventDefault();
    });

    submitButton.addEventListener('click', function(e) {
            putRequest.open("GET", "index.php?add&inputURL=" + submitInput.value, true);
            putRequest.send();
    });

Remember to also catch the enter key pressed in an input field which also fires the submit. 请记住,还要在输入字段中按回车键,这也会触发提交。 To catch it you'd have to append this. 要捕获它,您必须附加此内容。

    submitInput.addEventListener('keydown', function(e) {
        if(e.keyCode == 13) {
            putRequest.open("GET", "index.php?add&inputURL=" + submitInput.value, true);
            putRequest.send(); 
        }
    });

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

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