3

I'm writing some client-side validation for our custom CMS. I need to check for any URL containing an image in a text area, and let the user know that they need to use https instead of http.

I've figured out how to search for any URL that starts with http:

$('#template_form').submit(function() {
        val = $("#template_data").val();
        if (val.match(/http:\/\//)) {
            alert("You must set all complete image URLs to be https!");
            $("#logError").val("true");
            return false;
        }
        return true;
    });

But this will also apply to any regular links that are included. If we could limit this to <img> tags it would be simpler, because I could just get the last three characters of the string and do an indexOf against an array of image suffixes.

We can't do that, though - an image URL could be included in a custom tag, it could be part of an alt parameter, it could be posted as a direct link, or any number of other things. Is there any way to test the regex against an array, or do I need to loop through the array and run a String.indexOf against each element separately?

Ideally, I'd do something like this:

suffixes = [".jpg", ",jpeg", ".png", ".gif", ".tiff", ".bmp"];
if (val.match(/http:\/\//)) && suffixes.indexOf(**some kind of regex**) {
    alert("You must set all complete image URLs to be https!");
    $("#logError").val("true");
    return false;
}

but I can't quite figure out what that regex would be.

3
  • Is val just html? Commented Nov 30, 2016 at 16:22
  • @KevBot - yes, although some of it is custom tags. Commented Nov 30, 2016 at 16:23
  • suffix regex: /\.(jpe?g|png|bmp|tiff|gif)/g; Commented Nov 30, 2016 at 16:27

1 Answer 1

4

You can build a regex using your image extensions:

var suffixes = ["jpg", "jpeg", "png", "gif", "tiff", "bmp"];

var re = new RegExp('http://[^>\\s]+?\.(?:' + suffixes.join('|') + ')');

if (re.test(val)) {
    alert("You must set all complete image URLs to be https!");
    $("#logError").val("true");
    return false;
}

JSFiddle

Code Demo

var val = '<img src="http://www.google.com/test.jpg" />';
var suffixes = ["jpg", "jpeg", "png", "gif", "tiff", "bmp"];
var re = new RegExp('http://[^>\\s]+?\.(?:' + suffixes.join('|') + ')');

console.log(re.test(val));

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

7 Comments

.* is a bit aggresive use a url regex instead
Hm. That sounds like what I want, but I must be implementing it incorrectly. I'm updating the OP with what I did; please help!
It prints exactly what I showed in the OP as the text I typed into the textarea: all image urls must be secure this is not an image url: google.com this is <img src="http://www.google.com/test.jpg" />
(Sorry, can't figure out how to format multiple lines in a comment.)
That returns true for the URL you entered. It needs to return false - it contains an image suffix and doesn't start with https.
|

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.