I'm trying to make my code easier to understand by making it more correct and maintainable.
Here's the code I'm trying to simplify:
$(document).ready(function(){
$('.opretbruger').on('click', function(){
if (opretbruger() === true){
}
});
});
var opretbruger = function(){
if($('#brugernavn').val() === ''){
alert('remember Email');
return false;
}
if($('#pass1').val() === ''){
alert('remember password');
return false;
}
if($('#pass2').val() === ''){
alert('remember password');
return false;
}
if($('#fornavn').val() === ''){
alert('remember your name');
return false;
}
if($('#efternavn').val() === ''){
alert('remember your last name');
return false;
}
return true;
};
Here's my attempt at simplifying it:
$(function () {
var $login = $("#login"),
$pwd = $("#pass"),
$usr = $("#brugernavn");
$login.on("submit", function (event) {
var msg = "Husk ",
usr = ($usr.val().trim() !== ""),
pwd = ($pwd.val().trim() !== "");
msg += !usr ? " brugernavn" : "";
msg += !usr && !pwd ? " og " : "";
msg += !pwd ? " kodeord" : "";
(pwd && usr) || alert(msg);
return (pwd && usr);
});
});
overskulig
mean? \$\endgroup\$