Skip to main content
edited body
Source Link
private bool ValidateNIC(string nic)
{
    if (String.IsNullOrWhiteSpace(nic))
    {
        ShowMessage("NIC is null");
        return false;
    }

    if (nic.Length != 10)
    {
        ShowMessage("Length should be 10");
        return false;
    }

    var lastChar = nic[9];
    
    // Case sensitive
    if (lastChar != 'v' && lastChar != 'x')
    {
        ShowMessage("Last character should be 'v' or 'x'");
        return false;
    }

    var otherChars = nic.Substring(0, 89);

    if (otherChars.Any(x => !Char.IsNumber(x)))
    {
        ShowMessage("All characters except the last one should be numbers");
        return false;
    }

    return true;
}

private void ShowMessage(string message)
{
    MessageBox.Show(message);
}

Edit: Forgot to mention that the character check is case-sensitive.

private bool ValidateNIC(string nic)
{
    if (String.IsNullOrWhiteSpace(nic))
    {
        ShowMessage("NIC is null");
        return false;
    }

    if (nic.Length != 10)
    {
        ShowMessage("Length should be 10");
        return false;
    }

    var lastChar = nic[9];

    if (lastChar != 'v' && lastChar != 'x')
    {
        ShowMessage("Last character should be 'v' or 'x'");
        return false;
    }

    var otherChars = nic.Substring(0, 8);

    if (otherChars.Any(x => !Char.IsNumber(x)))
    {
        ShowMessage("All characters except the last one should be numbers");
        return false;
    }

    return true;
}

private void ShowMessage(string message)
{
    MessageBox.Show(message);
}
private bool ValidateNIC(string nic)
{
    if (String.IsNullOrWhiteSpace(nic))
    {
        ShowMessage("NIC is null");
        return false;
    }

    if (nic.Length != 10)
    {
        ShowMessage("Length should be 10");
        return false;
    }

    var lastChar = nic[9];
    
    // Case sensitive
    if (lastChar != 'v' && lastChar != 'x')
    {
        ShowMessage("Last character should be 'v' or 'x'");
        return false;
    }

    var otherChars = nic.Substring(0, 9);

    if (otherChars.Any(x => !Char.IsNumber(x)))
    {
        ShowMessage("All characters except the last one should be numbers");
        return false;
    }

    return true;
}

private void ShowMessage(string message)
{
    MessageBox.Show(message);
}

Edit: Forgot to mention that the character check is case-sensitive.

Source Link

private bool ValidateNIC(string nic)
{
    if (String.IsNullOrWhiteSpace(nic))
    {
        ShowMessage("NIC is null");
        return false;
    }

    if (nic.Length != 10)
    {
        ShowMessage("Length should be 10");
        return false;
    }

    var lastChar = nic[9];

    if (lastChar != 'v' && lastChar != 'x')
    {
        ShowMessage("Last character should be 'v' or 'x'");
        return false;
    }

    var otherChars = nic.Substring(0, 8);

    if (otherChars.Any(x => !Char.IsNumber(x)))
    {
        ShowMessage("All characters except the last one should be numbers");
        return false;
    }

    return true;
}

private void ShowMessage(string message)
{
    MessageBox.Show(message);
}