DEV Community

Hugo Borges
Hugo Borges

Posted on

Validação de Chassi (VIN) em C#

using System;
using System.Text.RegularExpressions;

public static class ChassiValidator
{
    // Método principal para validar o chassi (VIN)
    public static bool ValidarChassi(string chassi)
    {
        // 1 - Verifica se o chassi começa com "0"
        if (Regex.IsMatch(chassi, @"^0", RegexOptions.IgnoreCase))
        {
            return false;
        }

        // 2 - Remove espaços do chassi
        chassi = chassi.Replace(" ", "");

        // 3 - Verifica se há repetição consecutiva de mais de 6 vezes a partir do 4º dígito
        if (Regex.IsMatch(chassi, @"^.{3,}([0-9A-Za-z])\1{5,}", RegexOptions.IgnoreCase))
        {
            return false;
        }

        // 4 - Verifica se o chassi contém os caracteres proibidos: i, I, o, O, q, Q
        if (Regex.IsMatch(chassi, @"[iIoOqQ]", RegexOptions.IgnoreCase))
        {
            return false;
        }

        // 5 - Verifica se os quatro últimos caracteres são numéricos
        if (!Regex.IsMatch(chassi, @"[0-9]{4}$", RegexOptions.IgnoreCase))
        {
            return false;
        }

        // 6 - Verifica se o chassi tem exatamente 17 caracteres
        if (chassi.Length != 17)
        {
            return false;
        }

        // Se todas as verificações passaram, o chassi é válido
        return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay