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
Post a Comment