Reputation: 916
I'm working in a module that had the option to enter five website urls. So i need validation for those input fields. I tried to find out some plugins but didn't find anything so I'm trying to build a custom validation.I tried some code. Actually what I'm trying to achieve is that when a user enters a url into this 5 input fields i need to check whether any of this object in the array exists in the url .
function savewebsite(){
var cars = ["facebook", "amazon", "instagram", "twitter", "youtube", "paypal", "twitch", "tiktok", "cash.app", "onlyfans", "skype", "gofundme", "manyvids", "snapchat"];
for (var j = 1; j <= 5; j++) {
for (var i = 0; i < cars.length; i++) {
if ($('#sociallink'+j).val().indexOf(cars[i]) > -1) {
return false;
// alert("hai");
} else {
}
}
}
}
Upvotes: 2
Views: 331
Reputation: 38174
If you need to check whether the item exists in array, then just use some
method:
let val = ($('#sociallink'+j).val();
var isThere = cars.some(s => s === val )
An example:
let val = 'skype';
let cars = ["facebook", "amazon", "instagram", "twitter", "youtube", "paypal", "twitch", "tiktok", "cash.app", "onlyfans", "skype", "gofundme", "manyvids", "snapchat"];
let isThere = cars.some(s => s === val);
console.log(isThere);
UPDATE:
If you want to check whether string contains another string, then you can use includes
method:
let val = 'skype.com';
let cars = ["facebook", "amazon", "instagram", "twitter", "youtube", "paypal", "twitch", "tiktok", "cash.app", "onlyfans", "skype", "gofundme", "manyvids", "snapchat"];
let isThere = cars.some(s => val.includes(s));
console.log(isThere);
Upvotes: 1