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..
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..
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.
$("#name").blur(function() { // or keyup, keydown, keypress, whatever you need
if(this.value.indexOf(",") !== -1) {
alert('got a comma');
}
});
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.
And the obligatory no-jQuery solution ;)
if (document.getElementById("name").value.indexOf(",") !== -1) {
....
}