0

I want to avoid adding all site addresses including protocol or non protocol based or subdomains, i.e any sort of domain but looks like the regex is matching up with other texts as well which is wrong. I want it to only match up with domains not other things.

Code:

$(`[name="name"], [name="title"]`).not('[readonly]').on('focusout', function(){
    var value = $(this).val();
    var regex = new RegExp('^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$');
    if (regex.test(value.toLowerCase())) {
        $(this).val('');
    }
});

Take a look at the GIF:

enter image description here

NOTE: I can literally do it like this way but I don't want to:

$(`[name="name"], [name="title"]`).not('[readonly]').on('focusout', function(){
    var value = $(this).val();
    var regex = new RegExp('^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$');
    if (!/\s/.test(value)) {
        if (regex.test(value.toLowerCase())) {
            $(this).val('');
        }
    }
});
Umair Shah
  • 2,305
  • 2
  • 25
  • 50

0 Answers0