0

User copy input string: "A344 34" //string with space

expected output value in textbox: "A34434"

Problem: with below code: getting result as : "A344" //and skip the remain chars.

$('#pmField').mask('XXXXXX', {
    translation: {
        'X': {
            pattern: /[A-Za-z0-9]/, optional: true
        }
    }
});

//Input textbox.

user3711357
  • 1,425
  • 7
  • 32
  • 54

1 Answers1

0

Why don't you do this with javascript which is very simple using the code below which i have edited to get variable displayed in a text box

source: Remove spaces in a Javascript variable

<input type="text" id="mytext" name="mytext" value="">

    <script>

    var a = "A344 34";
    a=a.replace(/\s/g, "");
    alert(a);

    var test = "Hello";
     document.getElementById("mytext").value = a;
    </script>

also if you insist on using jquery, then similar solution has been implemented by @VSri58 as per the code below

H el lo

        $("#test").on('click', function(){
            //var songToTrim = $(this).html();
var songToTrim = 'A344 34';
           var trm  = songToTrim.replace(/ /g,'');
    // try this
    document.getElementById("mytext1").value = trm;
        alert(trm);
           var songToPlay = trm.toLowerCase();
            });

you can customize the code to suit your need

source for the above code: Jquery Trim all spaces from variable

Masking Example

  var value = "1234567"
var formatted = value.replace(/^(\d{3})(\d{4}).*/,"$1-$2");
alert(formatted);

source: Mask javascript variable value

chinazaike
  • 517
  • 6
  • 19
  • How to do while paste into textbox. – user3711357 Mar 30 '18 at 19:34
  • 1
    do you mean to have the value displayed in a textbox inputs – chinazaike Mar 30 '18 at 19:38
  • I have edited the answer to display result in a form inputs – chinazaike Mar 30 '18 at 19:57
  • Yes, when copy from any source and just paste to textbox then, textbox should only take non space char. Like- KLP 03K then textbox should be KLP03K and skip the field. Max lenght of textbox is 6. How to do Masking or any way to do that. – user3711357 Mar 31 '18 at 18:35
  • Have tested the above script. It will convert KLP 03K to non sapce char KLP03K. If you want to do masking. if you want to do masking, I have updated the script with example on how to do masking – chinazaike Mar 31 '18 at 19:51