User input validation

I need to validate user input for course code. The code should be:

  • Length 7
  • The first 3 characters are lowercase letters
  • Last four digits from 1000 to 9999
  • There are no spaces or symbols.

So far, I have 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 alpha + no spaces / characters.

+3


source to share


4 answers


Below is an example to help you do this 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");

    }

}

      

+2


source


First of all, you are clearly not using Java because of "var" and String.slice (). But this advice will still apply.

There are two approaches for this. A fun way and the way your professor wants you to do it.

An interesting way would be to use the regex operator to make sure it matches. I would recommend using this site to figure it out: http://regexr.com/



The way your professor probably wants you to do this is to iterate over it 1 char at a time and check if each char matches its position.

There might also be some javascript stuff you can do to make this more easily, but I can't help you.

0


source


It looks like Javascript, not Java. I decided to configure your program to work according to the 4 rules that you provided. It looked almost complete, just a few confirmations were missing. When you .slice()

a string

, you want to make sure that you pass two arguments in parentheses - 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);
}
      

Run codeHide result


As you already know, the while loop checks for user error, so most of the checking is canceled. Created a function that checks for uppercase and whole numbers. And a bit of research has found a couple of nice regular expressions.

Hope it helps.

0


source


You can use Java regular expressions , follow these links:

Regular expressions

Java Regex

0


source







All Articles