2

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 {

            }
        }
    }
}

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

I've checked this but this will work only when we use exact word " example ". What i actually need is if user enters example.com this url need to check with the item in the array.

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.