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 OverflowForm Validation using JavaScript Functions
HTML/JavaScript: Simple form validation on submit - Stack Overflow
Why should I use javascript to create a form validation when I can use html?
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.comVideos
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;
}
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.
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>