0

我在验证用户在 textarea 中的输入时遇到问题。假设用户在表单的文本区域字段之一中输入他的描述。但是有些人只是放了诸如“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'之类的随机文本,以绕过最小长度要求。

现在我想防止用户输入这么长的文本而没有任何空格,因为它会破坏我页面的 UI。用户输入的没有任何空格的长文本也可以是有效的 url。那么我该如何管理这个&只有当文本太长并且不是有效的url时才会向用户抛出错误以更正文本?

PS:我不想自己拆分字符串。我只想检测它并在客户端验证时向用户抛出错误。只是为了结束一些疑问,我将进行服务器端验证,在该验证中我将强行输入一个空间并将其保存在数据库中。但我期待在客户端解决这个问题

4

7 回答 7

2
function validate()
{
  var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
  var wordLengthExpr = /\b[^\s]{50,}\b/;
  var regex = new RegExp(expression);
  var wordLengthRegex = new RegExp(wordLengthExpr);
  var t = $("#myTextarea").val();
  if (t.match(regex) ||  !t.match(wordLengthRegex))
  {
    //valid
  }
  else
  {
    //throw error
  }
}
于 2013-04-25T11:08:04.670 回答
2
var STRING_MAX_LENGTH = 10;

var description = 'aaa aaaaaaaaaa bbbbbbbbbb http://www.google.com/search?q=client-side-filtering';
var array = description.split( ' ' );

$.each( array, function() {
  if ( this.length >= STRING_MAX_LENGTH ) {
    if( /^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i . test( this ) ) {
      alert( this + ' is an URL' );
    } else {
      alert( this + ' is not an URL' );
    }
  }
});

http://jsfiddle.net/vVYAp/

于 2013-04-25T12:17:52.677 回答
1

这是一个两步过程:

  1. 确定是否有任何单词太长。
  2. 如果是,请确定它们是否是有效的 URL。
var validateWordLength = function (str) {
    var maxLength = 50,  // or whatever max length you want
        reURL = /^(ftp|http|https):\/\/[^\s]+$/, // use whatever regular expression for URL matching you feel best
        words = str.split(/\s+/),
        i;

    for (i = 0; i < words.length; i += 1) {
        if (words[i].length > maxLength) {
            // test for url
            // but bear in mind the answer at http://stackoverflow.com/questions/1410311/regular-expression-for-url-validation-in-javascript
            // testing for url may not be fruitful
            if (!reURL.test(words[i])) {
                return false;
            }
        }
    }

    return true;
};
于 2013-04-25T12:12:09.620 回答
0

尝试这个

    var value = Your text;

    var result = value.replace(" ","");

if(value.length == result .length)
  //not valid
else
 //valid
于 2013-04-25T11:04:21.607 回答
0

您可以获得每个单词的长度,然后可以决定是否允许用户 -

var arr = text.split(' ');

$.each(arr,function(){
   console.log(this.length);
   // check valid word length
}); 

http://jsfiddle.net/mohammadAdil/cNZtn/

于 2013-04-25T11:06:30.807 回答
0

如果你使用jQuery validate 插件,你可以给它添加一个方法:

jQuery.validator.addMethod("samechars", function(value, element) { 
    return this.optional(element) ||  !/([a-z\d])\1\1/i.test(value); 
}, "Invalid input");
于 2013-04-25T11:12:25.890 回答
0

如果要使用 jQuery,可以使用以下命令:

$("form").submit(function(e){
var $textarea = $('#msg'),
    maxWordLength = 20;

            var value = $textarea.val().split(' '),
                longWord = false;

            for(var n = 0; n < value.length; n++) {
                if(value[n].length >= maxWordLength)
                    longWord = true;
            }

if(longWord) {
    alert('Too long word');
    return false;
}
});

这是一个小提琴:http: //jsfiddle.net/pJgyu/31286/

于 2013-04-25T12:10:44.710 回答