I have been studying the if
and else if
statements and trying to implement them on a project.
I was able to apply it to a single field on my form but had no success while trying to apply it to multiple fields.
I hope that I can find someone with more understanding to take a look and help me understand better what I do not understand.
Ok, so what I am trying to achieve is a validation of user input on my form.
I use AppleScript for the form and submission is made to the spreadsheet but I am trying to use the if
and else if
statements to check if a user is providing information that is already recorded on the spreadsheet to prevent multiple inputs.
I do not know if I have to provide the entire code of my app script but,
-1
, -5
, -6
, and -7
stand for the row numbers in the spreadsheet where the user inputs are saved.
(-1 USERNAME), (-5 ID NUMBER), (-6 PHONE NUMBER), (-7 EMAIL).
My script checks if user input does not match with any record in these rows on the spreadsheet and rejects it if there is a match and asks them to provide another one.
To check a single field, I use the code below and it works fine:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};
To check the other fields, I use the below code but does not work:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -5 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","ID No. taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -6 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Phone taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -7 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Email taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1, -5, -6, -7 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};