I need to know why this function is not appending my error message when referenced to a variable. I tried returning some value like "false" but still not working. Please help me learn something here.
function checkName(el)
{
if (/^[a-zA-Z ]+$/.test(el.val()))
{
return null
}
return el
}
$(document).ready(function()
{
$('button[name=btn-submit]').on('click', function(e)
{
e.preventDefault();
var err = {
fn: checkName($('input[name=fn]')),
mn: checkName($('input[name=mn]')),
ln: checkName($('input[name=ln]'))
}
var isSubmit = function(){
for (var key in err) {
if (err.hasOwnProperty(key)) {
if(err[key] !== null)
{
var parent_div = err[key].parent().closest('div');
parent_div.append('<p id="p-error" class="error-msg">test</p>');
}
}
}
}
console.log(isSubmit);
//
{on the same line of the structure, or on the next line) makes it easier to read and debug your code. In JavaScript, the overwhelming convention is to have the{on the same line, but that's just convention. (Just whatever you do, don't put a line break afterreturnbefore what you're returning; automatic semicolon insertion will bite you.)