mercredi 9 janvier 2019

Trying to get a prompt to limit the first three character of a prompt to letters only

So i have this code that i want to be able to limit what can be said in a prompt box using javascript. I am using a do and while statement to push out a prompt loop until the prompt meets the requirement, i am only allowed to use javascript. I want the first three characters of the prompt box to be only letters, however i think i have a problem with my regex! any help would be greatly appreciated.

   function myFunction() {
  var userInput = new Array();
  var letters = /^[A-Za-z]+$/;
  do {
     userInput = prompt('Enter course code');
     if (userInput.length != 7) {
        alert("The input must contain 7 characters");
     }
     var userInput3 = userInput.substring(0, 3);
     if (userInput3 != /[A-Za-z]+$/){
        alert("The first 3 characters must be letters");
     }
  }
  while (userInput.length != 7 && userInput3 != /[A-Za-z]+$/){
  }

}

Aucun commentaire:

Enregistrer un commentaire