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.
🌐
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
🌐
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.
🌐
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 ...
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.

🌐
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>
🌐
Reddit
reddit.com › r/flask › javascript form validation
r/flask on Reddit: JavaScript Form Validation
April 30, 2022 -

I used a js file to check if username and password are correct for a login. The js file contains a single username and password that work and I want to know how safe it is, I use flask so I can do the validation in the backend, Is it better or is it also unsafe?

The correct username and password according to the js file:

username = "username"

password = "password"

js file:

function validateForm() {
    let x = document.forms["myForm"]["username"].value;
    let y = document.forms["myForm"]["password"].value;
    let username = document.getElementById('errorUsername');
    let password = document.getElementById('errorPassword');

    if (x != "username" && y != "password") {
      username.style.visibility = "visible"; 
      password.style.visibility = "visible"; 
      return false
    }
    else if (x == "username" && y != "password"){
      username.style.visibility = "hidden"; 
      password.style.visibility = "visible"; 
      return false
    }
    
    else if (x != "username" && y == "password"){
      username.style.visibility = "Password"; 
      password.style.visibility = "hidden"; 
      return false
    }

    else {
      return true
    }
  }

HTML file:

<div class="center">
  <h1>login</h1>
  <form action="/login" method="POST" name="myForm" onsubmit="return validateForm()">
    <div class="txt_field">
      <input type="text" name="username" required autocomplete="off">
      <label for="username">username</label>
    </div>
    <small id=errorUsername>Incorrect username</small>
    <div class="pass"></div>
    <div class="txt_field">
      <input type="password" name="password" required>
      <label id=sisma for="username">password</label>
    </div>
    <small id=errorPassword>Incorrect password</small>
    <div class="pass"></div>
    <input type="submit" value="login">
    <div class="signup_link"> <a href="#"></a>
    </div>
  </form>
</div>

Python file in the backend:

@auth.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == "POST":
        username = request.form.get("username")
        session['logged_in'] = username
        return redirect(url_for('views.home'))
        
    return render_template("login.html")

Find elsewhere
🌐
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>
🌐
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).
🌐
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.
🌐
Bitstack
blog.bitsrc.io › you-have-been-doing-form-validation-wrong-8b36430d63f6
Best Practices in Validating Forms in JavaScript | Chameera Dulanga | Bits and Pieces | Bits and Pieces
November 6, 2023 - Input sanitization is the process of cleaning and validating user-generated content to remove potentially harmful data, such as malicious scripts. Implementing input sanitization is essential for enhancing security and protecting applications from security threats like cross-site scripting (XSS) and SQL injection. Here’s an example of how to implement input sanitization to protect against XSS attacks using the DOMPurify library in JavaScript: // HTML form <form id="comment-form"> <label for="comment">Comment:</label> <textarea id="comment" name="comment" required></textarea> <button type="su
🌐
The Art of Web
the-art-of-web.com › javascript › validate
Form Validation < JavaScript
Only when all conditions have been satisfied do we reach the return true command, in which case the form will be submitted. You'll see that the all validation scripts presented on this and subsequent pages adhere to the same basic format. Most modern browsers now support HTML5 Form Validation making it possible to validate form elements without (or before) any JavaScript is triggered.
🌐
BitDegree
bitdegree.org › learn › javascript-form-validation
Master JavaScript Form Validation & Get JavaScript Validation tips
August 8, 2017 - JavaScript form validation checks the inputted information before sending it to the server.
🌐
DEV Community
dev.to › nziokidennis › javascript-validation-39ka
JavaScript Validation - DEV Community
August 21, 2023 - Prerequisites: Basic Understanding ... validation is the process of making sure data provided by users into web forms meet the set criteria before being submitted to the server for the purpose of processing....
🌐
Codefinity
codefinity.com › courses › v2 › b9808bef-5849-468d-b10d-532a2e0a015f › a29789b4-29f5-403f-a590-e7e8ec3a45ce › ab8c5273-23a6-4eb7-af83-cb3175373856
Learn Form Validation | DOM Event Handling and Forms
Form validation is crucial in web application development as you can't trust the user's input. Therefore, you need to validate the user input both on the server and client side. In this chapter, you'll practice what you have learned in the previous chapter by validating each form element one after the other before submitting the form to the server.
🌐
Medium
medium.com › @pawan2505 › javascript-form-validation-7595b8c01c9e
JavaScript Form Validation. Form validation is an essential part of… | by PΛWΛN | Medium
October 1, 2024 - Form validation is an essential part of web development, ensuring that users provide the correct information before it is sent to a server. This guide will walk you through building a simple form validation system using JavaScript for a registration form that includes fields for name, password, date of birth, hobby, email, and gender.
🌐
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 …
🌐
freeCodeCamp
freecodecamp.org › news › learn-javascript-form-validation-by-making-a-form
Learn JavaScript Form Validation – Build a JS Project for Beginners ✨
September 22, 2021 - Now, we will create a function named engine which will do all sorts of form validation work for us. It will have three arguments – follow along here: 👇 ... Note: the id.value.trim() will remove all the extra white spaces from the value which the user inputs. You can get an idea of how it works by looking at this illustration 👇 ... We want the JavaScript to print a message inside the error class whenever the user submits a blank form.
🌐
NestJS
docs.nestjs.com › techniques › validation
Validation | NestJS - A progressive Node.js framework
When set to true, this will automatically remove non-whitelisted properties (those without any decorator in the validation class). Alternatively, you can stop the request from processing when non-whitelisted properties are present, and return an error response to the user. To enable this, set the forbidNonWhitelisted option property to true, in combination with setting whitelist to true. ... Payloads coming in over the network are plain JavaScript objects.