开发者

Check for comma in textbox using jQuery

How to check for a comma in a text box? I.e. if comma is pres开发者_运维百科ent the code should alert,

<input type="text" id="name"/>


You could do like:

if ($('#name').val().indexOf(',') !== -1)
{
  alert('There was a comma');
}

As you have not specified, you could put that code in blur event, etc.


$("#name").blur(function() { // or keyup, keydown, keypress, whatever you need
    if(this.value.indexOf(",") !== -1) {
        alert('got a comma');
    }
});


Doesn't really need jQuery (for the test). Here's a regular expression test().

if( /\,/.test( $('#name').val() ) ) {
   alert('found a comma');
}

A regular expression test() function returns true or false.


And the obligatory no-jQuery solution ;)

if (document.getElementById("name").value.indexOf(",") !== -1) {
    ....    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜