Becky Becky - 4 months ago 18
jQuery Question

Checking at least a single White space

How can I detect if an input field has at least one white space?

This is what I tried

if( $('#myVal').val().indexOf(' ') != 0 ){
///has at least a single white space within the string
}

Answer

You must compare indexOf to -1 not 0

if( $('#myVal').val().indexOf(' ') != -1 ){
     ///has at least a single white space within the string
}

Here is the DEMO

Comments