English 中文(简体)
如何在使用JQuery的文本箱中自动插入一个空间。
原标题:How to automatically insert a space after every comma in a textbox using JQuery?

I trying to write a script using jQuery, it is suppose to automatically put a space after every comma "," in order to separate a sequence of numbers that user input in an input field. e.g. if they enter (45,68,95,23) it becomes (45, 68, 95, 23) when the user moves away from the input field.

检查这些投入是否含有ma子

$("#test").blur(function() { 
    if(this.value.indexOf(",") !== -1) {
        alert( got a comma );
    }
});
最佳回答

简单地将投入价值按 com体分类,将每一项目的面积分成三倍,然后与 com和空间一道加入由此产生的阵列。

$("#test").blur(function () {
  this.value = $.map(this.value.split(","), $.trim).join(", ");
});
问题回答
$( #test ).blur(function(){
  $(this).val(function(i,oldValue){
    return oldValue.replace( /,s*/g,  ,   );
  });
});

或者,用更少的 j子:

$( #test ).blur(function(){
  this.value = this.value.replace( /,s*/g,  ,   );
});
this.value=this.value.replace(/,/gim, ,  );

Using regex, this will normalize the whitespace around each comma to one U+0020 space character after, happening when the text box loses focus after changing value. It also trims whitespace (or a stray comma) from the beginning and end:

$("#test").change(function() {
    this.value = this.value.replace(/s*,s*/g,  ,  ).replace(/^,?s*|,?s*$/g,   );
});

Regex还有助于告诉用户其投入是否有效。 http://jsfiddle.net/rfQbJ/5/>rel=“nofollow> Try this demo:

$("#test").focus(function() {
    $(this).removeClass( valid invalid );
}).blur(function() {
    this.value = this.value.replace(/s*,s*/g,  ,  ).replace(/^,?s*|,?s*$/g,   );
    if(/^(?:[d.](?:, )?)*$/.test(this.value)) {
        $(this).addClass( valid );
    } else {
        $(this).addClass( invalid );
    }
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签