0

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.

HardCode
  • 1,613
  • 4
  • 21
  • 42
  • 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 Answers3

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

If you use jQuery, maybe this plugin will help:

http://api.jquery.com/jQuery.inArray/

cambraca
  • 27,014
  • 16
  • 68
  • 99
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