[英]How to concatenate/change the error message if the input is not a valid date in js?
I tried this for almost two days but still nothing.我尝试了将近两天,但仍然没有。 Maybe someone can help who is highly skilled in javascript loops.
也许有人可以帮助精通 javascript 循环的人。
I asked this question before and change some code but still no luck in showing the expected data.我之前问过这个问题并更改了一些代码,但仍然没有显示预期数据。 And still struggling for this.
并且还在为此而挣扎。
I have this code:我有这个代码:
$(function(){
var len = $('#groupContainer > div').length;
var data = [];
for(var i=0; i < len; i++){
var number = $('#number_' + [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 = {number: a[0], date: b[0]}
data.push(newObj);
}
var message = "";
for(var c = 0; c < data.length; c++)
{
haveErrorInGroup = false;
for(var d in data[c])
{
if(data[c].hasOwnProperty(d))
{
if(data[c][d] == "")
{
if(!haveErrorInGroup){
haveErrorInGroup= true;
message += 'Group: ' + [c + 1] + '\n';
}
message += d + ' is required!\n';
}
if(d == "date")
{
if(data[c][d].length != 22 && data[c][d] != "")
{
message += 'Invalid Date!\n';
}
}
}
}
}
if(message){
alert(message);
}
});
And the expected output:和预期的输出:
If all the fields in group 1 is filled and group 2 is not show alertbox:如果第 1 组中的所有字段都已填写且第 2 组未显示警报框:
Group 2:
Number is required!
Date is required!
If all field is not filled show:如果未填写所有字段,请显示:
Group 1, 2 Number is required!
Group 1, 2 Date is required!
And if date is not really a date:如果日期不是真正的日期:
Group 1, 2 Date is invalid.
And if one of the inputs is invalid.如果输入之一无效。
Group 2 Date is invalid.
If all field is filled do nothing.如果所有字段都已填满,则什么都不做。
I think you are looking for something like jquery-validate .我认为您正在寻找类似jquery-validate 的东西。 I'm pretty sure it comes with a native date validator but if you want to have a more specific format you can just put a regex into it.
我很确定它带有一个本地日期验证器,但如果你想要一个更具体的格式,你可以在其中放入一个正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.