javascript - Allow at most 3 consecutive whitespaae charecters -
i'll info user using textarea. can not type 3 consecutive white space characters, invested code :d
function trim(mystring) { str = mystring; str = str.replace(/\s/g, " "); alert(str.lastindexof(" ")); if (str.lastindexof(" ")>-1) { alert("at 3 consecutive white space allowed !"); return false; } return mystring.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' '); }
but not work, can me correcting error ?
the above code first replaces types of white characters space, search 3 consecutive white space, if founds shows alert !
you can have @ allowing @ 2 newline in textarea
try alternative, simplified version:
function trim(str) { if (str.match(/\s\s\s+/)) { alert("at 3 consecutive white space allowed !"); return false; } return str.replace(/\s\s\s+/g,' '); }
Comments
Post a Comment