Pakistan Web Development Consultant Adeel Sarfraz is a consultant based in Pakistan giving out tips, tricks and code samples

11Jan/090

How to 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;
}

Filed under: Uncategorized Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.