Validating a comma in a text field using jQuery

How to check a comma in a text box. That is, if a comma is present, the code should warn,

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

Thank..

+3
source share
4 answers

You can do the following:

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

As you did not specify, you can put this code in an event blur, etc.

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

jQuery ( ). test().

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

test() true false.

+3

no-jQuery;)

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

Source: https://habr.com/ru/post/1745634/


All Articles