最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove character from input field while user is typing (keyup) - Stack Overflow

programmeradmin0浏览0评论

I'm looking for a js or jq way to remove # character while user is typing on a field.

I tried this:

$( function() {
    $( ".remove-sharp" ).on( "keyup", function( event ) {
        console.log( 'test' );
        $( this ).val().replace( /\#/, "" );
    } )
} );

I can see the "test" being printed in console but this has no effect on the characters in the field; it doesn't remove #. How to achieve this ?

I'm looking for a js or jq way to remove # character while user is typing on a field.

I tried this:

$( function() {
    $( ".remove-sharp" ).on( "keyup", function( event ) {
        console.log( 'test' );
        $( this ).val().replace( /\#/, "" );
    } )
} );

I can see the "test" being printed in console but this has no effect on the characters in the field; it doesn't remove #. How to achieve this ?

Share Improve this question asked Apr 24, 2017 at 14:47 Robert BraxRobert Brax 7,31812 gold badges44 silver badges76 bronze badges 1
  • possible duplicate of remove char when user is typing. – Felix Haeberle Commented Apr 24, 2017 at 14:49
Add a comment  | 

2 Answers 2

Reset to default 14

The issue is because you're not setting the value of the input, only getting it and making the replacement and doing nothing with the resulting string. Try this:

$(function() {
  $(".remove-sharp").on("keyup", function(event) {
    var value = $(this).val();
    if (value.indexOf('#') != -1) {
      $(this).val(value.replace(/\#/g, ""));
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="remove-sharp" />

function validateCustomerName(){
    var validatedName = "";
    var restrictedCharactersArray = ["0","1","2","3","4","5","6","7","8","9","~","`","!","@","#","$","%","^","&","*","(",")","-","_",
        "+","=","{","}","[","]",":",";","'","<",">",",",".","?","/","/\/","|"];
    var customerName = document.getElementById("customerName").value;
    var numberValidation = (/^[a-zA-Z_ ]+$/g).test(customerName);
    if(!numberValidation){
        validatedName = "";
        var customerNameArray = customerName.split("");
        for(var i=0;i<restrictedCharactersArray.length;i++){
            var restrictedCharacter = restrictedCharactersArray[i];
            if(customerNameArray.indexOf(restrictedCharacter) !== -1){
                for(var j=0; j<customerNameArray.length; j++){
                    var customerNameCharacter = customerNameArray[j];
                    if(customerNameCharacter !== restrictedCharacter){
                        validatedName = validatedName+customerNameCharacter;
                    }
                }
            }
        }
        document.getElementById("customerName").value = validatedName;
    }
}
<input type="text" id="customerName" onKeyUp="validateCustomerName();" />

发布评论

评论列表(0)

  1. 暂无评论