Text field left padding

Hi, I have a text form field that needs to be 5 characters long, I figure that a on field exit event would be the best choice to have a client script that will achieve this.

 

I have done a search and found on MSDN a Pad Left (MyString.PadLeft(20, "-"c) string function that will do what I want but when I put it into the Client Script event and test it the string isn't padded, no errors are displayed (either when the event fires or when deploying the solution), my client script is as follows.

 

txtMatterNo = PadLeft(5,'0')

I have also tried

 

txtMatterNo = txtMatterNo.PadLeft(5,'0')

 

Each with no apparent effect.

 

Has anyone got any ideas on how to have a left padding on a text field to always make the field 5 characters.  I don't want to force the user to always enter the padding themself, I would prefer that the system does the padding automatically.

 

Thanks,

 

 

Matt

Tagged:

Comments

  • Client script has nothing to do with C#.

     

    Try this function:

     

    // left padding s with c to a total of n chars
    function padding_left(s, c, n) {
        if (! s || ! c || s.length >= n) {
            return s;
        }
    
        var max = (n - s.length)/c.length;
        for (var i = 0; i < max; i++) {
            s = c + s;
        }
    
        return s;
    }
    
  • Thanks Jerome,

     

    This doesn't seem to be working for me, I have tried placing this into both the On field exit event code and also the When changed event visual script within a Code activity, I even attempted to add it as a toolbar item in a visual script but all of them seem to have no effect on the input field.

     

    In the code activity and event code I have the following:

    // left padding s with c to a total of n chars
    function padding_left(s, c, n) 
    {    
    if (! s || ! c || s.length >= n) 
    {        
    return s;    
    }    
    var max = (n - s.length)/c.length;    for (var i = 0; i < max; i++) 
    {        
    s = c + s;    
    }    
    return s;
    }
    ConveyancerPackagesData1.txtMatterNo = padding_left(ConveyancerPackagesData1.txtMatterNo,"0",5)
    

     

     I have also set the text field to Field is dependent and Field has dependents (assuming that this is required to make the field update once the field is exited?).

     

    Is there anything else that I need to be doing?

     

    And thanks for the info about Client Scripts, I wasn't aware of this and have always been under the impression they are C# but now you have pointed it out it does say JScript.

     

     

    Matt

  • You must first of all understand that client 'script' and server 'script' are two completely different things, and "ne'er the twain shall meet" (they are completely incompatible).

     

    1. Client script is script. It is just text, and parsed and executed at run time. This is JScript (a bit like JavaScript, and mostly identical, and similar syntax to Java). I believe masochists can also use VBScript (a bit like VB) but I'll not go there.

     

    1. Server 'script' is not script at all. It is C# code. This is compiled. Calling it 'script' has caused massive confusion for many, including yourself by the sound of it.

     

    1. Visual 'script' is not script either. Like server script, this is badly and confusingly named. These are 'activities' that are essentially .net functions being executed (it seems that way to me, although those who understand it better tell me it's not quite like that. I think it should be, however.)

     

    So, back to the issue at hand. This is JScript / JavaScript. I claim no responsibility as I grabbed it from the first Google search on left padding in JScript. Google is the developer's friend. Embrace it, and go hunting.

     

    Looking at the code, although I have not tried it myself, it seems as though it should work. It is actually quite compact, and seems fairly clever. Two very good signs. Comments would be good too - the third 'good sign' of decent code. Firstly, I would stop any sever code, 'when changed' events, and dependencies. Then just execute the client script and see if it works.

     

    Bear in mind that to 'get' and 'set' client fields, you must use GetField() and SetField(). Look in the Designer help file for the syntax.

  • Great, thanks for pointers. It can be very confusing when they have many things named very similar.

    Ill have a look and see what I can find.

    Matt