-3

I need to block/validate shorten URL in String. Below string contains shorten URL how can I block/validate this in string .

Hi #first_name# This is Mondi from Novato Cleaners. May I ask for a favor ? Our google https://bit.ly requires reviews. Could you provide one ?Thank you

2 Answers 2

0

So for this you need to follow these steps:

1- extract all urls from string.

2- request each urls and get there original location. very well explained here:

How to get domain name from shortened URL with Javascript?

3- when you have originalUrl, just check if url != originalUrl then it is a shorten url.

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

3 Comments

was this a help ? @vignesh
var searchText = $("#text_message").val(); var urls = searchText.match(/\b(http|https)?(:\/\/)?(\S*)\.(\w{2,4})\b/ig); if(urls != null && urls != ""){ for (var i = 0, il = urls.length; i < il; i++) { if(urls[i] != null && urls[i] != ''){ if(shorten_urls.includes(urls[i])==true){ //alert(urls[i]); show_alert("error", "Doesn't support shorten url"); return false; } } } }
Happy to help :)
0

Use regex to find whether there is a URL in your string or not, if they're just replacing it what you need on that space

/(https?://[^\s]+)/g

var string = "Hi Vignesh This is Mondi from Novato Cleaners. May I ask for a favor ? Our google https://bit.ly requires reviews. Could you provide one ?Thank you";
var protomatch = /(https?:\/\/[^\s]+)/g;
var b = string.replace(protomatch, '');
console.log(b)

2 Comments

Thanks for the reply Yahiya. I need to block only shorten URL not all URL's what will be do for that?
@vignesh stackoverflow.com/a/8151236/7057771 check this out or just block some specific shorten URL

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.