So, I have the problem with implementation method for validating currency and validating isbn number. I give below my code with summary description. I believe that somebody can help me and explain which I have done bad. Validation for currency ISO
/// <summary>
/// Determines whether a specified string is a valid ISO currency symbol.
/// </summary>
/// <param name="currency">Currency string to check.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="currency"/> is a valid ISO currency
symbol; <see langword="false"/> otherwise.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown if currency is null.</exception>
public static bool IsValid(string? currency)
{
if (currency == null)
{
throw new ArgumentNullException(nameof(currency));
}
Regex regex = new Regex(@"^\d*\.?\d?\d?$");
bool y = regex.IsMatch(currency);
return y;
}
Validation for ISBN number
/// <summary>
/// Verifies if the string representation of number is a valid ISBN-10 or ISBN-13
identification number of book.
/// </summary>
/// <param name="isbn">The string representation of book's isbn.</param>
/// <returns>true if number is a valid ISBN-10 or ISBN-13 identification number of
book, false otherwise.</returns>
/// <exception cref="ArgumentNullException">Thrown if isbn is null.</exception>
public static bool IsValid(string? isbn)
{
if (isbn == null)
{
throw new ArgumentNullException(nameof(isbn));
}
int n = isbn.Length;
if (n != 10)
{
return false;
}
int sum = 0;
for (int i = 0; i < 9; i++)
{
int digit = isbn[i] - '0';
if (digit < 0 || digit > 9)
{
return false;
}
sum += digit * (10 - i);
}
char last = isbn[9];
if (last != 'X' && (last < '0'
|| last > '9'))
{
return false;
}
sum += (last == 'X') ? 10 :
(last - '0');
return sum % 11 == 0;
}
I would be grateful for some help. I cannot decide it will be better resolve by using regex or different ways are more complex. My solutions are not good enough, so I believe in some help or explanation.