0

I want to have accurate form field validation for NEAR protocol account addresses.

I see at https://docs.near.org/docs/concepts/account#account-id-rules that the minimum length is 2, maximum length is 64, and the string must either be a 64-character hex representation of a public key (in the case of an implicit account) or must consist of "Account ID parts" separated by . and ending in .near, where an "Account ID part" consists of lowercase alphanumeric symbols separated by either _ or -.

Here are some examples.

The final 4 cases here should be marked as invalid (and there might be more cases that I don't know about):

  • example.near
  • sub.ex.near
  • something.near
  • 98793cd91a3f870fb126f66285808c7e094afcfc4eda8a970f6648cdf0dbd6de
  • wrong.near.suffix (INVALID)
  • shouldnotendwithperiod.near. (INVALID)
  • space should fail.near (INVALID)
  • touchingDotsShouldfail..near (INVALID)

I'm wondering if there is a well-tested regex that I should be using in my validation.

Thanks.

P.S. Originally my question pointed to what I was starting with at https://regex101.com/r/jZHtDA/1 but starting from scratch like that feels unwise given that there must already be official validation rules somewhere that I could copy.

I have looked at code that I would have expected to use some kind of validation, such as these links, but I haven't found it yet:

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 1
    It is not clear what matches should be invalid and why – anubhava Jun 07 '22 at 20:25
  • 1
    there's a regex in the platform spec that might help `^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$` found here https://nomicon.io/DataStructures/Account – amgando Jun 08 '22 at 00:36

3 Answers3

1

Something like this should do: /^(\w|(?<!\.)\.)+(?<!\.)\.(testnet|near)$/gm

Breakdown

^                 # start of line
(
  \w              # match alphanumeric characters
  |               # OR
  (?<!\.)\.       # dots can't be preceded by dots
)+
(?<!\.)           # "." should not precede:
\.                # "."
(testnet|near)    # match "testnet" or "near"
$                 # end of line

Try the Regex out: https://regex101.com/r/vctRlo/1

code
  • 5,690
  • 4
  • 17
  • 39
1

The pre-release (v0.6.0-0) version of the JS SDK comes with a built-in accountId validation function:

const ACCOUNT_ID_REGEX =
  /^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/;

/**
 * Validates the Account ID according to the NEAR protocol
 * [Account ID rules](https://nomicon.io/DataStructures/Account#account-id-rules).
 *
 * @param accountId - The Account ID string you want to validate.
 */
export function validateAccountId(accountId: string): boolean {
  return (
    accountId.length >= 2 &&
    accountId.length <= 64 &&
    ACCOUNT_ID_REGEX.test(accountId)
  );
}

https://github.com/near/near-sdk-js/blob/dc6f07bd30064da96efb7f90a6ecd8c4d9cc9b06/lib/utils.js#L113

Feel free to implement this in your program too.

Ryan
  • 22,332
  • 31
  • 176
  • 357
idea404
  • 399
  • 1
  • 2
  • 10
0

If you want to match word characters only, separated by a dot:

^\w+(?:\.\w+)*\.(?:testnet|near)$

Explanation

  • ^ Start of string
  • \w+ Match 1+ word characters
  • (?:\.\w+)* Optionally repeat . and 1+ word characters
  • \. Match .
  • (?:testnet|near) Match either testnet or near
  • $ End of string

Regex demo

A bit broader variant matching whitespace character excluding the dot:

^[^\s.]+(?:\.[^\s.]+)*\.(?:testnet|near)$

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70