Mobile Validator in a Rest Api query

Asked

Viewed 325 times

0

Today I use SMS sending system for our customers who are registered on Tiny ERP, but I am in need of a mobile validator to be able to send scheduled SMS that are saved in a local bank.

The problem is that the system accepts to record wrong or fixed numbers. I saw on the forum a validator made on c#, but I can’t perform very well.

Follow the section of the validator:

if (!String.IsNullOrEmpty(cliente.contatos[0].celularddd) && !String.IsNullOrEmpty(cliente.contatos[0].celularnumero))
{
    var celular = "55" + cliente.contatos[0].celularddd + cliente.contatos[0].celularnumero;
    celular = celular.Replace(" ", String.Empty);//quando vem nesse parâmetro o valor do celular = "5515999999999"
    if ( Convert.ToInt32(celular.Substring(5)) == 9)
    { 
        log.celular_cliente = celular;

When the system arrives in the if ( Convert.ToInt32(celular.Substring(5)) == 9) he does not recognize the number 9 and does not continue the process.

1 answer

0


The Substring(int startIndex) method returns the rest of the string from the given position (startIndex).

See https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8

Example:

"5515999999999".Substring((5)) == "99999999"

Update 1#

See the regular expression documentation on
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.8

Example of c# function to validate that starts with 55 and that 5 digit is a 9.

NumeroValido("55(12)94567-8910")
//true

NumeroValido("55(12)14567-8910")
//false


    public static Boolean NumeroValido(string numero)
    {

        if (String.IsNullOrEmpty(numero)) return false; //ignora

        Regex regex = new Regex(@"[^\d]"); //expressão regular para excluir todos os caracteres excepto dígitos

        numero = regex.Replace(numero, ""); //remove não digítos

        //@"^55\d\d9" expressão regular de validação

        if (Regex.IsMatch(numero, @"^55\d\d9"))
        {
            Console.WriteLine($"O número {numero} é válido");

            return true;
        }
        else
        {
            Console.WriteLine($"O número {numero} é invválido");

            return false;
        };

    }
  • I get it, there’s a validator I can use?

  • You can use regex, you know the numbering rules?

  • Ah yes, I’ll test the regex, thank you

  • Have an example? because it comes from an Api

  • Show an example of a number in your country and explain its composition

  • No Brasil o celular (12)34567-8910, however for sending has the country code that is 55, except that in the case the string comes "5512345678910" and I need the fifth number to be 9

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.