admin管理员组文章数量:1355589
I am trying to validate a large contact form. When the user forgets a required input field then I populate the empty variable with default text.
My current solution uses nine if
statements. Is there a better way to do it with less code?
html:
<xehases class="" id="xehases"></xehases>
var onoma = $("#fname").val();
var eponimo = $("#lname").val();
var email = $("#email").val();
var diefthinsi = $("#address").val();
var poli = $("#city").val();
var xora = $("#country").val();
var katigoriaDiafimisis = $("#AdCategory").val();
var plano = $("#plan").val();
var istoselida = $("#website").val();
var epixirisi = $("#pany").val();
var minima = $("#message").val();
var missing = ' ';
if (onoma === "") {
missing += 'Όνομα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (eponimo === "") {
missing += 'Επώνυμο ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (email === "") {
missing += 'email ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (poli === "") {
missing += 'Πόλη ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (xora === "please choose a category") {
missing += 'Χώρα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (plano === "") {
missing += 'Πλάνο ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (katigoriaDiafimisis === "") {
missing += 'Κατηγορία Διαφήμισης ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (epixirisi === "") {
missing += 'Επιχείρηση ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (minima === "") {
missing += 'Μήνυμα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
I am trying to validate a large contact form. When the user forgets a required input field then I populate the empty variable with default text.
My current solution uses nine if
statements. Is there a better way to do it with less code?
html:
<xehases class="" id="xehases"></xehases>
var onoma = $("#fname").val();
var eponimo = $("#lname").val();
var email = $("#email").val();
var diefthinsi = $("#address").val();
var poli = $("#city").val();
var xora = $("#country").val();
var katigoriaDiafimisis = $("#AdCategory").val();
var plano = $("#plan").val();
var istoselida = $("#website").val();
var epixirisi = $("#pany").val();
var minima = $("#message").val();
var missing = ' ';
if (onoma === "") {
missing += 'Όνομα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (eponimo === "") {
missing += 'Επώνυμο ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (email === "") {
missing += 'email ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (poli === "") {
missing += 'Πόλη ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (xora === "please choose a category") {
missing += 'Χώρα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (plano === "") {
missing += 'Πλάνο ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (katigoriaDiafimisis === "") {
missing += 'Κατηγορία Διαφήμισης ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (epixirisi === "") {
missing += 'Επιχείρηση ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
if (minima === "") {
missing += 'Μήνυμα ';
$("xehases#xehases").html(missing);
} else {
$("xehases#xehases").html(missing);
}
Share
Improve this question
edited Sep 3, 2017 at 8:03
asked Sep 3, 2017 at 7:15
user7038047user7038047
3
-
There is one extra
else{//proceed to ajax}
which is not bound to anyif
– brk Commented Sep 3, 2017 at 7:19 - it shouldn't matter if it is an html tag or not. I created it my own just to remember. – user7038047 Commented Sep 3, 2017 at 7:41
- I would also remend using english in your code for everything (variables, ments...), even if it is just your small hobby project (to get into the habit). If it is work related, then definitely use english, even if you and all your colleagues speak only greek. You never know who will e after you to maintain it. – Lope Commented Sep 3, 2017 at 10:49
3 Answers
Reset to default 11You can create a dict containing the form fields and the strings displayed when they are missing and iterate over the list. Also, as another response indicated, move setting the missing error message to the end and only do it once; Further, the if/else for that isn't needed if you're going to do the same thing in each case. Write the code similar to this:
// key is form input, value is displayed in missing field message
const fieldsDict = {
"fname": "Όνομα",
"lname": "eponimo",
// ...
};
let missing = "";
Object.keys(fieldsDict).forEach((field) => {
if ($("#" + field).val() === "") {
missing += fieldsDict[field] + " ";
}
});
$("xehases#xehases").html(missing);
I can see there is some duplication of the code example
$("xehases#xehases").html(missing);
This can be put only in the last. So over all you just need to build the content of missing variable.
if(onoma === "")
missing +='Όνομα ';
if(eponimo === "")
missing+='Επώνυμο ';
if(email === "")
missing+='email ';
if(poli === "")
missing+='Πόλη ';
if(xora === ""){
missing+='Χώρα ';
// and more
$("xehases#xehases").html(missing);
Something like this...
var fields = ['fname', 'lname', 'email']; // ...and so on
var errField = $('#xehases');
function submit(){
fields.forEach(function(field){
var domElem = $('#' + field);
if (domElem.val() === '') {
errField.html(errField.text() + ' ' + domElem.attr('err-msg'));
}
});
}
'err-msg' could be an attribute on input elem used for missing msg.
本文标签: javascriptHow to reduce code of multiple if statementsStack Overflow
版权声明:本文标题:javascript - How to reduce code of multiple if statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743951537a2567380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论