i want a function for javascript like in_array in PHP to look for the special characters in an array. if that special character is in the array it returns true.
Asked
Active
Viewed 352 times
0
-
possible duplicate of [JavaScript is in array](http://stackoverflow.com/questions/5864408/javascript-is-in-array) – Felix Kling Oct 14 '11 at 06:58
3 Answers
2
There is a javascript function indexOf
var myArray = [9, 3, 4, 7];
var index = myArray.indexOf(4);
// index is 2
index = myArray.indexOf(6);
// index is -1

Christian P
- 12,032
- 6
- 60
- 71
-
My Array is like var special_chars = ['@','#','$',%] and i have to check that if the input form user has any special chars or Not – HardCode Oct 14 '11 at 07:12
-
It will work if you're searching for strings but I would strongly recommend you to use jQuery or some other framework because it will help you a lot when working in Javascript. – Christian P Oct 14 '11 at 07:24
0
You can resort to indexOf if you (the client you are serving) have javascript 1.6
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
On the same page there is also an equivalent function if you don't have the function implemented natively into the engine.
For further references on array methods you can read
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array

Eineki
- 14,773
- 6
- 50
- 59