No space validation in text field.

Hi All Developers,

i need to create custom text field, and apply validation, that fields value should not have any space.

if user put space in the field and try to save the record, then system popup the error message.

any body have any idea? how we can achieve.

Thanks in advance  

Parents
  • Thanks Andre for your help.

    I have review the above guide, it has only example for require values.

    actually i need to give validation, if any custom fields values has white spacing then record unable to save and system give the error message. 

    could you please suggest me regarding above

  • Hi


    Following the tutorial previously shared, you can have a working example with these steps: 



    1 - Created a new field in Studio to add the validation, on my test I've named it testfield and added it to the record view 

    https://www.screencast.com/t/gaJk4hIh

    2 - Create a custom error message label file: 

    Extension/application/Ext/Language/en_us.nospace_error_message.php

    <?php
    
    $app_strings['ERROR_NOSPACE_MESSAGE'] = 'Please remove empty spaces';

    3 - Extend the CreateView controller to add the validation by creating the file 

    custom/modules/Accounts/clients/base/views/create/create.js

    ({
        extendsFrom: 'CreateView',
        initialize: function (options) {
    
            this._super('initialize', [options]);
    
        //add custom message key
            app.error.errorName2Keys['nospace_message'] = 'ERROR_NOSPACE_MESSAGE';
    
                //add validation tasks
            this.model.addValidationTask('check_testfield_spaces', _.bind(this._doValidateTestFieldSpaces, this));
    
        },
    
            _doValidateTestFieldSpaces: function(fields, errors, callback) {
            // Validate testfield for spaces
            const testfield_c = this.model.get('testfield_c');
            if (testfield_c.includes(' ')) {
    
            errors['testfield_c'] = errors['testfield_c'] || {};
            errors['testfield_c'].nospace_message = true;
    
            }
    
    
            callback(null, fields, errors);
        },
    })

    4 - Extend the RecordView controller to add the validation by creating the file 

    custom/modules/Accounts/clients/base/views/record/record.js

    ({
        /* because 'accounts' already has a record view, we need to extend it */
             extendsFrom: 'AccountsRecordView',
             initialize: function (options) {
    
            this._super('initialize', [options]);
    
        //add custom message key
            app.error.errorName2Keys['nospace_message'] = 'ERROR_NOSPACE_MESSAGE';
    
                //add validation tasks
            this.model.addValidationTask('check_testfield_spaces', _.bind(this._doValidateTestFieldSpaces, this));
    
        },
    
            _doValidateTestFieldSpaces: function(fields, errors, callback) {
            // Validate testfield for spaces
            const testfield_c = this.model.get('testfield_c');
            if (testfield_c.includes(' ')) {
    
            errors['testfield_c'] = errors['testfield_c'] || {};
            errors['testfield_c'].nospace_message = true;
    
            }
    
    
            callback(null, fields, errors);
        },
    })

    5 - Run a quick repair and rebuild. 


    After this, the validation should work like this: 






    Can you give it a try to see if this matches your requirement? 

    I hope this helps. 

    André

  • Hi Andre,

    i need to add one more condition on the above code (no space allow code)  i need to add the condition that only numbers are allowed in the same text field, i have tried diff diff. code but i have not yet achieve the possible solution, could you please help me

  • Hi 

    You could do this by adding another message and another condition to your validation: 

    Take the create.js as an example: 

    custom/modules/Accounts/clients/base/views/create/create.js

    ({
        /* because 'accounts' already has a record view, we need to extend it */
             extendsFrom: 'CreateView',
             initialize: function (options) {
    
            this._super('initialize', [options]);
    
             //add custom message key
            app.error.errorName2Keys['nospace_message'] = 'ERROR_NOSPACE_MESSAGE';
            app.error.errorName2Keys['numbers_message'] = 'ERROR_NUMBERS_MESSAGE';
    
             //add validation tasks
            this.model.addValidationTask('check_testfield_spaces', _.bind(this._doValidateTestFieldSpaces, this));
    
        },
    
            _doValidateTestFieldSpaces: function(fields, errors, callback) {
            // Validate testfield for spaces
            const testfield_c = this.model.get('testfield_c');
            if (testfield_c.includes(' ')) {
    
            errors['testfield_c'] = errors['testfield_c'] || {};
            errors['testfield_c'].nospace_message = true;
    
            }
            // Validate testfield for numbers
            
            if (!/^\d+$/.test(testfield_c)){ 
                errors['testfield_c'] = errors['testfield_c'] || {};
                errors['testfield_c'].numbers_message = true;
            }
    
    
            callback(null, fields, errors);
        },
    })




Reply Children
No Data