gpt4 book ai didi

javascript - 如何在js中使用条件连接消息或仅在一个警报框中构造消息

转载 作者:行者123 更新时间:2023-12-03 08:39:59 26 4
gpt4 key购买 nike

我在仅在一个警报框中构建我的消息时遇到了麻烦。我已尽力构建,但这就是我所拥有的一切。希望有人可以帮助我并解释一下你的答案,因为我对连接以及我做错了什么感到困惑。

代码:

$(function(){
var len = $('#groupContainer > div').length;
var data = [];

for(var i=0; i < len; i++){

var number = $('#name_' + [i + 1]);
var date = $('#date_' + [i + 1]);
var count = i + 1;
var message ="";

var a = number.map(function(){
return this.value;
});

var b = date.map(function(){
return this.value;
});

var newObj = {Name: a[0], Date: b[0]}
data.push(newObj);
}

var message = "";
var ErrInGroup = false;

for(var i = 0; i < data.length; i++)
{
for(var d in data[i]) {

if(data[i].hasOwnProperty(d)) {
message += 'Group: ' + [i + 1] + '\n';
if(data[i][d] == "" || data[i][d] == null){
message += d + ' is required!\n';
}

if(d == "Date") {
if(data[i][d].length != 10 && data[i][d] != "") {
message += 'Invalid Date!\n';
}
}

}
}
}

if(message){
alert(message);
}

});

预期输出:

If all fields is empty If Only the first field is empty If one of the field is not empty If there are date's that are Invalid

<强> Fiddle Demo

注意:日期的长度为10,如果!=到10则无效。

最佳答案

请检查[

$(function(){

var errorObj = {
group: {},
hasError: false
};

function errorObjMange(groupNo, type, msg) {

errorObj.hasError = true;
groupNo = groupNo.toString();
if (!errorObj.group[groupNo]) {
errorObj.group[groupNo] = {};
}

errorObj.group[groupNo][type] = msg;
}

function alertError() {
var priority = {
"1": new Array(), // Invalid date
"2": new Array(), // empty name and inavlid date
"3": new Array(), // empty naem and empty date
"4": new Array(), // only empty name
"5": new Array(), // only empty DATE
}

for (var groupIndex in errorObj.group) {
var group = errorObj.group[groupIndex];
if (!group.emptyName && !group.emptyDate && group.invalidDate) {
priority["1"].push(groupIndex);
}

if (group.emptyName && !group.emptyDate && group.invalidDate) {
priority["2"].push(groupIndex);
}

if (group.emptyName && group.emptyDate && !group.invalidDate) {
priority["3"].push(groupIndex);
}

if (group.emptyName && !group.emptyDat && !group.invalidDate) {
priority["4"].push(groupIndex);
}

if (!group.emptyName && group.emptyDate && !group.invalidDate) {
priority["5"].push(groupIndex);
}

}

var groupStr = "Group: ";
var msg = '';

if (priority["3"].length) {
groupStr += priority["3"].toString();
msg = groupStr + '\n'
+ 'Name is required\n'
+ 'Date is required\n';
} else if (priority["2"].length) {
groupStr += priority["2"].toString();
msg = groupStr + '\n'
+ 'Name is required\n'
+ 'Invalid Date\n';
} else if (priority["4"].length || priority["5"].length) {
msg = '';

if (priority["4"].length) {

groupStr = 'Group :' + priority["4"].toString();
msg += groupStr + '\n'
+ 'Name is required\n';
}

if (priority["5"].length) {
groupStr = 'Group :' + priority["5"].toString();
msg += groupStr + '\n'
+ 'Date is required\n';

}

} else if (priority["1"].length) {
groupStr += priority["1"].toString();
msg = groupStr + '\n'
+ 'Invalid Date\n';
}
console.log();
alert(msg);
}

// Count number of groups we have.
var len = $('#groupContainer > div').length;
var data = [];



// For each given group
for(var i=0; i < len; i++){

// Get the values
var number = $('#name_' + [i + 1]);
var date = $('#date_' + [i + 1]);
var a = number.map(function(){
return this.value;
});

var b = date.map(function(){
return this.value;
});

var newObj = {Name: a[0], Date: b[0]};
data.push(newObj);
}

var message = "";


var ErrInGroup = false;

for(var i = 0; i < data.length; i++) {
haveErrorInGroup = false;


for(var d in data[i]) {

if(data[i].hasOwnProperty(d)) {
// Its because you are doing the
// below line.
//message += [i + 1] + '\n';


if(data[i][d] == "" || data[i][d] == null){

if (d == 'Name') {
errorObjMange((i+1), 'emptyName', (d + ' is required!\n'));
}

if (d == 'Date') {
errorObjMange((i+1), 'emptyDate', (d + ' is required!\n'));
}
}

if(d == "Date" && !(data[i][d] == "" || data[i][d] == null)) {
if(!(new Date(data[i][d]))
|| !(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(data[i][d].trim()))) {
message += 'Invalid Date!\n';
errorObjMange((i+1), 'invalidDate', 'Invalid Date!\n');

}
}
}
}

}

if(errorObj.hasError){
alertError();
//console.log(errorObj);
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="groupContainer">
<div id ="profileGroup1">
<div class="item">
Name1: <input type="text" id="name_1" value="asdf">
</div>
<div class="item">
Date1: <input type="text" id="date_1" value="01/50/48">
</div>
</div>
<div id ="profileGroup2">
<div class="item">
Number2: <input type="text" id="name_2" value="">
</div>
<div class="item">
Date2: <input type="text" id="date_2" value="">
</div>
</div>

关于javascript - 如何在js中使用条件连接消息或仅在一个警报框中构造消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072198/

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