javascript - Preventing a form from submitting when filled with new lines only -


so have form has textarea , don't want form sent if textarea filled spaces or new lines \n, first condition do

$("textarea").val().replace(/\u0020+/gm, ' '); if($("textarea").val().length<2) return; 

but don't know how prevent form submitting when textarea filled new lines only.

jquery

i see you're using jquery. has handy $.trim() function remove unnecessary whitespace in beginning , end of string. running trim on string consists of spaces, enters , other whitespace characters result in empty string - work fine you.

if($.trim($("textarea").val()).length < 2) return; 

javascript

javascript has native string.trim() function, old ies don't support it. using code can still "teach" them method:

if(!string.prototype.trim) {   string.prototype.trim = function () {     return this.replace(/^\s+|\s+$/g,'');   }; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -