How can I remove leading zeros?

TEdwards
Kilo Sage

Hello all,

I have a requirement to remove the leading zeros from a string variable on a catalog item. The variable ('kronos_id_1') is populated using the onChange client script that follows. The employee_number may have up to five zeros preceding the number. Is there any way to remove them? It would not necessarily need to be onChange, as long as the result is that the zeros no longer appear after submission.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getValue('name_1');
    if (name != '') {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', name);
        gr.query(function(gr) {
            if (gr.next()) {
                g_form.clearValue('kronos_id_1');
                g_form.clearValue('email_address_1');

                g_form.setValue('kronos_id_1', gr.employee_number);
                g_form.setValue('email_address_1', gr.email);

                g_form.setReadOnly('kronos_id_1', 'true');
                g_form.setReadOnly('email_address_1', 'true');
            }
        });
    } else {
        g_form.clearValue('kronos_id_1');
        g_form.clearValue('email_address_1');
    }
}

Thank you in advance for any assistance you can provide.

1 ACCEPTED SOLUTION

If you use parseInt() it will drop the leading zeroes as well. Converting it to an integer will just remove them which is all +num is doing.

View solution in original post

8 REPLIES 8

Thank you, Kieran.

Would I incorporate that in my current client script or would it be a separate onSubmit client script? Also, can you recommend a better way of accomplishing what I am trying to do without using a GlideRecord?

You can use g_form.getReference() if it's a reference field.

g_form.getReference('field_name', functionName);

function functionName(response){
   alert(response.field_name); 
}

If you use parseInt() it will drop the leading zeroes as well. Converting it to an integer will just remove them which is all +num is doing.

Community Alums
Not applicable

Absolutely - either + or parseInt() - doing the very same thing, just the first is for lazy people (well I obviously fit in this category 😄 ). 

 

Cheers,

Joro

 

P.S. What @Elijah Aromola - CloudPires  explained about the getReference() is the easiet way to do it without GlideRecord() (bad in SCJS) otherwise AJAX but only for this....baaah nope