Interview Questions

Jquery - Regular Expression to validate Url

Use the following code to validate urls in jquery.

function validateUrl(e){

var urlPattern = new RegExp(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/);
        var spaceCharPattern = new RegExp(/[\s]/);
        if (!urlPattern.test($(e.target).val()) || spaceCharPattern.test($(e.target).val())) {
             
               alert("invalid url");
        }
}

0
No votes yet
Your rating: None