javascript - Validate user input for extra long words in textarea -
i have problem here validating user's input in textarea. user suppose enter description in 1 of textarea feild in form. people put random text 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' or bypass minimum length requirement.
now want prevent user typing such long text without spaces since disrupts ui of page. long text entered user without spaces can valid url too. how manage & throw error user correct text if long , isnt valid url ??
ps: dont want split string myself.. want detect , throw error user on client side validation. put end doubts, server side validation in forcibly enter space , save in db. expecting solve problem on client side
this 2 step process:
- determine if words long.
- if so, determine if valid urls.
var validatewordlength = function (str) { var maxlength = 50, // or whatever max length want reurl = /^(ftp|http|https):\/\/[^\s]+$/, // use whatever regular expression url matching feel best words = str.split(/\s+/), i; (i = 0; < words.length; += 1) { if (words[i].length > maxlength) { // test url // bear in mind answer @ http://stackoverflow.com/questions/1410311/regular-expression-for-url-validation-in-javascript // testing url may not fruitful if (!reurl.test(words[i])) { return false; } } } return true; };