PHP validate URL input and browser validation first with captcha?

Could you please help me to create a form with an input field in it that will be used for URL input then on submit check with JavaScript if what was entered is correct URL then check again with the PHP if the URL format is correct before using that input field data. Any idea how to add simple captcha?

Well, you’ve correctly identified the steps required.

  1. Create HTML form.
    1.5: add captcha.
  2. Create Javascript validation for URL field.
  3. Create PHP form handler.

What have you done so far?

I am terrible with JavaScript and PHP I wondered if you could post some code?

PHP part. I have to use $regex (regular expression) then use preg_match to check if the pattern is correct but how have no idea.

<form action="check.php" method="post">

if ($_POST['URL']) {
//do check here

JavaScript have no idea at all so is captcha :slight_smile:
Sorry could you help

Well surely you’ve at least created the HTML form. So show us what you have, and we can help. We’re not going to write the entire thing for you.


<form action="check.php" method="post">
First name: <input type="text" name="url">
<input type="submit">


$url = $_POST['URL'];
if(filter_var($url, FILTER_VALIDATE_URL))
//continue cause it's a valid URL 

Could you hep me with JavaScript validation as I know 0 about that and adding simple captcha?

So you’ve got step 1 and step 3 covered. Good.

Step 2: Javascript validation.
Define a URL. (It’s not that easy.)
If I say that a valid URL is anything that begins with http , (note that this is a very weak definition of a URL.)

function ValidURL() {
  var str = document.getElementById("url").value;
  var pattern = new RegExp('^https?:\/\/');
  if(!pattern.test(str)) {
    alert("Please enter a valid URL.");
    return false;
  } else {
    return true;

and then your submit button needs an onclick=“return ValidURL();” and your URL field needs an id=“url”

Step 1.5: CAPTCHA
Go get set up with a site like reCAPTCHA, and follow their instructions for implanting the code into your form.

Wow that you so much that works pretty good.