0

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); 

};
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Link
  • 63
  • 9
  • 2
    I notice the final `else (…) {…}` which should really be `else if (…) {…}`. Fix it first, try again see if things work. Perhaps this is your problem. – hackape Jul 29 '22 at 11:45
  • 1
    Please describe more thoroughly what "does not work" means. What result do you see? What result do you expect instead? Why do you expect that? What [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) have you done to understand the results you get (more [here](https://stackoverflow.com/questions/25385173/))? – T.J. Crowder Jul 29 '22 at 11:48
  • 1
    Please see the edit I made to the question. There's no need to boldface entire sentences; as TheMaster pointed out, ALL CAPS reads as shouting; and if you use Stack Snippets (the `<>` button in the toolbar), the snippet should be **runnable** ([details](https://meta.stackoverflow.com/questions/358992/)). Otherwise, just use code fences (see the edit). – T.J. Crowder Jul 29 '22 at 11:49
  • The script Validates the Username Field and also the ID Number field and it does not go further than that. when I try to submit the form it gets stuck at ID Number taken instead of going forward to also validate the Phone field and the Email address field. – Link Jul 29 '22 at 11:56
  • Please provide a copy of the script. – Emel Jul 29 '22 at 15:49
  • Check the return value `checkResult ` by inserting as the first line of your callback function `alert("checkResult = "+checkResult);` – TheWizEd Jul 29 '22 at 16:14

0 Answers0