javascript - SCRIPT438: Object doesn't support property or method 'indexOf' - Stack Overflow

my function below keeps breaking at var pos1=dtStr.indexOf(dtch)function isDate(dtStr){var daysInMonth

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 that indexOf 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 means this 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
Add a ment  | 

2 Answers 2

Reset to default 2

The 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;
    // ...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745642995a4637803.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信