Field validation

I am new to using Suitecrm and I would like to know how to validate a field so that it only receives numerical data without any punctuation.
I created a NIT field in the accounts module, the field is text type but I need it to only receive numbers, I tried to create an integer type field but when saving it generates commas (,) automatically.
I hope you can help me.

Hey there,

Have you found a solution for this, by chance?

Iā€™ve spent some time and managed to scrape something together that seems to force a field to only accept numerical inputs, with a mix of jQuery/Javascript

Iā€™m not sure how clean it is, so if anyone else has any better suggestions, please let us know!


Anyways;

In the file located at: custom/modules/Accounts/metadata/editviewdefs.php

add a reference to a new custom Javascript file, ie:
image

(I have added the line ā€˜fileā€™ => ā€˜custom/modules/Accounts/custAccount.jsā€™)

If this file does not exist, feel free to copy it from
modules/Accounts/metadata/editviewdefs.php
and paste it in the /custom/ location


Create this new javascript file in the same Module folder, ie:
custom/modules/Accounts/custAccount.js

and add the following to the file:

jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, enter, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 || 
                key == 9 ||
                key == 13 ||
                key == 46 ||
                key == 110 ||
                key == 190 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};

Then, create a new extension to the Accounts Edit View at:

custom/modules/Accounts/views/view.edit.php

and add the following to the file:

<?php
class AccountsViewEdit extends ViewEdit
{
    function display() {
        //Attach the forceNumeric function onto the specified field
        $jsscript = <<<EOQ
                   <script>
                      $('#testjava_c').ForceNumericOnly();
                </script>
EOQ;
        parent::display();

        
            echo $jsscript;     //echo the script
       
    }
}

In the above contents, ā€œtestjava_cā€ is the name of my custom field
Please feel free to change this to the name of your custom NIT field


Then, in the CRM run a ā€œQuick Repair & Rebuildā€ and ā€œRebuild JS Grouping Filesā€
Then, clear browser cache

Then, the input on your field should be restricted to only numbers


Does the above work for yourself?

2 Likes

Hi,
i want to validate the field(phone_mobile) from Contact module.
So i follow your step but still it not working.
first i add this file path in custom/modules/Accounts/metadata/editviewdefs.php
ā€˜fileā€™ => ā€˜custom/modules/Accounts/custAccount.jsā€™ then i am going to path:- custom/modules/Accounts to create custAccount.js and add your js code then i am going to that path :- custom/modules/Accounts but in that path view folder is not their so i create view folder and create new file view.edit.php then paste your php code and add my field name.
Then quick and repair this but still it not working.

Hi,
I solved that problem the need to create only one file at this path:-custom/modules/Contacts/views at this given path create view.edit.php and write the code in it like:-

<?php
class ContactsViewEdit extends ViewEdit
{
    function display() {
        //Attach the forceNumeric function onto the specified field
        $jsscript = <<<EOQ
                   <script>
                   $("#phone_mobile").add("#phone_work").keypress(function (e) {
                    if (e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57)) {
                        alert('Enter Numeric value only');
                                  return false;
                   }
                 })
                      
                </script>
EOQ;
        parent::display();

        
            echo $jsscript; //echo the script
       
    }

    
}

After save this file then quick repair and rebuild from Admin.
Then it work