Becky
Becky

Reputation: 5585

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
}

Upvotes: 1

Views: 41

Answers (2)

eltonkamami
eltonkamami

Reputation: 5190

This will do it

/\s/.test($('#myVal').val())

Upvotes: 2

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

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

Upvotes: 2

Related Questions