Benny Sutton is 4 hire - just click here

Validate URL's with JavaScript and Regex

Validate URL's with JavaScript and Regex

Very handy to validate input on the client in the browser using JavaScript and Regular expressions

See the working demo below.

Demo: How to Validate URL's with JavaScript and Regex

JavaScript

This is pretty much the standard JavaScript you'll paste into your page script block - or in a .js file if you prefer.

            
            function isURL(elm) {
              if (elm.value == '') {
                    alert("fill in email"); return false;
                }
                if (elm.value.indexOf(" ") != -1) {
                    alert("Spaces are not allowed in a URL");
                    return false;
                } if (elm.value.match(/(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp):\/\/)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&%_\.\/-~-]*)?/)) {
                    alert("VALID URL");
                   return true;
                } else {
                    alert("The URL seems incorrect!!! ");
                    return false;
                }
            }
        
    

CSS

Edit as necessary.

           
            body{
  font-size:2em;
  font-weight:bold;
}
input{
  font-size:50px;
}
button{
  font-size:50px;
}
        
    

Conclusion

A few lines of JQuery is all you need to validate url's with javascript and regex