Share via

Validate ISRC

Peter Volz 1,295 Reputation points
2023-09-18T11:40:56.6233333+00:00

Hello

I need to validate the user entry to make sure it's a valid ISRC code:

https://isrc.ifpi.org/en/

I found a php script here:

https://github.com/supl/isrc-validator/commit/d4d8209b538803599a680a1e997906dbf446f174

Easy to get it working in vb.net? Only the upper first part is needed to validate.

It's 12 chars max, without -

Thanks :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

KOZ6.0 6,810 Reputation points
2023-09-18T14:06:44.0466667+00:00

I asked ChatGPT.


Q:Convert PHP to C#

class Validator
{
    public function validate($isrc)
    {
        return (bool) preg_match('/[A-Z]{2}[A-Z0-9]{3}[0-9]{7}/i', $isrc);
    }
}

A:Here's the equivalent C# code for the PHP Validator class:

using System;
using System.Text.RegularExpressions;

public class Validator
{
    public bool Validate(string isrc)
    {
        Regex regex = new Regex("[A-Z]{2}[A-Z0-9]{3}[0-9]{7}", RegexOptions.IgnoreCase);
        return regex.IsMatch(isrc);
    }
}

In this C# code:

We define a Validator class with a Validate method, just like in the PHP code.

We use the Regex class from the System.Text.RegularExpressions namespace to work with regular expressions in C#.

The regular expression pattern "[A-Z]{2}[A-Z0-9]{3}[0-9]{7}" matches the same pattern as in your PHP code. We added RegexOptions.IgnoreCase to make the regular expression case-insensitive, equivalent to the /i flag in PHP's regex.

We use the IsMatch method to check if the input string isrc matches the regular expression pattern. The result is returned as a boolean value, just like in your PHP code.

This C# code will perform the same validation as your PHP code for ISRC codes.


I have modified the code to test.

using System;
using System.Collections.Generic;
using System.Diagnostics;

static class Program {

    public static IEnumerable<(string isrc, bool expected)> ValidIsrcTestData() {
        yield return ("QM-6QV-14-00007", false);
        yield return ("HKC840500162", true);
    }

    [STAThread]
    static void Main() {

        foreach (var args in ValidIsrcTestData()) {
            var validator = new Validator();
            bool actual = validator.Validate(args.isrc);
            Debug.Assert(actual == args.expected);
        }

   }
]

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.