Replace commas with dots

Replace commas with dots

paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

Hi,

I have some fields where I use "float" numbers (e.g. 2.81) and when I copy values from a source the numbers will be provided with a comma (like 2,81).

Is there a solution to replace the comma while/after paste it into the field?

Thanks in advance

Patrick

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    You could listen for the paste event on the input element in question (assuming this is in Editor, you can get the input element using field().input()). Then modify the value as required.

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    That sounds interesting and I created this example:

    <input type="text" onpaste="myFunction(this.value)" id="myText" value="Paste something in here">

    function myFunction(p1) { // alert(p1); alert( document.getElementById("myText").value ); }

    But unfortunately it gave me only the orginal value and not the pasted value back.

    Do you have any idea and also how to combine it with datatables?

    Patrick

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    You might have to add a short setTimeout to let the value be set and then read the value and you can modify it that way. Even a setTimeout of 0mS might be enough.

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    Cool this try works pretty well:
    <input type="text" placeholder="Paste text" onPaste="var e=this; setTimeout(function(){myFunction(e.value);}, 0);">

    function myFunction(p1) { alert(p1); }

    But how can I call the function with datatables? Is it possible to add it to single fields (to use it e.g. just for 2 of 8 fields)

    Patrick

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    Answer ✓

    I tried the below code and it seems to work.

      editor.field( 'name' ).input().on( 'paste',  function () {
       setTimeout(function() { 
         var pasteField = editor.field( 'name' ).val();
         pasteField = pasteField.replace(/,/g, '.');
         editor.field( 'name' ).val(pasteField);
       }, 0);
      });
    

    Not sure this is the best way (I'm not the most proficient at JS :-). Just change the 'name' to the name of the Editor field of interest.

    Kevin

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    Looks good to me :smile:.

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0
    edited February 2017

    I really love datatables, but you guys are really awesome. Your code works pretty good :-)

    Is it possible to use the function for two or more fields like this:

    siteEditor.field( ['sellPrice', 'purchasePrice'] ).input().on( 'paste',  function () {
              setTimeout(function() {
                var pasteField = siteEditor.field( 'sellPrice' ).val();
                pasteField = pasteField.replace(/,/g, '.');
                siteEditor.field( 'sellPrice' ).val(pasteField);
              }, 0);
             });
    

    Cheers,

    Patrick

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    Answer ✓

    There are a few ways to do it, but I'm afraid that isn't one of them! field() doesn't take an array option.

    You could use jQuery().add() with the input elements from the two fields to create a single jQuery instance that contains both nodes.

    Or save the function into a variable and pass that variable in as the callback for the event handler for both elements.

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    ok, but I have only two fields where I need it that's why I will use this way:

            siteEditor.field( 'sellPrice' ).input().on( 'paste',  function () {
              setTimeout(function() {
                var pasteField = siteEditor.field( 'sellPrice' ).val();
                pasteField = pasteField.replace(/,/g, '.');
                siteEditor.field( 'sellPrice' ).val(pasteField);
              }, 0);
             });
             siteEditor.field( 'purchasePrice' ).input().on( 'paste',  function () {
              setTimeout(function() {
                var pasteField = siteEditor.field( 'purchasePrice' ).val();
                pasteField = pasteField.replace(/,/g, '.');
                siteEditor.field( 'purchasePrice' ).val(pasteField);
              }, 0);
             });
    

    Thank you very much for your support.

    Patrick

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    Answer ✓

    Code reuse:

    var pasteHandler = function ( fieldName ) {
      return function () {
      setTimeout(function() {
        var pasteField = siteEditor.field( fieldName ).val();
        pasteField = pasteField.replace(/,/g, '.');
        siteEditor.field( fieldName ).val(pasteField);
      }, 0);
     };
    };
    
    siteEditor.field( 'sellPrice' ).input().on( 'paste', pasteHandler( 'sellPrice' ) );
    siteEditor.field( 'purchasePrice' ).input().on( 'paste', pasteHandler( 'purchasePrice' ) );
    

    Allan

  • paintitblackpaintitblack Posts: 60Questions: 20Answers: 0

    As always it works pretty well. Thank you very much for your support. I really love datatables :-)

This discussion has been closed.