Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check textfield for whitespaces in jquery

I have a form. I want to put validation so that It will check if user enters white spaces or not. If its white spaces then show error. How could I do this?

like image 341
Om3ga Avatar asked Jan 20 '26 21:01

Om3ga


1 Answers

In case you want to detect if there is any white space all through the user's input string,

var str = $("input").val();
if( str.indexOf(" ") !== -1 )
{
    alert("bad input");
}

Example: http://jsfiddle.net/pYquc/

like image 107
Anirudh Ramanathan Avatar answered Jan 22 '26 10:01

Anirudh Ramanathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!