Validation field

  • totallnet
  • totallnet's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • Posts: 322
  • Thank you received: 11

totallnet created the topic: Validation field

Hi

How can i validate this script

Field : cnpj
function validarCNPJ(cnpj) {
 
    cnpj = cnpj.replace(/[^\d]+/g,'');
 
    if(cnpj == '') return false;
     
    if (cnpj.length != 14)
        return false;
 
    // Elimina CNPJs invalidos conhecidos
    if (cnpj == "00000000000000" || 
        cnpj == "11111111111111" || 
        cnpj == "22222222222222" || 
        cnpj == "33333333333333" || 
        cnpj == "44444444444444" || 
        cnpj == "55555555555555" || 
        cnpj == "66666666666666" || 
        cnpj == "77777777777777" || 
        cnpj == "88888888888888" || 
        cnpj == "99999999999999")
        return false;
         
    // Valida DVs
    tamanho = cnpj.length - 2
    numeros = cnpj.substring(0,tamanho);
    digitos = cnpj.substring(tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
            pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0))
        return false;
         
    tamanho = tamanho + 1;
    numeros = cnpj.substring(0,tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
            pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1))
          return false;
           
    return true;
    
}

Thanks in advance

Kazuo

Kazuo
Level I support
Official Breezing Forms Support
Skype: totallnet 18pm-22pm -3UTC
===============================================
Satisfied? Consider a membership!
===============================================
Follow us on Twitter
===============================================
#232405
  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: Validation field

Hello,

To write custom validation script based on the script you've attached do as follows.

Go to Properties of the element where CNPJ should be entered. In Validation section, mark the Required checkbox and set Type to Custom.
Then in the code area below put the following code:
function ff_fieldname_validation(element, message)
{
var cnpj = element.value;

// Elimina CNPJs invalidos conhecidos
if (cnpj == "00000000000000" || 
        cnpj == "11111111111111" || 
        cnpj == "22222222222222" || 
        cnpj == "33333333333333" || 
        cnpj == "44444444444444" || 
        cnpj == "55555555555555" || 
        cnpj == "66666666666666" || 
        cnpj == "77777777777777" || 
        cnpj == "88888888888888" || 
        cnpj == "99999999999999") {
             if (message=='') message ="Informe um CNPJ válido. \n"
             ff_validationFocus(element.name);
             return message;
        }
// Valida DVs
    var tamanho = cnpj.length - 2
    var numeros = cnpj.substring(0,tamanho);
    var digitos = cnpj.substring(tamanho);
    var soma = 0;
    var pos = Number(tamanho) - 7;
    for (i = Number(tamanho) ; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
            pos = 9;
    }
    var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(0))  {
             if (message=='') message ="Informe um CNPJ válido. \n"
             ff_validationFocus(element.name);
             return message;
        }
         
    tamanho = tamanho + 1;
    numeros = cnpj.substring(0,tamanho);
    soma = 0;
    pos = tamanho - 7;
    for (i = tamanho; i >= 1; i--) {
      soma += numeros.charAt(tamanho - i) * pos--;
      if (pos < 2)
            pos = 9;
    }
    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
    if (resultado != digitos.charAt(1))  {
             if (message=='') message ="Informe um CNPJ válido. \n"
             ff_validationFocus(element.name);
             return message;
        }
    return '';

} // ff_fieldname_validation
NOTE: In the code above replace every instance of fieldname with the Name of that field.

Test whether this is validating CNPJ numbers correctly and let me know whether it is working properly for you.

Regards,

Mihaela
#232408
The following user(s) said Thank You: totallnet
  • totallnet
  • totallnet's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • Posts: 322
  • Thank you received: 11

totallnet replied the topic: Validation field

Thanks for your solution..

Could you help me in one more issue...
I used the code above with masked input...
With masked dont work.. without masked work..
Im using masked input library from your tutorials.

Thanks

Kazuo

Kazuo
Level I support
Official Breezing Forms Support
Skype: totallnet 18pm-22pm -3UTC
===============================================
Satisfied? Consider a membership!
===============================================
Follow us on Twitter
===============================================
#232409
  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: Validation field

Hello again,

I'm glad to help you.

Please give me an example of CNPJ number with masked input which doesn't pass the validation and example of number that does pass it.

Regards,

Mihaela
#232411
  • totallnet
  • totallnet's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • Posts: 322
  • Thank you received: 11

totallnet replied the topic: Validation field

01.513.052/0001-24

Kazuo
Level I support
Official Breezing Forms Support
Skype: totallnet 18pm-22pm -3UTC
===============================================
Satisfied? Consider a membership!
===============================================
Follow us on Twitter
===============================================
#232412
  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: Validation field

Does a number like 01513052000124 pass the validation?

If it does, you just need to add a javascript code to the script that I've sent you to remove
all characters except numbers from cnpj variable.
You should add the code after this line:
var cnpj = element.value;

Let me know whether you have managed to do so.

Regards,

Mihaela
#232414
Moderators: ForumSupporttomeperica
Time to create page: 0.080 seconds

New Support System

Dear Customers,

Users with active subscription have support priority which will be dona via ServiceDesk (My Account > Raise a ticket).
FREE Support topics will be answered when AND if there is time.

You can check support guidelines here : here

Regards,
Crosstec Team

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99€

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here