My personal recommendation would be something like this:

<script type="text/javascript">
function validate() {
    return [
        document.form.price,
        document.form.price2,
        document.form.price3,
        document.form.price4,
        document.form.price5
    ].every(validatePrice)
}

function validatePrice(price)
{
    if (price.value.trim() === "") {
        alert("Please enter a price");
        price.focus();
        return false;
    }
    if (price.value !== "") {
        if (! (/^\d*(?:\.\d{0,2})?$/.test(price.value))) {
            alert("Please enter a valid price");
            price.focus();
            return false;
        }
    }
    return true;       
}
</script>
Answer from Brisbe on Stack Overflow
🌐
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

validation - How to validate input using javascript - Stack Overflow
jQuery Form Validator is a feature rich and multilingual jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code. More on stackoverflow.com
🌐 stackoverflow.com
HTML/JavaScript: Simple form validation on submit - Stack Overflow
I'm trying to validate my form with the easiest way possible, but somehow it is not working and when I click submit it just takes me to the next page without giving the alert message: HTML: More on stackoverflow.com
🌐 stackoverflow.com
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
javascript - How to manually trigger validation with jQuery validate? - Stack Overflow
I want to manually trigger validation including showing error messages with jQuery Validate. The scenario I am trying to accomplish is a form like this: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Site24x7
site24x7.com › tools › javascript-validator.html
Javascript Validator | Validate Online - Site24x7 Tools
Instantly validate JavaScript code with our free, accurate, and user-friendly online validator tool. Ensure error-free scripts effortlessly.
🌐
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
🌐
Validatejs
validatejs.org
validate.js
Validate.js provides a declarative way of validating javascript objects.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_form_validations.htm
JavaScript - Form Validation
This was really a lengthy process which used to put a lot of burden on the server. JavaScript provides a way to validate form's data on the client's computer before sending it to the web server.
Find elsewhere
🌐
W3C
validator.w3.org
The W3C Markup Validation Service
This validator checks the markup validity of Web documents in HTML, XHTML, SMIL, MathML, etc. If you wish to validate specific content such as RSS/Atom feeds or CSS stylesheets, MobileOK content, or to find broken links, there are other validators and tools available.
🌐
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 - It allows you to create custom validation functions to check various aspects of user input, such as format, length, or specific requirements. The below examples show a simple JavaScript validation function to check if a password is at least 8 characters long:
🌐
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).
🌐
Code Beautify
codebeautify.org › jsvalidate
Best JavaScript Validator Online
This is an Online Validator to validate your Javascript. You can copy and paste, or Load from url or You can upload your JavaScript file.
🌐
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.
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.

🌐
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 ...
🌐
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 …
🌐
ValidateJavaScript
validatejavascript.com
Validate JavaScript
ValidateJavaScript is an online validating (or linting) tool that will automatically find basic errors and help prevent potentially destructive bugs in JavaScript & JSX (React.js) code.
🌐
Minifier
minifier.org › javascript-validator
JavaScript Validator | Online JS Code Checker
JavaScript Validator checks and validates JS code, simplifying debugging and improving scripts by identifying syntax errors, warnings, and possible issues.
🌐
Jqueryvalidation
jqueryvalidation.org
jQuery Validation Plugin | Form validation with jQuery
This jQuery plugin makes simple clientside form validation easy, whilst still offering plenty of customization options. It makes a good choice if you’re building something new from scratch, but also when you’re trying to integrate something into an existing application with lots of existing ...