You can reach the various inputs with jQuery, using selectors like input[name=campaignName]
See example and fiddle below.
$(document).ready(function() {
$("#submitCreateCampaignForm").submit(function(e) {
var $form = $(this);
var errors = 0;
var campaignName=$form.find("input[name=campaignName]").val()
if (!errors && campaignName == "") {
alert("nono, campaignType empty");
errors++;
}
var fromDate=$form.find("input[name=fromDate]").val()
if (!errors && fromDate == "" ) {
alert("nono, fromDate empty");
errors++;
}
if (!errors && !isValidDate(fromDate)) {
alert("nono, fromDate "+fromDate+" not a valid date");
errors++;
}
// ...
if (errors) e.preventDefault();
});
});
// http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript/14794066#14794066
function isInt(value) {
if (isNaN(value)) return false;
var f=parseFloat(value);
return f==(f|0);
}
// http://stackoverflow.com/questions/276479/javascript-how-to-validate-dates-in-format-mm-dd-yyyy
function isValidDate(date) {
var matches = /^(\d{2})[-\/](\d{2})[-\/](\d{4})$/.exec(date);
if (matches == null) return false;
var d = matches[2];
var m = matches[1] - 1;
var y = matches[3];
if(y>2100 || y<1900) return false; // accept only recent & not too far years
var composedDate = new Date(y, m, d);
return composedDate.getDate() == d &&
composedDate.getMonth() == m &&
composedDate.getFullYear() == y;
}
https://jsfiddle.net/ukfw3xo9/15/
This only tests two field for being not empty, a valid date, but you can test whatever you want with simple js
The !errors &&
in front of the tests is just to alert only for the first error, not to show all errors at once (means "if you have not already reported an error and ...").
UPDATE
Set red fields:
https://jsfiddle.net/ukfw3xo9/16/
CSS:
.red {background-color:red;}
or (border only)
.red {border:1px solid red;}
JAVASCRIPT:
$(document).ready(function() {
$("#submitCreateCampaignForm").submit(function(e) {
var $form = $(this);
var errors = 0;
$form.find("input").removeClass("red");
var campaignName=$form.find("input[name=campaignName]").val()
if (!errors && campaignName == "") {
$form.find("input[name=campaignName]").addClass("red");
errors++;
}
var fromDate=$form.find("input[name=fromDate]").val()
if (!errors && fromDate == "" ) {
$form.find("input[name=fromDate]").addClass("red");
errors++;
}
if (!errors && !isValidDate(fromDate)) {
$form.find("input[name=fromDate]").addClass("red");
errors++;
}
// ...
if (errors) e.preventDefault();
});
});
// http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript/14794066#14794066
function isInt(value) {
if (isNaN(value)) return false;
var f=parseFloat(value);
return f==(f|0);
}
// http://stackoverflow.com/questions/276479/javascript-how-to-validate-dates-in-format-mm-dd-yyyy
function isValidDate(date) {
var matches = /^(\d{2})[-\/](\d{2})[-\/](\d{4})$/.exec(date);
if (matches == null) return false;
var d = matches[2];
var m = matches[1] - 1;
var y = matches[3];
if(y>2100 || y<1900) return false; // accept only recent & not too far years
var composedDate = new Date(y, m, d);
return composedDate.getDate() == d &&
composedDate.getMonth() == m &&
composedDate.getFullYear() == y;
}
You can now remove all the "!errors && " (all cells will become red not only the first one).
This code is a bit ugly, just to show the concept, a cleaner way is to get the inputs only once:
var $fromDateInput=$form.find("input[name=fromDate]");
var fromDateValue=$fromDateInput.val();
if (fromDateValue == "" ) {
$fromDateInput.addClass("red");
errors++;
}