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.

1

3 Answers 3

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  
Sign up to request clarification or add additional context in comments.

2 Comments

My Array is like var special_chars = ['@','#','$',%] and i have to check that if the input form user has any special chars or Not
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.
0

If you use jQuery, maybe this plugin will help:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.