[英]Uncaught TypeError: Object #<Object> has no method 'replace'
我試圖做一個JavaScript函數來驗證字段中的月份和年份。 字段上日期的格式為mm / yyyy,驗證該日期的函數為以下格式:
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;
}
}
在字段上輸入日期后,我會模糊地調用上面的函數,但是Chrome控制台返回錯誤Uncaught TypeError:對象#沒有方法“替換”。
看起來您正在同時使用vencimento
作為字符串和jQuery對象。 你不能那樣做。 也許像這樣創建一個新的變量vencimentoText
:
var vencimentoText = vencimento.val();
然后,在您當前將它用作字符串的任何地方使用vencimentoText
(例如,使用replace
, substring
等,但不使用addClass
等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.