作为编码培训,我现在正在制作一个网页,您可以在其中单击“创建”按钮,这会触发一个弹出窗口,您应该在其中填写6个数据输入,其输入样式如文本,选择等等。(请参见下面的代码和所附图像)
<template>
<v-btn class="create-button" color="yellow" @click="alertDisplay">Create</v-btn>
<br/>
<p>Test result of createCustomer: {{ createdCustomer }}</p>
</div>
</template>
<script>
export default {
data() {
return {
createdCustomer: null
}
},
methods: {
alertDisplay() {
const {value: formValues} = await this.$swal.fire({
title: 'Create private customer',
html: '<input id="swal-input1" class="swal2-input" placeholder="Customer Number">' +
'<select id="swal-input2" class="swal2-input"> <option value="fi_FI">fi_FI</option> <option value="sv_SE">sv_SE</option> </select>'
+
'<input id="swal-input3" class="swal2-input" placeholder="regNo">' +
'<input id="swal-input4" class="swal2-input" placeholder="Address">' +
'<input id="swal-input5" class="swal2-input" placeholder="First Name">' +
'<input id="swal-input6" class="swal2-input" placeholder="Last Name">'
,
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value,
document.getElementById('swal-input3').value,
document.getElementById('swal-input4').value,
document.getElementById('swal-input5').value,
document.getElementById('swal-input6').value
]
}
})
if (formValues) {
this.createdCustomer = this.$swal.fire(JSON.stringify(formValues));
console.log(this.createdCustomer);
}
}
}
}
</script>
从技术上讲,它正在工作。单击“创建”按钮时将显示弹出窗口,您可以填写所有6个空白,然后也单击“确定”按钮。但是我想添加一些功能来检查用户输入是否有效,我的意思是
,依此类推。
如果是C或Java,我可能会做类似
的操作if(length <= 50){
// proceed
} else {
// warn the user that the string is too long
}
,但是当涉及到使用Vue-SweetAlert2在单个弹出窗口中验证多个输入时,我不确定该怎么做,也找不到足够详细说明的页面。
如果只是一个输入,也许您可以像这样使用inputValidor
const {value: ipAddress} = await Swal.fire({
title: 'Enter an IP address',
input: 'text',
inputValue: inputValue,
showCancelButton: true,
inputValidator: (value) => {
if (!value) {
return 'You need to write something!'
}
}
})
if (ipAddress) {
Swal.fire(`Your IP address is ${ipAddress}`)
}
,但是此示例仅涉及“一个输入”。另外,此检查仅是“是否已提供IP地址”(这意味着是否存在值,并且它实际上不会检查IP地址的长度是否正确和/或是否输入字符串由数字/字母组成)。
另一方面,我想做的是“ 限制多个输入值 (例如字符串的长度等)” > 在一个弹出窗口中 ”。知道我应该怎么做吗?
答案 0 :(得分:1)
很遗憾,限制输入(例如,必填,模式等)的HTML标签不起作用(请参阅此issues), 所以我找到了两种解决方法。
在链接的问题中使用preConfirm
您可以使用带有Regex的preConfirm和if / else语句来检查您的要求,如果不满意,可以使用Swal.showValidationMessage(error)
。
const{value:formValue} = await Swal.fire({
title: "Some multiple input",
html:
<input id="swal-input1" class="swal-input1" placeholder="name"> +
<input id="swal-input2" class="swal-input2" placeholder="phone">,
preConfirm: () => {
if($("#swal-input1").val() !== "Joe"){
Swal.showValidationMessage("your name must be Joe");
} else if (!('[0-9]+'.test($("#swal-input2").val())){
Swal.showValidationMessage("your phone must contain some numbers");
}
}
});
使用Bootstrap
通过这种方式Bootstrap在验证时进行检查,您需要在输入类中包括class="form-control"
并稍微更改html代码。
如果某些条件失败,浏览器将按照html代码中的顺序显示每个字段的验证消息。
const {value: formValue} = await Swal.fire({
title: 'Some multiple inputs',
html:
'<form id="multiple-inputs">' +
'<div class="form-group">' +
'<input type="text" class="form-control swal-input1" id="swal-input1" min=2 max=4 required>' +
'</div>' +
'<div class="form-group">' +
'<input type="text" class="form-control swal-input2" id="swal-input2" placeholder="Name" pattern="[A-Za-z]" required>' +
'</div>' +
'</form>',
});
我已经尝试了两种解决方案,实际上仅使用Bootstrap3,但它也应该与最新版本一起使用。