jQuery验证textarea maxlength错误 [英] jQuery validate textarea maxlength bug

查看:66
本文介绍了jQuery验证textarea maxlength错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery.validate v 1.6.0来验证我的表单.

我的数据库字段之一限制为1000个字符.我将验证添加到了相应的文本区域,如下所示:

在页面标题中,我添加

$('form.validate').validate();

在我的页面中,我声明:

<form method="post" class="validate" action="Save">
    <textarea name="description" minlength="15" maxlength="1000" id="description" class="required"></textarea>
    <input type="submit" value="Save">    
</form>

我遇到的问题是,jQuery似乎对换行"中涉及的字符数的计算方式与我的数据库不同.

当我精确键入1000个字符而没有换行时,一切顺利,并且验证有效.如果我用一些新行键入1000个字符,则jQuery允许POST发生,但是我的数据库拒绝插入/更新,因为数据太大.

任何提示将不胜感激.

解决方案

我遇到了一个使用jQuery Validate 1.9的复杂问题.因为我在服务器上使用ASP.NET MVC 3和C#,所以服务器上的换行符在服务器上是"\ r \ n",而在客户端上是"\ n".当我在客户端上验证文本区域时,验证不会失败,因为"\ n"只是一个字符,但是当文本在换行符为"\ r \ n"的服务器上进行验证时,验证将失败.

对此问题,我的灵魂是重写jQuery验证方法"rangelengt",因为我还定义了最小长度:

$.validator.addMethod('rangelength', function (value, element) {
        var maxlen = parseInt($(element).attr('data-val-length-max'));
        if (maxlen > 0) {
            var remaining = (maxlen - (parseInt($(element).val().replace(/(\r\n|\n|\r)/gm, '\r\n').length)));
            if (remaining < 0) {
                return false;
            }
        }

        return true;
    });

因此,此代码实际执行的操作是获取最大长度值,然后从元素中获取值,然后将所有"\ n"字符替换为"\ r \ n",以获取与服务器相同的字符数会.

I'm using jQuery.validate v 1.6.0 to validate my forms.

One of my database fields is limited to 1000 chars. I added a validation to the corresponding textarea like this:

In the header of my page, I add

$('form.validate').validate();

Inside my page, I declare:

<form method="post" class="validate" action="Save">
    <textarea name="description" minlength="15" maxlength="1000" id="description" class="required"></textarea>
    <input type="submit" value="Save">    
</form>

The issue I'm encountering is that jQuery seems to count the number of chars involved in a 'new line' differently as my database.

When I type exactly 1000 chars without new lines, all goes well, and the validation works. If I type 1000 chars with some new lines, jQuery allows the POST to happen, but my database refuses the insert/update, because the data is too big.

Any hints would be appreciated.

解决方案

I came a cross this perticular problem using jQuery Validate 1.9. Because I'm using ASP.NET MVC 3 and C# on the server line breaks was "\r\n" on the server but "\n" on the client. When I validated my textarea on the client the validation did not fail because "\n" is just counted as one character but when the text went to validation on the server where linebreaks are "\r\n" the validation would fail.

My soultion to this problem was to override jQuery validation method "rangelengt" because i also have a minimum length defined:

$.validator.addMethod('rangelength', function (value, element) {
        var maxlen = parseInt($(element).attr('data-val-length-max'));
        if (maxlen > 0) {
            var remaining = (maxlen - (parseInt($(element).val().replace(/(\r\n|\n|\r)/gm, '\r\n').length)));
            if (remaining < 0) {
                return false;
            }
        }

        return true;
    });

So what this code actually does is to get the maxlength value and then get the value from the element and then it replaces all "\n" characters with "\r\n" to get the same count of characters as the server would.

这篇关于jQuery验证textarea maxlength错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆