Reputation: 39
I have to validate a user input for a course code. The code must be:
So far I've got something like this:
var inputMessage = "Please enter valid course code";
var userInput = " ";
userInput = prompt (inputMessage);
while (userInput.length !=7 || userInput.slice(-4)<1000 || userInput.slice(-4)>9999)
{
alert ("Invalid Course Code. Please try again");
userInput = prompt(inputMessage);
}
Any help would be great. I am struggling with the alpha + the no spaces/symbols.
Upvotes: 3
Views: 563
Reputation: 1402
This looks to be Javascript rather than Java. I decided to tweak your program to work under the 4 rules you had provided. It looked to be nearly complete, it was just missing a couple of validations. When you .slice()
a string
you want to make sure you are passing two arguments within the parenthesis - the first and last place you want to 'slice' your string.
Here is the Javascript code:
function isUpperCase(str) {
return str === str.toUpperCase();
}
function isInteger(value) {
return /^\d+$/.test(value);
}
var inputMessage = "Please enter valid course code";
var userInput = "";
userInput = prompt (inputMessage);
while (
userInput.length !=7 || //length
!isUpperCase(userInput.slice(0,3)) || //first 3 UpperCase
!isInteger(userInput.slice(3,7)) || //last 4 is integer
userInput.slice(3,7)<1000 || //last 4 is at least 1000
userInput.slice(3,7)>9999 || //last 4 is maximum 9999
userInput.match(/[^A-Za-z0-9\-_]/) //no space / spec chars
)
{
alert ("Invalid Course Code. Please try again");
userInput = prompt(inputMessage);
}
As you are already probably aware, the while loop is checking for user error, so most of the validation is reversed. Created a function that checks for uppercase, and integers. And a bit of research found a couple of nice regular expressions work.
Hope this helps.
Upvotes: 0
Reputation: 2114
First of all, you clearly aren't using Java because of "var" and String.slice(). But this advice will still apply.
There are two approaches I would take for this. The fun way, and the way your professor wants you to do it.
The fun way would be to use a regex statement to verify that it matches. I'd recommend using this website to figure that out: http://regexr.com/
The way your professor probably wants you to do it is to iterate over it 1 char at a time and check if each char is what its supposed to be given its position.
There might also be some javascript stuff you can do to do it more easily, but I can't help you there.
Upvotes: 0
Reputation: 520
Below example will help you to do that in Java.
import java.util.Scanner;
import java.util.regex.Pattern;
public class RegexSample {
public static boolean checkPass(String s) {
Pattern p = Pattern.compile("[A-Z]{3}[0-9]{4}");
return p.matcher(s).find();
}
public static void main(String[] args) {
String inputMessage = "Please enter valid course code";
String userInput = " ";
Scanner input = new Scanner(System.in);
System.out.println(inputMessage);
userInput = input.nextLine();
while (userInput.length() != 7 || !checkPass(userInput)) {
System.out.println("Invalid Course Code. Please try again");
userInput = input.nextLine();
}
System.out.println("Input Validated");
}
}
Upvotes: 2