.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> JQuery >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Validate website URL using jquery code

Posted By:JQuery Developer       Posted Date: May 22, 2013    Points: 40    Category: JQuery    URL: http://www.dotnetspark.com  

Below code example to check or validate string input value is a valid URL or not it will work with http or without http using JQuery

Some of the web site need to validate website url in their form. below JQuery code will validate or check the input entered by user is a valid URL or not. For this we are going to use regular expression. Lets see the code

Javascript Method with regular expression

function ValidateURL(urlToCheck) {
    // Below regular expression can validate input URL with or without http:// etc 
    var pattern = new RegExp("^((http|https|ftp)\://)*([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
    return pattern.test(urlToCheck);


JQuery to bind click event and your custom logic

$(document).ready(function () {
    $('#myButton').click(function () {
        // Get the value from textbox
        if (ValidateURL($('#txtURL').val().trim())) {
            //your custom logic goes here if its valid web site URL    
            alert("Good Job!!!");
        else {
            // Not a valid URL
            alert("Please enter a valid URL");

HTML control used in this example

<input id="txtURL" type="text" name="name" value=" " />
<input id="myButton" type="button" name="name" value=" Click To Test URL Input" />

Click here for live demo

If you want to validate email using JQuery you can refer below link code sample.

Validate email address using JQuery code


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend