Search code examples
javascriptgoogle-chromegoogle-chrome-extensiondna-sequence

Check if user input string contains only uppercase and/or lowercase letters


I'm creating a Chrome extension that converts a sequence of nucleotides into their corresponding amino acids, and I'm running into some problems with validating user input.

I ultimately want to check to make sure the user has input only uppercase or lowercase characters (no numbers, special characters, spaces, etc.). If any unacceptable characters are found, it alerts the user of invalid input. Otherwise, the program proceeds as normal and no error/alert is thrown.

Below is the JavaScript code I have currently. I have tried a few variations based off of this post but I can't seem to get anything to work for me. With the code I have right now, when I try to submit valid input ("ATG" for example), the alert pops up instead of the program proceeding as normal and correctly outputting "M" (see this link for reference).

document.getElementById('button').onclick = function () {

    console.log(document.querySelectorAll("textarea"))
    var n_seq = document.querySelectorAll("textarea")[0].value.toUpperCase();


    // validate user input below
    if (!/^[a-zA-Z]+$/i.test(n_seq)); {
        alert("invald input");
        return;
    }


document.getElementById("amino_acid_seq").value = translateInput(codonDict, n_seq);

}

Solution

  • This ended up working for me:

    // only letters are acceptable characters, spaces, special characters, and 
    // numbers will throw an error
    if (!/^[A-Z]+$/i.test(n_seq)) {
        alert("Invalid input!");
        return;
    }