views:

182

answers:

4

How to check for a comma in a text box. I.e. if comma is present the code should alert,

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

Thanks..

+1  A: 

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.

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

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.

Sarfraz
+1  A: 

And the obligatory no-jQuery solution ;)

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