Forums

Verificador RUT

Zyraj 20 Jul, 2018
I'm posting this because it could really be a time saver for someone.
Where I live we use a checksum at the end of our ID number, this checksum is obtained with an algorithm called Modulus 11. With this code you can add RUT validation to the text fields, but you could also break CF6 so back up the files involved before doing anything and proceed at your own risk!

Edit this file:
libraries/cegcore2/assets/js/g2.forms.js

And add the following function right after the email rules function (line 17 in my file):
jQuery.fn.form.settings.rules.rut = function(value){
if(value.match(/^\d{1,2}\.\d{3}\.\d{3}\-(\d|k|K)$/)){
var dv = value.slice(-1);
var rut=value.replace(/\.|\-/g,"");
var j = 2;
var sum = 0;
var i = (rut.length-1);
while (i--) {
if(j>7) j=2;
sum += rut.charAt(i) * j;
j++;
}
var dv2;
dv2=(11-(sum%11));
if (dv2==10) dv2="K";
if (dv2==11) dv2=0;
if(dv2==dv){
return true;
}else{
return false;
}
}else{
return false;
}
};

Also modify this file to add the button under the validation tab in the text field options:
libraries/cegcore2/admin/extensions/chronofc/views/field_text/field_text_config.php
change line 103 where it says "two fields" to "three fields" so it looks like this:
<div class="three fields">

Then in line 118 right after the div block for URL validation add this:
<div class="field">
<div class="ui checkbox toggle red">
<input type="hidden" name="Connection[views][<?php echo $n; ?>][validation][rut]" data-ghost="1" value="">
<input type="checkbox" class="hidden" name="Connection[views][<?php echo $n; ?>][validation][rut]" value="true">
<label><?php el('RUT'); ?></label>
</div>
</div>

I strongly suggest adding a specific mask for the RUT to avoid problems with the dots and hyphen since THIS validation will ask for them (E.g. 20.234.234-K). This can be easily done by adding this at line 220:
<option value="mask:99.999.999-&">RUT</option>

That's it! Hope it helps someone someday.
healyhatman 21 Jul, 2018
Any changes you make to those files will be overwritten the next time you update CF. Why don't you just add the custom validation in as a custom validation instead of messing with the core files?

https://www.chronoengine.com/faqs/74-chronoforms/chronoforms6/5293-validating-fields

Also be careful with input masks - they can cause problems with Android browsers.
healyhatman 21 Jul, 2018
ALSO ALSO: You don't need to add the custom mask to the core files. In the Design tab under "Extra Attributes" just use
data-inputmask:'mask':'99.999.999-&'
Zyraj 21 Jul, 2018
Well it's a hack of course they will go away, it's up to you to keep your mods if you want, now if you want custom validation the code is perfectly functional in a jscript element but then again I rather have just a button to do it for me since it's used almost in all systems around here, and finally that's kinda the point of using open source plugins, to adapt the system to your needs, else I would be using brainfuck for my apps 😛

Again its up to you to use masks or not, it wont affect the validation.

Cheers!
healyhatman 21 Jul, 2018
Step 1: Create your custom JS validation block
Step 2: Click the "save" button to the right of the block's name
Step 3: You now have a reusable block you can quickly add to any form
Zyraj 21 Jul, 2018
Lets agree to disagree since my subordinate would never learn those steps and I need him to get things done easily, tuff luck.
Though I have to admit the save feature is absolutely new to me, it really comes handy, about the mask I actually went further and created an alias in inputmask.js because of specific needs, I'm not gonna get this guy to learn regexp, so my suggestion about adding the code just seemed like an easy tip to format the input, but again, if you don't want to modify core files its just about a copy/paste.
Thanks for all your tips anyway! it's always good to have an orthodox version.
Also masks been working perfect to me both in Android and iOS but I guess older versions may have trouble, I'll keep an eye on that!
healyhatman 23 Jul, 2018
Sure no problem. If someone facing your specific use case finds your tips helpful that's great. Just had to put it out there though so other people wanting custom validation, custom input mask, or easily reusable code blocks doesn't think they need to go through modifying core files to do it🙂
This topic is locked and no more replies can be posted.