Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace space to underscore at same time (when i enter the text in input field)?

Tags:

jquery

regex

can you please tell me how to replace space to underscore at same time (when i enter the text in input field)? If I have string I used like that.

replace(/ /g,"_");

But I am looking for when user enter text on input field then It automatically replace the space with underscore .I used keyup event it only fire first time.

$("#test").keyup(function() {
  var textValue = $(this).val();
    if(textValue==' '){
        alert("hh");
    }
});
like image 336
Rohit Avatar asked Aug 29 '13 07:08

Rohit


People also ask

How do I change a space underscore in SQL?

ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name; UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_')); Just be sure to update the data-type in the ALTER command to reflect your actual data-type. Save this answer.

How do you change underscore in Excel with spaces?

Select the range of cells where you want to replace spaces (B2:B7), and in the Menu, go to Edit > Find and replace (or use the keyboard shortcut CTRL + H). 2. In the pop-up window, (1) enter space in the Find box and (2) underscore (_) in the Replace with box. The, (3) click Replace All and (4) Done.

What is the correct syntax to substitute all of the underscores _ with hyphens as shown in the exhibit?

1) Change the format for ALL cells on a sheet to TEXT. 2) Put 1_1_1 in cell A1 and 20_20_20 in cell B1. 3) Search and Replace "_" with "-" (replace hyphen with a dash).


2 Answers

DEMO

$("#test").keyup(function () {
    var textValue = $(this).val();
    textValue =textValue.replace(/ /g,"_");
    $(this).val(textValue);
});

Update

DEMO

$("#test").keyup(function () {
    this.value = this.value.replace(/ /g, "_");
});
like image 64
Tushar Gupta - curioustushar Avatar answered Oct 11 '22 13:10

Tushar Gupta - curioustushar


Simply try this Demo

$("#test").keyup(function() {
    if ($(this).val().match(/[ ]/g, "") != null) {
        $(this).val($(this).val().replace(/[ ]/g, "_"));
    }
});
like image 3
zzlalani Avatar answered Oct 11 '22 14:10

zzlalani