Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See if Input Has Spaces with jQuery

I'm trying to write a function that reads a inputs value, and determines if a space has been used in it's creation. I'm not trying to trim anything -- just see if it would have needed the ability to trim.

I'm not even sure where to begin on this, so I don't have any code to look at. Please help if you can!

I was trying this solution but it doesn't seem to work (I don't think I'm using .has() correctly)

if ($('#id').val().has(" ")) {
            alert('has spaces')
        }
like image 855
streetlight Avatar asked Dec 01 '22 22:12

streetlight


1 Answers

Use val and indexOf :

var hasSpace = $('#myInputId').val().indexOf(' ')>=0;

If you want to test other types of "spaces" (for example a tabulation), you might use a regex :

var hasSpace = /\s/g.test($('#myInputId').val());

Demonstration

like image 69
Denys Séguret Avatar answered Dec 09 '22 19:12

Denys Séguret