2

Is this ASP InStr function equivalent to the js indexOf function :

ASP :

<%
Function validation(ByVal var2)
  If (InStr(var1, "," & var2 & ".") = 0) Then
    validation = 1
    Exit Function
  End If
End Function
%>

JS :

validation = function(var2) {
  if (var1.indexOf("," + var2 + ".") == -1) {
      ValidateAction = 1;
  };
  return ValidateAction;
};
user3470686
  • 31
  • 1
  • 3

1 Answers1

4

No, they are same only with one case. If value not found InStr return 0 and indexOf return -1, but Instr has many other, have a look on

Differences

The InStr function returns the position of the first occurrence of one string within another.

The InStr function can return the following values:

If string1 is "" - InStr returns 0
If string1 is Null - InStr returns Null
If string2 is "" - InStr returns start
If string2 is Null - InStr returns Null
If string2 is not found - InStr returns 0
If string2 is found within string1 - InStr returns the position at which match is found
If start > Len(string1) - InStr returns 0

Whereas javascript indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.
Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20