How to Validate text box entry ends data from .txt file

Fairwinds 1 Reputation point
2021-08-10T18:41:46.007+00:00

Newbie question:

I am new to using Visual Studio (I was decent with MS Access) and I'm trying to validate user entries into a textbox, and there are a number of conditions, but I'm only really struggling with the last part.

I need to ensure that the entry ends with any of the strings that are stored in a .txt file (which supervisors can edit at any time to keep up with future updates)

Can I compare against the file or do I need to somehow open it first? Do I need to store them in an array before I can compare against them? How do I write my code to use the file/array like a wildcard?

I assume the comparison part could look similar to this:

If (txtInput Like "*[<.txtfile>]" = True) Then

Developer technologies | VB
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-08-10T19:57:02.9+00:00

    Try something like this:

    If File.ReadLines(txtFilePath).Any(Function(line) txtInput.TrimEnd.EndsWith(line.TrimEnd, StringComparison.CurrentCultureIgnoreCase)) Then
    
       . . .
    
    End If
    

  2. Fairwinds 1 Reputation point
    2021-08-10T23:04:49.843+00:00

    Sadness. I was wrong. I am still getting stumped, and now I realize that it has to match exactly with the entire portion of the txtInput(user entry) that follows "00", which is another layer of complexity I wasn't expecting.

    i.e. with LEAD in the text file, "...00LEAD" would be accepted, but "...00SLEAD", "...00LEADS", "...00EAD", and "..00LEA" would all fail.


  3. Fairwinds 1 Reputation point
    2021-08-11T05:00:45.413+00:00

    I figured it out eventually, and I was thinking about it wrong. I needed to trim my entry data down to just the part I needed to compare, then treat each line in the text file as an .equals test against the trimmed entry as a whole.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.