Javascript 未捕获类型错误:对象#<;对象>;没有方法';更换';
我试图创建一个JavaScript函数来验证字段中的月份和年份。字段上的日期格式为mm/yyyy,验证日期的函数如下:Javascript 未捕获类型错误:对象#<;对象>;没有方法';更换';,javascript,Javascript,我试图创建一个JavaScript函数来验证字段中的月份和年份。字段上的日期格式为mm/yyyy,验证日期的函数如下: function validateVencimento(){ var valid = true; var currentTime = new Date(); var actualMonth = currentTime.getMonth() + 1; var actualYear = currentTime.getFullYear(); v
function validateVencimento(){
var valid = true;
var currentTime = new Date();
var actualMonth = currentTime.getMonth() + 1;
var actualYear = currentTime.getFullYear();
vencimento = vencimento.replace('///g', '');
var month = parseInt(vencimento.substring(0, 2),10);
var year = parseInt(vencimento.substring(2, 6),10);
if((month < 1) || (month > 12)) valid = false;
if((year < actualYear)) valid = false;
if((year < actualYear) && (month < actualMonth)) valid = false;
if(valid == false){
vencimento.addClass("error");
vencimentoInfo.text("A validade do cartao esta invalida!");
vencimentoInfo.addClass("error");
return false;
}
else{
vencimento.removeClass("error");
vencimentoInfo.text("");
vencimentoInfo.removeClass("error");
return true;
}
}
函数validateVencimento(){
var valid=true;
var currentTime=新日期();
var actualMonth=currentTime.getMonth()+1;
var actualYear=currentTime.getFullYear();
vencimento=vencimento.replace('///g','');
var month=parseInt(vencimento.substring(0,2,10));
var year=parseInt(vencimento.子串(2,6,10);
如果((月<1)| |(月>12))有效=假;
如果((年<实际年))有效=假;
如果((年<实际月)和&(月<实际月))有效=假;
if(有效==false){
vencimento.addClass(“错误”);
vencimentoInfo.text(“一个合法的医疗机构!”;
vencimentoInfo.addClass(“错误”);
返回false;
}
否则{
vencimento.removeClass(“错误”);
vencimentoInfo.text(“”);
vencimentoInfo.removeClass(“错误”);
返回true;
}
}
在字段中输入日期后,我在blur上调用上面的函数,但Chrome控制台返回错误Uncaught TypeError:Object#没有“replace”方法。看起来您正在使用
vencimento
作为字符串和jQuery对象。你不能那样做。也许可以创建一个新变量vencimentoText
,如下所示:
var vencimentoText = vencimento.val();
然后将
vencimentoText
用作字符串(例如,使用replace
,substring
等,但不使用addClass
等)行号,好吗?JSFIDLE测试用例也会有很大帮助。在哪里定义了vencimento
?哦,不需要,代码中只有一个replace
,哈哈哈。您在哪里定义了vencimento
变量?它是否在全球范围内?那么您希望vencimento
是什么(您发布的代码中没有定义)?显然,它没有replace
方法(也许它不是字符串?@elclanrs,是的,Regex对象应该只有/\//g
(没有引号,转义/
)。@T.J.Crowder:text,addClass
,和removeClass
方法在没有任何其他信息的情况下向我指示jQuery。@ick:yes,抱歉,我刚刚看到了这些(但遗憾的是,在你看到评论之前没有删除!)。我认为你的基础相当坚实。:-)@T.J.Crowder,.addClass
,.removeClass
和.text
对我来说听起来很像jQuery。