Chaito
Chaito

Reputation: 23

RUC Paraguayan validator

I want to make a validation for RUC from Paraguay.

Now I can validate this expression ^[0-9+#-]*$ and got this result 1234567-8, but I also have to validate this values:
1234567 -> only numbers
1234567A-8 -> number follows by a only one letter follow by -8.

Upvotes: 2

Views: 441

Answers (2)

padaleiana
padaleiana

Reputation: 1074

Two alternatives: the very long and very short versions. Both assumes that RUC (without check digit) has a length of 6 to 8 characters.

  1. The very long version:
    Here, the only alphanumeric character that could appear at the end of the document is the capital A.
^(?:(?!0)\d{6,8}|(?!0)\d{5,7}A)(?:-\d)?$
^           # Start of string
  (?:         # Start of first non-capturing group:
    (?!0)       # Negative lookahead (?!): avoid starting with zero
    \d{6,8}     # Digits (an equivalent is [0-9]); 6 to 8 times
      |           # Or
    (?!0)       # Negative lookahead (?!): avoid starting with zero
    \d{5,7}A    # Digits (an equivalent is [0-9]); 5 to 7 times, followed by an 'A'
  )           # End of first non-capturing group
  (?:         # Start of second non-capturing group:
    -\d         # A hyphen followed by a single digit
  )?          # End of second non-capturing group; '?' = 'optional': may or may not appear
$           # End of string

Version with capturing groups:

^((?!0)\d{6,8}|(?!0)\d{5,7}A)(-\d)?$

  1. The very short version:
    In this case, only capital A or B or C or D can be the alphanumeric characters that could appear at the end of the document.
^[1-9]\d{4,6}[\dA-D](?:-\d)?$

# or

^[1-9]\d{4,6}[\dA-D](-\d)?$
^           # Start of string
  [1-9]       # Digits from 1 to 9 (avoid starting with zero)
  \d{4,6}     # Digits (an equivalent is [0-9]); 4 to 6 times
  [\dA-D]     # Digits or capital A/B/C/D
  (?:         # Start of non-capturing group:
    -\d         # A hyphen followed by a single digit
  )?          # End of second non-capturing group; '?' = 'optional': may or may not appear
$           # End of string

Both tested in Javascript.



Fact: The letter "A" was added to the document numbers that were mistakenly duplicated.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627371

You can use

^[0-9]+[A-Z]?(-[0-9])?$

See the regex demo.

Details:

  • ^ - string start
  • [0-9]+ - one or more digits
  • [A-Z]? - an optional uppercase ASCII letter
  • (-[0-9])? - an optional sequence of a - and a digit
  • $ - end of string.

Upvotes: 0

Related Questions