well, I am more of a PHP person, and my JS skills are close to none when it comes to any JS other than simple design related operations , so excuse me if I am asking the obvious .
the following operations would be a breeze in PHP (and might also be in JS - but I am fighting with unfamiliar syntax here ...)
It is some sort of input validation
var ar = ["BRS201103-0783-CT-S", "MAGIC WORD", "magic", "Words", "Magic-Word"];
jQuery(document).ready(function() {
jQuery("form#searchreport").submit(function() {
if (jQuery.inArray(jQuery("input:first").val(), ar) != -1){
jQuery("#contentresults").delay(800).show("slow");
return false;
}
This question has 2 parts .
E.g. - BRS201103-0783-CT-S
will give the same result as brs201103-0783-ct-s
AND Brs201103-0783-CT-s
or MAGIC magic Magic MaGIc
basically i need something like ignoreCase() for array , but I could not find any reference to that in jQuery nor JS...
I tried toLowerCase()
- but It is not working on the array (ittirating??) and also, would it resolve the mixed case ?
E.g. - if one types only "word"
, I would like it to pass as "words"
, and also if someone types "some word"
it should pass (containing "word" )
The jQuery inArray() method is used to find a specific value in the given array. If the value found, the method returns the index value, i.e., the position of the item. Otherwise, if the value is not present or not found, the inArray() method returns -1. This method does not affect the original array.
inArray() method we can easily verify if values are available in an array of objects. The jQuery. InArray() is a built-in jquery method, used to find a given value within an array. If the value is available it returns its index, and if value not exists then it returns -1.
You can process your array to be entirely lowercase, and lowercase your input so indexOf()
will work like it's performing a case insensitive search.
You can lowercase a string with toLowerCase()
as you've already figured out.
To do an array, you can use...
arr = arr.map(function(elem) { return elem.toLowerCase(); });
You could check for a substring, for example...
// Assuming you've already transformed the input and array to lowercase.
var input = "word";
var words = ["word", "words", "wordly", "not"];
var found = words.some(function(elem) { return elem.indexOf(input) != -1; });
Alternatively, you could skip in this instance transforming the array to be all lowercase by calling toLowerCase()
on each elem
before you check indexOf()
.
some()
and map()
aren't supported in older IEs, but are trivial to polyfill. An example of a polyfill for each is available at the linked documentation.
As Fabrício Matté also pointed out, you can use the jQuery equivalents here, $.map()
for Array.prototype.map()
and $.grep()
with length
property for Array.prototype.some()
. Then you will get the browser compatibility for free.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With