Skip to content

Instantly share code, notes, and snippets.

@foxted
Last active June 29, 2020 03:31
Show Gist options
  • Save foxted/201c990cd0128cb4aab34fd8a2789526 to your computer and use it in GitHub Desktop.
Save foxted/201c990cd0128cb4aab34fd8a2789526 to your computer and use it in GitHub Desktop.
Validate a URL to add http if needed (VueJS)
validateUrl() {
let regex = /^(http|https)/;
if(this.url.length > 3 && !this.url.match(regex)) {
this.url = 'http://' + this.url;
}
}
@Astriaporta
Copy link

@silasrm
Copy link

silasrm commented Jun 29, 2020

My solution:

validateUrl: function() {
            const regex = RegExp('(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*(\\?[;&a-z\\d%_.~+=-@]*)?(\\#[-a-z\\d_@]*)?$', 'i');
            return this.url.match(regex);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment