my function below keeps breaking at var pos1=dtStr.indexOf(dtch)
function isDate(dtStr){
var daysInMonth = DaysArray(12);
var pos1 = dtStr.indexOf(dtCh);
var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
var strMonth = dtStr.substring(0, pos1);
var strDay = dtStr.substring(pos1 + 1, pos2);
var strYear = dtStr.substring(pos2 + 1);
strYr = strYear;
the error message that I am getting is SCRIPT438: Object doesn't support property or method 'indexOf'. I took out all of my code after the variables and I am still receiving the same error
my function below keeps breaking at var pos1=dtStr.indexOf(dtch)
function isDate(dtStr){
var daysInMonth = DaysArray(12);
var pos1 = dtStr.indexOf(dtCh);
var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
var strMonth = dtStr.substring(0, pos1);
var strDay = dtStr.substring(pos1 + 1, pos2);
var strYear = dtStr.substring(pos2 + 1);
strYr = strYear;
the error message that I am getting is SCRIPT438: Object doesn't support property or method 'indexOf'. I took out all of my code after the variables and I am still receiving the same error
Share Improve this question edited Nov 16, 2011 at 21:48 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Nov 16, 2011 at 21:43 Juan AlmonteJuan Almonte 3952 gold badges8 silver badges14 bronze badges 5-
2
What are you passing
isDate
? – Alex Turpin Commented Nov 16, 2011 at 21:46 -
2
And what is
this
? You understand thatindexOf
is a function for strings? What exactly are you trying to achieve? – Alex Turpin Commented Nov 16, 2011 at 21:53 - I am trying to get the date entered into a textfield and break it into substrings at the dtCh either "-" or "/" – Juan Almonte Commented Nov 16, 2011 at 22:02
-
@Juan: Since you're using
onkeyup
, that meansthis
is the HTML element, you need to use.value
to get its value. – gen_Eric Commented Nov 16, 2011 at 22:06 -
1
this
is the input object. You need to use it's value instead. See @Rocket's answer. – Alex Turpin Commented Nov 16, 2011 at 22:07
2 Answers
Reset to default 2The isDate
function is expecting its dtStr
parameter to be a String
(as indicated by the indexOf
and substring
function calls). However, the function is being invoked with an argument that is of type Object
rather than String
. You will need to modify the code where this function is being called to pass the correct parameter to the isDate
function.
You said you're doing onkeyup="isDate(this);"
. This is passing the element to isDate
, you need to get its value before you can use it.
function isDate(dtStr){
dtStr = dtStr.value;
// ...
}