gpt4 book ai didi

validation - SweetAlert2 : Validation required for one of the fields

转载 作者:行者123 更新时间:2023-12-03 23:54:33 25 4
gpt4 key购买 nike

我正在尝试对表单中的一个字段执行验证。

只有当该字段的值存在时,我才能调用 API,否则将抛出错误消息。

我尝试了 SweetAlert2 网站上的各种示例。我只想要对其中一个字段的验证。

Swal.fire({
title: 'Are you sure you want to Save the Notes?',
type: 'info',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
}).then((result) => {

console.log('result.value',result.value);
if (result.value) {
Swal.fire( {
title: 'Download Notes',
html:"<div class='b'><p>ID</p></div><input id='swal-input2' class='swal2-input' required/><div class='b'><p>Notes</p></div><input id='swal-input1' class='swal2-input' autofocus minlength='500' >",
confirmButtonText: 'Save',
preConfirm: (login) => {
console.log('document.getElementById(swal-input2).value',document.getElementById('swal-input2').value);
request_string = {
"Request":
[
{
"Col1": "value1",
"Col1": "value2",
"Col1": document.getElementById('swal-input2').value,
"Col1": document.getElementById('swal-input1').value,

}
]
};
fetch('API_URL', {
headers: {
'Accept': 'application/json, text/plain, application/xml, */*',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
},
method: 'POST',
body: JSON.stringify(request_string)
}
).then(response => {
if (response.status !== 200) {

return;
}
response.text().then(data => {

response_data = data;
response_jsonObj = JSON.parse(response_data);

});
}).catch(error => this.setState({ error }));
},
allowOutsideClick: () => !Swal.isLoading()

}).then((result) => {
swal({
title: " Your request is being processed!",
icon: "success",
confirmButtonText: 'OK'
}).then((okay) => {
if (okay) {
history.push('/page1');
history.push('/page2');
}
});
});
}
})

最佳答案

如果您只想确保第一个输入(即 swal-input2 )不为空,那么您只需添加 preConfirm像那样:

    preConfirm: () => {
if (document.getElementById('swal-input2').value) {
// Handle return value
} else {
Swal.showValidationMessage('First input missing')
}
}

您可以找到工作解决方案 here

关于validation - SweetAlert2 : Validation required for one of the fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com