简体   繁体   中英

How to remove space in jquery masking

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM