简体   繁体   中英

Matching user input with REGEX

Maybe the subject line is incorrect, but here is my question:

I am trying to see if the user input is a valid email address, or if there is an input at all at the first place. if none of the above, then i want to loop the question requesting the answer again, until i get a valid answer(in this case, email address). Below is the code i have written, which was working until i added REGEX testing.

    function emailPrompt() {

   var ui = SpreadsheetApp.getUi();
  var entry = ui.prompt("REQUIRED: Email Address", "Please type the email address you want the exported excel spreadsheet to be emailed to:"+"\n\n", ui.ButtonSet.OK);
  var button = entry.getSelectedButton();
  var response = entry.getResponseText();

  var sum = 1;
  for(var i=0;i<sum;i++){

  var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  var matchRegex = regex.test(response);
      if(response == ""||response == " "|| response != matchRegex) {
        if(!matchRegex) { ui.alert("Invalid Email Address")}
       ui.prompt("REQUIRED: Email Address", "Please type the email address you want the exported excel spreadsheet to be emailed to:"+"\n\n", ui.ButtonSet.OK);

      sum++;
    } else {

    sum--;

    }
  }
  return response;
  Logger.log(response);
}

Specifically, if the input is incorrect/invalid email address, i inserted another if statement to alert the user. I am positive i am messed the code somewhere in the REGEX matching/testing. Any help would be much appreciated. TIA

Your regex statement is ok. It tests and returns a boolean. Your first if statement is a little redundant. response == ""||response == " "|| response != matchRegex response == ""||response == " "|| response != matchRegex Most of these are already tested by the regex statement and the last one should never be false as you are comparing a string to a boolean .

EDIT: Additionally, the response variable is never update with the new prompt data (Code updated).

function emailPrompt() {

  var ui = SpreadsheetApp.getUi();
  var entry = ui.prompt("REQUIRED: Email Address", "Please type the email address you want the exported excel spreadsheet to be emailed to:"+"\n\n", ui.ButtonSet.OK);
  var button = entry.getSelectedButton();
  var response = entry.getResponseText();

  var sum = 1;
  for(var i=0;i<sum;i++){

    var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var matchRegex = regex.test(response);

    if(!matchRegex) { 
      ui.alert("Invalid Email Address");

      //Ask for email again and set new response.
      var responseItem = ui.prompt("REQUIRED: Email Address", "Please type the email address you want the exported excel spreadsheet to be emailed to:"+"\n\n", ui.ButtonSet.OK);
      response = responseItem.getResponseText();

      sum++;
    } 

    //sum--; this isn't needed to stop the loop.

    if(sum > 3) //You shouldn't go on forever... Stop after a few tries?
      break;
  }
  Logger.log(response); //Moved above return so this code runs.
  return response;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM