I am using javascript to check if the fields in my form are not empty. Everything works great, it also checks on an amount of characters in a field.
But now I would like that the email field will be checked if it contains an '@' and at least one '.' otherwise it should give the (same) error message.
I hope someone can help me!
Fiddle:
http://jsfiddle.net/43vLT/1/
function isValid(fldId,len) {
var fld = $("#"+fldId);
var val = fld.val();
window.console && console.log(fld,fldId)
if (val == "" || (len && val.length<len)) {
fld.css("borderColor","red");
return false;
}
fld.css("borderColor","black");
return true;
}
$(function() {
$("form").on("submit",function() {
var errormessage = [];
if (!isValid('naam', 10)) {
errormessage.push("You forgot to fill in your name ");
}
if (!isValid('email')) {
errormessage.push("You forgot to fill in your email ");
}
if (errormessage.length>0) {
window.console && console.log("error present");
$(".error-messages").html(errormessage.join('<br/>')).fadeIn();
return false;
}
window.console && console.log("success");
return true;
});
});