News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

User fields - add a class for JS validation - OR any other type of validation

Started by ingmardevos, November 20, 2014, 02:34:32 AM

Previous topic - Next topic

ingmardevos

The past few hours I have been trying to add classes to individual VM user fields.  I want to add a class to use JS validation (eg class="validate-numeric").
Problem 1: There is no option in the user field manager, nor is their a numeric field type that only accepts numbers (f.i for house number). I need to be able to automatically validate the house number, so no mistakes are made in the shipping.

Furthermore we need to build in a check on fields that have a certain value, such as "fill in this field".
Why? This is because a company can give Christmas presents to their employees, they provide us with the basic information, and we then send the login to the employee. However, many fields that are imported are empty - and in an automatic import this is not allowed. Thus we put "fill in this field". The employee then logs in the first time, and has to change their address. We want them not to overlook these pre-filled standard value fields, and throw an error when they don't replace it.

I perfectly know how to do this with regex and the build in Joomla form validation, however it seems that the JS simply does not work.

So f.i. for the house number: (a field called "hnr")

var elem = jQuery('#hnr_field');
        elem.attr('class', "validate-numeric");


Or for other fields that contain "fill in" value:

indow.addEvent('domready', function(){
   document.formvalidator.setHandler('fillin', function(value) {
      regex=/[a-z]+\s[a-z]+\W/;
      return regex.test(value);
   });
});

var elem = jQuery('#address_1_field');
        elem.attr('class', "validate-fillin");



Any ideas, suggestions?

Milbo

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

ingmardevos

True, there are numbers such as 23a. That's why we have a seperate field for house nr suffix.

I managed to do it this using the standard build in Joomla validation.

Just add this JS:
Using build in validation types:
jQuery(function($){$( "#hnr_field" ).addClass( "validate-numeric" );}) (only numbers)

And using custom validation:
jQuery(function($){$( "#last_name_field" ).addClass( "validate-letters" );})
jQuery(function($){$( "#first_name_field" ).addClass( "validate-letters" );})
jQuery(function($){$( "#company_field" ).addClass( "validate-letters" );})
jQuery(function($){$( "#address_1_field" ).addClass( "validate-letters" );})
jQuery(function($){$( "#city_field" ).addClass( "validate-letters" );})
jQuery(function($){$( "#hrn_toev_field" ).addClass( "validate-toevoeging" );})
jQuery(function($){$( "#zip_field" ).addClass( "validate-postcode" );})


The custom validations are defined using this JS:

var $j = jQuery.noConflict();
jQuery(function($){
window.addEvent('domready', function(){
   document.formvalidator.setHandler('letters', function(value) {
      regex=/^[a-zA-Z\s]*$/;
      return regex.test(value);
   });
});
})

jQuery(function($){
window.addEvent('domready', function(){
   document.formvalidator.setHandler('postcode', function(value) {
      regex=/\d\d\d\d../;
      return regex.test(value);
   });
});
})

jQuery(function($){
window.addEvent('domready', function(){
   document.formvalidator.setHandler('toevoeging', function(value) {
      regex=/./;
      return regex.test(value);

   });
});
})


Using these custom defined definitions you can use any REGEX, so it can practically validate anything you want, for any field made in VM.

I think it would be a great addition if the vm userfields would have the option to add a class to the field, that way it will be super easy to use any kind of validation, or f.i. have users fill in their email addresses twice - and validate to each other, to rule out that they put in the wrong one.

Just posting my solution for this project, in case it will ever help someone.

This customization can be added to any file that is loaded for the VM account and address pages.

Milbo

VM2 and VM3 use the joomla validation natively, so the only thing missing is the possibility to add your own class to a userfield. But good idea, yes.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

mkb-buerokonzepte.de

Hello I have just the same problem with the class name.

I want to check my form fields to a minimum length.

How or where do I add the individual form fields a class?

Best Regards

rayge

I am also in the same boat. I need to validate One specific field but only if it is filled in. the code posted doesn't seem to work for me. any help would be GREATLY appreciated.
I Use: Joomla 3.4.1 & VM 3.0.9