You have several errors there.

First, you have to return a value from the function in the HTML markup: <form name="ff1" method="post" onsubmit="return validateForm();">

Second, in the JSFiddle, you place the code inside onLoad which and then the form won't recognize it - and last you have to return true from the function if all validation is a success - I fixed some issues in the update:

https://jsfiddle.net/mj68cq0b/

function validateURL(url) {
    var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
    return reurl.test(url);
}

function validateForm()
{
    // Validate URL
    var url = $("#frurl").val();
    if (validateURL(url)) { } else {
        alert("Please enter a valid URL, remember including http://");
        return false;
    }

    // Validate Title
    var title = $("#frtitle").val();
    if (title=="" || title==null) {
        alert("Please enter only alphanumeric values for your advertisement title");
        return false;
    }

    // Validate Email
    var email = $("#fremail").val();
    if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {
        alert("Please enter a valid email");
        return false;
    }
  return true;
}
Answer from Adidi on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_validation.asp
JavaScript Form Validation
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... HTML form validation can be done by JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Extensions › Forms › Form_validation
Client-side form validation - Learn web development | MDN
HTML form validation HTML form attributes can define which form controls are required and which format the user-entered data must be in to be valid. JavaScript form validation JavaScript is generally included to enhance or customize HTML form validation.
Discussions

Form Validation using JavaScript Functions
Forms that allow input must be validated, data element by data element. The input forms I’ve created have data elements whose validation seems to fall into categories that will be repeated across a number of forms. First, every form’s entry spaces must be checked to see if they are empty ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
March 3, 2025
HTML/JavaScript: Simple form validation on submit - Stack Overflow
Here is also in jsfiddle http://jsfiddle.net/CrLsR/ ... You shouldn't rely on Javascript to validate a form. More on stackoverflow.com
🌐 stackoverflow.com
Why should I use javascript to create a form validation when I can use html?
You generally need 3 levels of validation: HTML-level to make sure that they've entered something in the inputs required Client-side: spot the obvious errors like no proper email, new passwords not matching each other et al. Server-side: Ensure that every field is the type in needs to be, contains no special characters and repeat all the checks you did on the client side. Never trust client-side validation! More on reddit.com
🌐 r/learnjavascript
22
32
December 28, 2019
Live validation in JavaScript and Jquery

It might help yourself and others if you put the code in a jsfiddle (http://jsfiddle.net/). Generally it's easier to help someone with code that way.

To directly answer your question, one path is to use the keypress (or keyup) event to do the validation.

More on reddit.com
🌐 r/javascript
3
0
July 29, 2011
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › form-validation-using-javascript
JavaScript Form Validation - GeeksforGeeks
JavaScript Validation: The JS validates user input on form submission using regular expressions (regex) for fields like email, username, password, and phone, and calculates age for the DOB field to ensure the user meets the age requirement.
Published   January 9, 2025
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_form_validations.htm
JavaScript - Form Validation
Now we will see how we can validate our entered form data before submitting it to the web server. The following example shows how to validate an entered email address. An email address must contain at least a &commat; sign and a dot (.). Also, the &commat; must not be the first character of the email address, and the last dot must at least be one character after the &commat; sign. Try the following code for email validation. <script type = "text/javascript"> function validateEmail() { var emailID = document.myForm.EMail.value; atpos = emailID.indexOf("@"); dotpos = emailID.lastIndexOf("."); if (atpos < 1 || ( dotpos - atpos < 2 )) { alert("Please enter correct email ID") document.myForm.EMail.focus() ; return false; } return( true ); } </script>
🌐
The Odin Project
theodinproject.com › lessons › node-path-javascript-form-validation-with-javascript
Form Validation with JavaScript | The Odin Project
Add the JavaScript code that checks validation as the user progresses through the form. When a user leaves a form field, it should automatically validate that field. Test out all possible cases.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Form Validation using JavaScript Functions - JavaScript - The freeCodeCamp Forum
March 3, 2025 - Forms that allow input must be validated, data element by data element. The input forms I’ve created have data elements whose validation seems to fall into categories that will be repeated across a number of forms. First, every form’s entry spaces must be checked to see if they are empty ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Guides › Constraint_validation
Using HTML form validation and the Constraint Validation API - HTML | MDN
By programmatically writing content into the form (certain constraint validations are only run for user input, and not if you set the value of a form field using JavaScript).
Find elsewhere
Top answer
1 of 6
24

You have several errors there.

First, you have to return a value from the function in the HTML markup: <form name="ff1" method="post" onsubmit="return validateForm();">

Second, in the JSFiddle, you place the code inside onLoad which and then the form won't recognize it - and last you have to return true from the function if all validation is a success - I fixed some issues in the update:

https://jsfiddle.net/mj68cq0b/

function validateURL(url) {
    var reurl = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
    return reurl.test(url);
}

function validateForm()
{
    // Validate URL
    var url = $("#frurl").val();
    if (validateURL(url)) { } else {
        alert("Please enter a valid URL, remember including http://");
        return false;
    }

    // Validate Title
    var title = $("#frtitle").val();
    if (title=="" || title==null) {
        alert("Please enter only alphanumeric values for your advertisement title");
        return false;
    }

    // Validate Email
    var email = $("#fremail").val();
    if ((/(.+)@(.+){2,}\.(.+){2,}/.test(email)) || email=="" || email==null) { } else {
        alert("Please enter a valid email");
        return false;
    }
  return true;
}
2 of 6
17

The simplest validation is as follows:

<form name="ff1" method="post">
  <input type="email" name="email" id="fremail" placeholder="[email protected]" />
  <input type="text" pattern="[a-z0-9. -]+" title="Please enter only alphanumeric characters." name="title" id="frtitle" placeholder="Title" />
  <input type="url" name="url" id="frurl" placeholder="http://yourwebsite.com/" />
  <input type="submit" name="Submit" value="Continue" />
</form>

It uses HTML5 attributes (like as pattern).

JavaScript: none.

🌐
Medium
medium.com › @ravipatel.it › form-validation-with-javascript-all-html-input-types-2b0f0bb6e28e
Form Validation with JavaScript (All HTML Input Types) | by Ravi Patel | Medium
September 13, 2024 - Form Validation with JavaScript (All HTML Input Types) Objective: This blog will guide you through building a registration form using various HTML input types and adding JavaScript validation. The …
🌐
Hyva
docs.hyva.io › hyva-themes › writing-code › form-validation › javascript-form-validation.html
JavaScript form validation - Hyvä Docs
Usually you want to trigger the validation of the whole form using the Alpine.js @submit event with the onSubmit method. <form x-data="hyva.formValidation($el)" @submit="onSubmit"> ... Since $el is only used on the root element with x-data of ...
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › forms › validation
Validation · Bootstrap v5.0
All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls. Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS. You may provide custom validity messages with setCustomValidity in JavaScript.
🌐
freeCodeCamp
freecodecamp.org › news › form-validation-in-javascript
Client-Side Form Handling with JavaScript – Explained with Example Code
March 8, 2024 - In a real scenario, you might want to implement an AJAX call to submit the data to a server for processing and handle the response accordingly But that's not what we're going to discuss, as mentioned at the start of this tutorial. Overall this example covers form creation, form handling with JavaScript, form validation using regular expressions, and dynamic custom error message display, demonstrating a basic user registration form with client-side validation.
🌐
Tutorial Republic
tutorialrepublic.com › javascript-tutorial › javascript-form-validation.php
JavaScript Form Validation - Tutorial Republic
The form validation process typically consists of two parts— the required fields validation which is performed to make sure that all the mandatory fields are filled in, and the data format validation which is performed to ensure that the type and format of the data entered in the form is valid. Well, let's get straight to it and see how this actually works. Let's first create a simple HTML form that we will validate on client-side using JavaScript when the user clicks on the submit button.
🌐
W3Resource
w3resource.com › javascript › form › javascript-sample-registration-form-validation.php
JavaScript: A sample registration form validation - w3resource
August 19, 2022 - It sets the focus to the input field until the user supplies a valid value. When the user does so, they may proceed and can supply value to the next available field. The later JavaScript function created is called on the onsubmit event of the form.
🌐
SitePoint
sitepoint.com › blog › javascript › instant form validation using javascript
Instant Form Validation Using JavaScript — SitePoint
November 11, 2024 - The JavaScript function ‘instantValidation()’ tests a field and performs the actual validation, controlling the ‘aria-invalid’ attribute to indicate the state of the field. This function can be bound to an ‘onchange’ event to provide instant form validation.
🌐
DEV Community
dev.to › javascriptacademy › form-validation-using-javascript-34je
Form validation using javascript - DEV Community
November 4, 2021 - In-fact, by using the HTML form validation attributes (type="", required, pattern, etc), you can make your Javascript validation far more generic.
🌐
Reddit
reddit.com › r/learnjavascript › why should i use javascript to create a form validation when i can use html?
r/learnjavascript on Reddit: Why should I use javascript to create a form validation when I can use html?
December 28, 2019 -

So I am new to javascript and wanted to start a project where I would make a form validation thing that insured you had entered your name, email and password. Although when I started building it I realized you can just add required to all of them and when you click submit if you have not entered your info it will give a pre styled message saying to enter. Can someone explain the benefits of using javascript for this specific feature? example below

  <form class="join-form">
        <div class="input-group">
          <label>Name:</label>
          <input type="text" required>
        </div>
        <div class="input-group">
          <label>Email:</label>
          <input type="email" required>
        </div>
        <div class="input-group">
          <label>Password:</label>
          <input type="password" required>
        </div>
        <div class="input-group">
          <button type="submit" class="btn">Join Now</button>
        </div>
      </form>
🌐
DEV Community
dev.to › tracy4code › form-validation-with-javascript-21l6
Form Validation with JavaScript - DEV Community
June 3, 2023 - In this case, the width of the .formContainer is set to 30% of the parent element. This makes the form container smaller when viewed on larger screens. The code listens for the form submission event and prevents the form from submitting by default. It then performs validations on each input field and displays appropriate error messages in the errorMessage divs and applies the success and error classes to the parent formDetails divs based on the validation result.
🌐
Javatpoint
javatpoint.com › javascript-form-validation
JavaScript form validation - javatpoint
April 12, 2013 - JavaScript form validation tutorial for beginners and professionals with example, javascript retype password validation example, example of javascript number validation, javascript validation with image, javascript email validation etc.