Home Not able to disable comma in textarea
Reply: 3

Not able to disable comma in textarea

coder12345
1#
coder12345 Published in 2017-09-14 08:56:33Z

Below is the code where textarea will be created automatically. But i want to disable comma in this textarea,so i am using the below javascript function.

    $(document).ready(function(){
    
        var counter = 1;
        var val;
    
        $("#addButton").click(function () {
    
        	var person = prompt("Please enter the Field name:", "");
      	  if (person == null || person == "") {
      	       
      		return false;
      	    } else {
      	       
      	        val = person;
      	    }
    if(tabid == "menu4"){
    	return false;
    }
    	//alert(tabid);
      	var newTextBoxDiv0 = $(document.createElement('div'))
        .attr("class", 'form-group row')
        .attr("id",'form1ac' + counter);
      	
        newTextBoxDiv0.after().html('<div class="col-xs-1"><input type="button" value="delete" onclick= rem(form1ac'+counter+')></div><div class="col-xs-1"><button type="button" class="btn btn"  name="" id="buttonl"  style="width: 170px;height:45px;background-color:#dcdcdc;color:black;">'+val+'</button><input type="hidden" name="buttonl" form="form1" value='+val+'></div><div class="col-xs-2"></div><div class="col-xs-4"><div class="form-group"><textarea form="form1" name="df" id="df" oninput = "this.value = this.value.replace(/[,]/g, "2")" ></textarea></div><input form="form1" type="hidden" name="tabid" value='+tabid+'></div>');
        newTextBoxDiv0.appendTo('#'+tabid);
        
        counter++;
         });
    
     
      
        
        $("#removeButton").click(function () {
        	if(counter==1){
                  alert("No more textbox to remove");
                  return false;
               }
    
        	counter--;
        	
                $("#form1ac" + counter).remove();
    
             });
    
        
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea form="form1" name="df" id="df" oninput = "this.value = this.value.replace(/[,]/g, "2")" ></textarea>

The above jquery code will dynamically create new textarea with label option,which works perfectly. But i am trying to disable comma with the above mentioned javascript code which is not working perfectly.

Please help me!

Toni Michel Caubet
2#
Toni Michel Caubet Reply to 2017-09-14 09:12:56Z

If you want to prevent user to type a comma:

<textarea class="no-comma"></textarea>

And

$(function() {
    $('textarea.no-comma').on('keydown', function(e) {
        if (e.keyCode == 188 || e.keyCode == 110) { // thats a comma
            e.preventDefault();
        }
    }).on('change input', function() {
        var self = $(this);
        self.html( self.html().replace(new RegExp(',', 'g'),'') ); // Remove all commas.
    });
})
Brahma Dev
3#
Brahma Dev Reply to 2017-09-14 09:05:17Z

Your html has a syntax error "2")" >

Should be '2')" >

<textarea form="form1" name="df" id="df" oninput = "this.value = this.value.replace(/[,]/g, '2')" ></textarea>

Anup pandey
4#
Anup pandey Reply to 2017-09-14 09:09:00Z

In place of below code

<textarea form="form1" name="df" id="df" oninput = "this.value = this.value.replace(/[,]/g, "2")" ></textarea>

use below code

<textarea form="form1" name="df" id="df" oninput = 'this.value = this.value.replace(/[,]/g, "2")' ></textarea>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306508 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO