Check for whitespace in Javascript

If  you have ever come across form validation in HTML then you will have come across the issue of visitors inputting white space in the form controls and submitting it which does not serve any purpose to the client. Not only that this could also be seen as a potential vulnerability by spammers.

So the best way to go about this is to place validation to check for white space and what better way than to use regular expression for checking it.

You can use the following function to check for white space:

function chkspace(str)
{
isWhiteSpace = new RegExp(/^\s+$/);
if (isWhiteSpace.test(str)) {
return false;
}
return true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *