Search code examples
javascriptpowerapps

javascript string validator failing to validate correctly using startsWith()


I have this very simple validator

if (!toString(aAccountNumber).startsWith('A')) {
      alert('AAccountNumber must start with the capital letter A');
      return;
    }

I then call this when pressing a button on a web page but not matter what I type in the input field, even starting with an A like A112233 it still fails and puts up the alert

What am I missing here?


Solution

  • This is not the right way to use toString, it must be called on the object to test, you were probably just getting undefined.

    if (!aAccountNumber.toString().startsWith('A')) {
      alert('AAccountNumber must start with the capital letter A');
    }
    

    Also note that a text input value is already a string so you probably don't need to call toString at all.