# Problem using IndexOf

**URL:** <https://forum.xojo.com/t/problem-using-indexof/60559>\
**Category:** General\
**Created:** [January 18, 2021, 7:36pm UTC](https://forum.xojo.com/t/problem-using-indexof/60559 "2021-01-18T19:36:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dean\_Davidge](https://forum.xojo.com/user_avatar/forum.xojo.com/dean_davidge/32/244_2.png) [@Dean\_Davidge](https://forum.xojo.com/u/Dean_Davidge)\
**Post date:** [January 18, 2021, 7:36pm UTC](https://forum.xojo.com/t/problem-using-indexof/60559/1 "2021-01-18T19:36:52Z")

</div>

I wrote this function to validate the complexity of a password entered by a user. If I enter a password with a leading zero, j returns zero as it should. If I enter a zero anywhere else in the password, j returns -1. How can I work around for this or what did I do wrong?

> Private Function PasswordRequires() as Boolean  
> var b As String = “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWZYZ”  
> var c As String  
> var j As Integer  
> var a(3) as Integer  
> for i as Integer = 0 to tfPassword.Text.Length-1  
> c = tfPassword.Text.Middle(i,1)  
> j = b.IndexOf(i,c, ComparisonOptions.CaseSensitive)  
> if j \> 35 then // capital letter  
> a(0) = 1  
> elseif j \> 9 then // lower case letter  
> a(1) = 1  
> ElseIf j \> -1 then // number  
> a(2) = 1  
> Else // other character  
> a(3) = 1  
> end if  
> Next  
> j = a(0) + a(1) + a(2) + a(3)  
> if tfPassword.Text.Length \> 7 and j \> 2 then Return True
> 
> var s As String  
> if Session.LanguageCode.Left(2) = “en” then  
> s = “Invalid password.” + EndOfLine + "Passwords must be 8 or more characters in length and contain 3 of these 4 "+\_  
> “items: upper case letter, lower case letter, number, special character.”  
> Else  
> s = “Contraseña invalida.” + EndOfLine + "Las contraseñas deben tener 8 o más caracteres de longitud y contener 3 "+\_  
> “de estos 4 elementos: letra mayúscula, letra minúscula, número, carácter especial.”  
> end if  
> MessageBox s
> 
> End Function

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [January 18, 2021, 7:40pm UTC](https://forum.xojo.com/t/problem-using-indexof/60559/2 "2021-01-18T19:40:43Z")

</div>

I use RegEx to validate password strength.

```auto
Protected Function IsPasswordStrongEnough(sPassword as String) as Boolean
  // Check that password is strong enough *for me*
  // This is 7 chars, at least 1 letter, at least one symbol
  
  dim rx as new RegEx
  rx.SearchPattern = "^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{7,}$"
  
  dim rxm as RegExMatch = rx.Search(sPassword)
  return (rxm <> nil)
End Function

```

Update: The problem in your code is that the further along the password test you go, the further into the source string it starts. You should use better variable names, it will make your code significantly easier to debug.

` j = b.IndexOf(i,c, ComparisonOptions.CaseSensitive)`

Jump out as wrong when swapping in decent variable names.

`iTestIndex = sSourceString.IndexOf(iLoopIteration, sTestCharacter, ComparisonOptions.CaseSensitive)`

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 18, 2021, 7:48pm UTC](https://forum.xojo.com/t/problem-using-indexof/60559/3 "2021-01-18T19:48:01Z")

</div>

Look at this line:

```auto
j = b.IndexOf(i,c, ComparisonOptions.CaseSensitive)

```

You are searching b starting at subsequent characters after the first character in your password. In other words, the call looks like this:

```auto
j = b.IndexOf(1, "0", ComparisonOptions.CaseSensitive)

```

Since “0” is at position 0, this will return -1.

---

<div class="post-metadata">

**Author:** ![Dean\_Davidge](https://forum.xojo.com/user_avatar/forum.xojo.com/dean_davidge/32/244_2.png) [@Dean\_Davidge](https://forum.xojo.com/u/Dean_Davidge)\
**Post date:** [January 18, 2021, 8:54pm UTC](https://forum.xojo.com/t/problem-using-indexof/60559/4 "2021-01-18T20:54:55Z")

</div>

> [@Tim\_Parnell](#):
>
> Update: The problem in your code is that the further along the password test you go, the further into the source string it starts. You should use better variable names, it will make your code significantly easier to debug.

Thanks Tim,  
Not used to the new syntax yet. Obviously  
j = b.IndexOf(i,c, ComparisonOptions.CaseSensitive)  
should have been  
j = b.IndexOf(0,c, ComparisonOptions.CaseSensitive)

I started programming in 1961 in Fortran where the only integer variables were i, j, k, l, m and a few others. I’m afraid it’s a habit that is hard to break.
