dimanche 5 décembre 2021

Validating ID Number C#

I am working on this method that validates a student Id number. The credentials of the ID number are; the first character has to be 9, the second character has to be a 0, there can not be any letters, and the number must be 9 characters long. The method will return true if the student id is valid. When I go to test the method manually through main it comes out as true, even when I put in a wrong input. In my code, I have the if statements nested, but I originally did not have them nested. What is a better way to validate the input to align with the credentials of the ID number? Would converting the string into an array be more ideal?

 public static bool ValidateStudentId(string stdntId)
        {
            string compare = "123456789";
            if (stdntId.StartsWith("8"))
            {
                if (stdntId.StartsWith("91"))
                {
                    if (Regex.IsMatch(stdntId, @"^[a-zA-Z]+$"))
                    {
                        if (stdntId.Length > compare.Length)
                        {
                            if (stdntId.Length < compare.Length)
                            {
                                return false;
                            }
                        }
                    }
                }
            }

screenshot of my main. The method was made in a class called student.

Aucun commentaire:

Enregistrer un commentaire