Search code examples
javascripthtmlregexregex-greedy

Validation of Distinguish Name ( DN ) Code in Regex JavaScript


I want to validate an DN code where

All segments must occur in the sequence : cn=,ou=,o=,o=swift A comma (,) must separate all segments.

Maximum 100 lower case characters

No spaces are allowed

Number of segments: minimum two and maximum 10

Segment Requirement

Segment follows the BIC format validation but 8 chars are allowed.

Segment consists of the following characters:

minimum two characters.

maximum 20 characters.

alphanumeric characters: a to z (lower case only), 0 to 9, and the special character '-' If segment has value as only numbers will consists of maximum two digits.

I have tried with this following regex:

^((CN=([a-zA-Z0-9-,])))?((((?:CN|OU)=[a-zA-Z0-9-,]+,?)+),)?((o=([a-zA-Z]{4})([a-zA-Z]{2})(([0-9a-zA-Z]{1})([0-9a-np-zA-NP-Z]))(|([xX]{3})|)+,?)+)?((o=([swift])+,{1})+)$

But it's working with following code: cn=harsha,ou=treasury,ou=treasury,o=dazppxxx,o=swift,

but not with

cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift"

It is also accepting

cn=test,cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift," which is invalid.

Please help !!!


Solution

  • Here is an attempt with several test strings added to test your validation rules:

    var strings = [
      'cn=harsha,ou=treasury,ou=treasury,o=dazppxxx,o=swift',
      'cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift',
      'cn=test,cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift',
      'cn=t1,cn=t2,ou=t3,ou=t4,ou=t5,ou=t6,ou=t7,ou=t8,ou=t9,ou=t10,ou=t11,o=dazppxxx,o=swift',
      'cn=a,ou=b,ou=c,o=dazppxxx,o=swift',
      'cn=more-than-20-characters,ou=treasury,ou=treasury,o=dazppxxx,o=swift',
      'cn=more-than-100-total1,cn=more-than-100-total2,ou=more-than-100-total3,ou=more-than-100-total4,o=dazppxxx,o=swift'
    ];
    var re = /^(?=[^,]+(,[^,]+){1,9}$)(?=.{1,100}$)(cn=([a-z0-9\-]{2,20}|[0-9]{1,2}),)+(ou=([a-z0-9\-]{2,20}|[0-9]{1,2}),)+(o=([a-z0-9\-]{2,20}|[0-9]{1,2}),)+o=swift$/
    
    strings.forEach(function(str) {
      var result = re.test(str);
      console.log(str + '\n  ==> ' + result);
    });

    Console output:

    cn=harsha,ou=treasury,ou=treasury,o=dazppxxx,o=swift
      ==> true
    cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift
      ==> true
    cn=test,cn=test,ou=treasury,ou=treasury,o=dazppxxx,o=swift
      ==> true
    cn=t1,cn=t2,ou=t3,ou=t4,ou=t5,ou=t6,ou=t7,ou=t8,ou=t9,ou=t10,ou=t11,o=dazppxxx,o=swift
      ==> false
    cn=a,ou=b,ou=c,o=dazppxxx,o=swift
      ==> false
    cn=more-than-20-characters,ou=treasury,ou=treasury,o=dazppxxx,o=swift
      ==> false
    cn=more-than-100-total1,cn=more-than-100-total2,ou=more-than-100-total3,ou=more-than-100-total4,o=dazppxxx,o=swift
      ==> false
    

    Explanation:

    • the regex starts with a positive lookahead to test for 1-9 commas, e.g. 2 to 10 segments
    • the second lookahead tests for max 100 chars total
    • followed by (cn=([a-z0-9\-]{2,20}|[0-9]{1,2}),)+, which means cn has either 2-20 chars of (lowercase, numbers, dash), or a number up to 2 digits
    • followed by the same for ou and o
    • followed by o=swift

    You stated that the third example is invalid. Based on your stated rules I do not understand why it violates your stated rules