# Regex test for numeric asset names

**URL:** <https://forums.counterparty.io/t/regex-test-for-numeric-asset-names/1737>\
**Category:** 3rd Party\
**Created:** [January 7, 2016, 12:41pm UTC](https://forums.counterparty.io/t/regex-test-for-numeric-asset-names/1737 "2016-01-07T12:41:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JPJA](https://yyz2.discourse-cdn.com/flex004/user_avatar/forums.counterparty.io/jpja/32/1324_2.png) [@JPJA](https://forums.counterparty.io/u/JPJA)\
**Post date:** [January 7, 2016, 12:41pm UTC](https://forums.counterparty.io/t/regex-test-for-numeric-asset-names/1737/1 "2016-01-07T12:41:41Z")

</div>

Is there a simple regex test for validating **numeric** asset names?  
Alternatively, a function or algorithm?

_FYI, regular alphabetic asset names can be validated like this:_

> [@Regex test if valid asset name](https://forums.counterparty.io/t/regex-test-if-valid-asset-name/1640):
>
> The regular expression /^[B-Z][A-Z]{3,11}$/ tests whether a string is a valid asset name. To allow for lowercase characters, use /^[b-zB-Z][a-zA-Z]{3,11}$/ /\b[B-Z][A-Z]{3,11}\b/ finds the first valid uppercase asset name within the string. Add g to find all asset names; /\b[B-Z][A-Z]{3,11}\b/g … You can test a regex on [[regex101.com](http://regex101.com)][1]. … For example, to find all assets within a string with PHP, you may use this code: $text = 'My favorite XCP assets are FLDC, BITCRYSTALS and …

---

<div class="post-metadata">

**Author:** ![something](https://yyz2.discourse-cdn.com/flex004/user_avatar/forums.counterparty.io/something/32/240_2.png) [@something](https://forums.counterparty.io/u/something)\
**Post date:** [January 7, 2016, 1:44pm UTC](https://forums.counterparty.io/t/regex-test-for-numeric-asset-names/1737/2 "2016-01-07T13:44:03Z")

</div>

I think you could look at the CP and CW source code, there should be a validation regex for alphanumeric assets in there.

Edit: maybe this helps

1. Protocol spec: integers between 26^12 + 1 and 256^8 (inclusive), prefixed with ‘A’ ([http://counterparty.io/docs/protocol\_specification/](http://counterparty.io/docs/protocol_specification/))

2. Validation: [https://github.com/CounterpartyXCP/counterparty-lib/blob/master/counterpartylib/test/fixtures/vectors.py#L1299](https://github.com/CounterpartyXCP/counterparty-lib/blob/master/counterpartylib/test/fixtures/vectors.py#L1299)

If asset name starts with `A`, cut off the leftmost character `A` and check if the value of remaining string is larger than the lower bound and equal or smaller than the higher bound. If true, it’s valid.

---

<div class="post-metadata">

**Author:** ![JPJA](https://yyz2.discourse-cdn.com/flex004/user_avatar/forums.counterparty.io/jpja/32/1324_2.png) [@JPJA](https://forums.counterparty.io/u/JPJA)\
**Post date:** [January 26, 2016, 9:50am UTC](https://forums.counterparty.io/t/regex-test-for-numeric-asset-names/1737/3 "2016-01-26T09:50:51Z")

</div>

I think I got it here. Tested with all edge cases and seems to work, at least if the min and max assumptions are correct.

I did not use BigInt because one does not always want to import a library.

```javascript
function isValidAsset(asset) {
	if (isValidAlphabeticAsset(asset) || isValidNumericAsset(asset)) return true;
	return false;
}

function isValidAlphabeticAsset(asset) {
	//4-12 chars, cannot start with A
	//A few old ones have 13 or 14 chars
	if (asset.match(/^[B-Z][A-Z]{3,11}$/) == null) return false;
	return true;
}

function isValidNumericAsset(asset) {
	//'A' followed by a really large number
	//Min = 26^12+1 = 95,428,956,661,682,177
	//Max = 2^64-1 = 18,446,744,073,709,551,615
        while (asset.substring(0,2) == 'A0') asset = asset[0] + asset.substring(2);
 	if (asset.length>21) return false;
	if (asset.length<18) return false;
	if (asset[0] != 'A') return false;
	if (asset.substring(1).match(/[^0-9]/) != null) return false;
	if (asset.length==18 && asset.substring(1,9)<95428956) return false;
	if (asset.length==18 && asset.substring(1,9)==95428956 && asset.substring(9)<661682177) return false;
	if (asset.length==21 && asset.substring(1,10)>184467440) return false;
	if (asset.length==21 && asset.substring(1,10)==184467440 && asset.substring(10)>73709551615) return false;
	return true;
}

```
