validate a text field based on a date

  • DAVIDPL
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 0

DAVIDPL created the topic: validate a text field based on a date

I need to create a script that automatically assigns a value to a text field based on a date field previously entered in the form.

Example:
1.- I enter the date of birth, if it is greater than 2013 must assign a value to category that would be GREATER

2.- Introduced date of birth between 2006 and 2012, must assign a value to the ALEVIN cache

3.- Introduced date of minor birth to 2006, the category would be lower

CODIGO SCRIPT:

$mychoice1 = ["Adulto"];
$mychoice2 = ["Alevin"];
$mychoice3 = ["Menor"];
{
if([Fecha_nacimiento]) >= "01/01/2013");{
setInterval('ff_getElementByName("Categoria_edad").value=$mychoice1',500);
}
{
else([Fecha_nacimiento]) >= "01/01/2006" and < "01/01/2012");{
setInterval('ff_getElementByName("Categoria_edad").value=$mychoice2',500);
#228028
  • Posts: 2969
  • Karma: 100
  • Thank you received: 393

mihaela replied the topic: validate a text field based on a date

Hello,

Here are the instructions to achieve what you want.

Go to your forms Advanced options > More options > Scripts > Initialization Script, set Type to Custom and paste in the following code:
function ff_codigo_init()
{  setInterval(calc, 500);
} // ff_codigo_init

function calc() {
var d1 = Date.parse(ff_getElementByName('Fecha_nacimiento').value);
var d2 = Date.parse("01/01/2013");
var d3 = Date.parse("01/01/2006");

if( d1 >= d2) {
 ff_getElementByName('Categoria_edad').value= "Adulto"; }
else if( d1 >= d3 && d1 < d2 ) { 
  ff_getElementByName('Categoria_edad').value= "Alevin"; }
else if( d1 <d3 ) { 
  ff_getElementByName('Categoria_edad').value= "Menor"; }

}
NOTE: To make the code above work as it should, Name of the date field must be 'Fecha_nacimiento' and the name of the textfield to display category must be 'Categoria_edad'.

I hope that I understood well what you wanted. I assumed that in your code it should be "01/01/2013" instead of "01/01/2012". Otherwise, for date of birth for year 2012 it wouldn't fit in any category.

Let me know if this works how you wanted.
If you have more questions, feel free to ask.

Regards,

Mihaela
#228071
The following user(s) said Thank You: DAVIDPL
  • DAVIDPL
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 0

DAVIDPL replied the topic: validate a text field based on a date

Thank you very much Mihaela for the code, it worked perfectly, you helped me a lot,

Now what I want is to improve some aspects to simplify the completion of the form by the user, basically it would be to automate the completion of some fields.

If the athlete is a minor (> 2013), when you enter the date of birth, I want you to automatically activate "Yes" the radio group, and "No" if you are of legal age. (<2013)

When the form starts, I would like it if the hidden fields appear hidden by the tutor because they appear when activated if in the radio group

Cód
function ff_copy3_Alta_licencia_deportista_init()
{ setInterval(calc, 500);
} // ff_copy3_Alta_licencia_deportista_init

function calc() {
var d1 = Date.parse(ff_getElementByName('Fecha_nacimiento').value);
var d2 = Date.parse("01/01/2013");
var d3 = Date.parse("01/01/2006");

if( d1 >= d2) {
ff_getElementByName('Categoria_edad').value= "3"; }
else if( d1 >= d3 && d1 < d2 ) {
ff_getElementByName('Categoria_edad').value= "2"; }
else if( d1 <d3 ) {
ff_getElementByName('Categoria_edad').value= "1"; }
else if( d1 <d2 ) {
ff_getElementByName('bfQuickMode3720662').value= 'off'; }[/color]

} // ff_copy3_Alta_licencia_deportista_init
#228135
Attachments:
  • Posts: 2969
  • Karma: 100
  • Thank you received: 393

mihaela replied the topic: validate a text field based on a date

Hello again,

Thank you for the kind words, I'm glad that I've helped and that it worked.

Here is the code that will fill radio group value depending on the date.
function ff_copy3_Alta_licencia_deportista_init()
{  setInterval(calc, 500);
} 

function calc() {
var d1 = Date.parse(ff_getElementByName('Fecha_nacimiento').value);
var d2 = Date.parse("01/01/2013");
var d3 = Date.parse("01/01/2006");

if( d1 >= d2) {
 ff_getElementByName('Categoria_edad').value= "Minor";
//jQuery('[name="ff_nm_rad1[]"]:checked').val() = "yes";
jQuery(document).ready(function(){
document.getElementById("ff_elem13448").checked = true;
 });  
 }
else if( d1 >= d3 && d1 < d2 ) { 
  ff_getElementByName('Categoria_edad').value= "Alevin"; }
else if( d1 <d3 ) { 
  ff_getElementByName('Categoria_edad').value= "Adulto"; 
jQuery(document).ready(function(){
document.getElementById("ff_elem13448_1").checked = true;
 });  
}

}
NOTE: The important part is to replace ff_elem13448 and ff_elem13448_1 with ids of radio group options yes and no. To get the id, right-click on the circle before yes or no option and click Inspect. You will then see the ids as shown in the picture below.


Let me know if this works how you wanted.
Regarding the part for hiding elements, can you please explain a bit more how you want it to work because I didn't understand.

Regards,

Mihaela
#228204
Attachments:
  • DAVIDPL
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 16
  • Thank you received: 0

DAVIDPL replied the topic: validate a text field based on a date

Hola Michaela

To obtain the name corresponding to the id of the joomla user table, I use the following formula:
Element properties:
Value:
<?php $user = JFactory::getUser(); return $user->get('name'); ?>

But to be able to obtain the data of the joomla custom fields, such as: (sport, club, coach), there is some way to do it, since each user has a value assigned that I want to capture in the form.

regards
#228359
  • Posts: 2969
  • Karma: 100
  • Thank you received: 393

mihaela replied the topic: validate a text field based on a date

Hello again,

Please take a look at this crosstec.org/en/forums/3-breezingforms-f...onent-3x.html#226770 to obtain the data of the joomla custom fields.

Let me know if that helped you.

Regards,

Mihaela
#228372
Moderators: ForumSupporttomeperica
Time to create page: 0.215 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription 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 for just 49! (Summer Sale)

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!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the 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