Validate data in form

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

totallnet created the topic: Validate data in form

Hi

I would like use this script in my form
How can i use it?
jQuery.validator.addMethod("cpf", function(value, element) {
   value = jQuery.trim(value);
	
	value = value.replace('.','');
	value = value.replace('.','');
	cpf = value.replace('-','');
	while(cpf.length < 11) cpf = "0"+ cpf;
	var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--);
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	
	var retorno = true;
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) retorno = false;
	
	return this.optional(element) || retorno;

}, "Informe um CPF válido");
 

$(document).ready(function(){
 
   $("#meuForm").validate({
      rules: {
          cpf: {cpf: true, required: true}
      },
      messages: {
         cpf: { cpf: 'CPF inválido'}
      }
      ,submitHandler:function(form) {
         alert('ok');
      }
   });
});

Script Source

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

mihaela replied the topic: Validate data in form

Hello,

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

Go to Properties of the element where CPF 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 tmp = jQuery.trim(element.value);
    tmp = tmp.replace('.','');
	tmp= tmp.replace('.','');
	cpf = tmp.replace('-','');
	while(cpf.length < 11) cpf = "0"+ cpf;
	var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--);
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	
	var retorno = true;
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) retorno = false;
	

    if (!retorno) {
        if (message=='') message ="Informe um CPF válido. \n"
        ff_validationFocus(element.name);
        return message;
    } // if
    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 CPF numbers correctly and let me know whether it is working properly for you.

Regards,

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

totallnet replied the topic: Validate data in form

Works like a charm.. thanks

Kazuo
Level I support
Official Breezing Forms Support
Skype: totallnet 18pm-22pm -3UTC
===============================================
Satisfied? Consider a membership!
===============================================
Follow us on Twitter
===============================================
#232188
  • totallnet
  • totallnet's Avatar Topic Author
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • Posts: 322
  • Thank you received: 11

totallnet replied the topic: Validate data in form

How can i make this value unique?

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

mihaela replied the topic: Validate data in form

Hello,

Great! I'm glad that it works.

Do you by "How can i make this value unique?" mean that you want to check whether CPF number which wasn't previously submitted (unique) is entered in a form.

To check whether the value is unique requiers using AJAX. Please take a look at the following example:
crosstec.org/en/forums/3-breezingforms-f...database.html#174316 .
Here is attached an example to check for unique email address so you can use this to achieve what you want. You can also take a look at this topic crosstec.org/en/forums/3-breezingforms-f...-domain.html?start=0 .

Regards,

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

totallnet replied the topic: Validate data in form

Thanks for reply

My filed is named cpf...
I tried to follow your sample but no sucess.
Can you help me with this field?

* i fix my code and now is working..
Thanks

Kazuo
Level I support
Official Breezing Forms Support
Skype: totallnet 18pm-22pm -3UTC
===============================================
Satisfied? Consider a membership!
===============================================
Follow us on Twitter
===============================================
#232205
Moderators: ForumSupporttomeperica
Time to create page: 0.075 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