jQuery Form Validation to Restrict Multiple URL

by Vincy. Last modified on July 6th, 2023.

In jQuery, it is effortless to restrict the user from entering more than one URL via a form input. By validating this, we can avoid spam content with more hyperlinks.

In the previous tutorial, we have seen the jQuery example to validate the contact form. This tutorial uses jQuery match() to check the number of URL occurrences.

We pass a regex pattern to find a URL match among the input string for this jQuery function.

View Demo

HTML Text-Area Input

This code shows the HTML text area input to the user. After entering text data, the jQuery regex match will be called to check URL occurrences.


<textarea id="text-content" cols="80" rows="4"></textarea>
	<input type="button" name="btnValidate" id="btnValidate" value="Submit" onClick="validate()"/>
	<label id="validation-message"></label>

jQuery URL Match

The jQuery regex match function will return the number of URLs the user enters. We are checking for the URLs starting with HTTP://, https://, or FTP://. If the count exceeds 1, then this code will return an error message.

function validate() {
    var num_url_match = $('#text-content').val().toLowerCase().match(/(<a href="([^"]+)">([^$|\s+]*)<\/a>)/g); 	
	if(num_url_match.length > 1) {
        $("#validation-message").html(" More than 1 URL is not allowed.");
    } else {
		$("#validation-message").html(" " +num_url_match.length+ " URL is found.");

View DemoDownload

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

↑ Back to Top

Share this page