MVC5模式正则表达式验证在textarea中不起作用

时间:2017-12-21 07:51:42

标签: c# asp.net-mvc

当我在MVC中为textarea添加任何正则表达式模式时,它无法正常工作。当我单击提交按钮时,错误的值将添加到数据库

我的观看代码:

$incident->CustomFields->c->make

我的型号代码:

class AbstractBase
{
protected:
    AbstractBase()
    {};

    virtual ~AbstractBase()
    {};

public:
    //Place holder method
    void SimpleMethod(void)
    {
        printf_s("\nAbstractBase::SimpleMethod");
    };
};

class Derived : public AbstractBase
{
public:
    //This overridden but not virtual.
    void SimpleMethod(void)
    {
        printf_s("Derived::SimpleMethod");
    };
};

typedef void (AbstractBase::*SimpleMethodCallback)();

class Delegator
{
private:
    AbstractBase * pbasePtr;
    SimpleMethodCallback pCallback;

public:
    Delegator()
    {
        this->pbasePtr = nullptr;
        this->pCallback = nullptr;
    };

    void RegisterCallback(AbstractBase * pbasePtr, SimpleMethodCallback callback)
    {
        if (pbasePtr == nullptr || callback == nullptr)
            throw "Delegate target or callback cannot be null";

        this->pbasePtr = pbasePtr;
        this->pCallback = callback;
    };

    void Invoke()
    {
        if (this->pbasePtr == nullptr || this->pCallback == nullptr)
            throw "Inoke cannot be performed on null target or callback";
        (pbasePtr->*pCallback)();
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    Delegator delegate;
    delegate.RegisterCallback(new Derived(), reinterpret_cast<SimpleMethodCallback>(&Derived::SimpleMethod));

    delegate.Invoke();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

这适用于任何其他有这个问题的人...... 基于开发者评论: HTML5 <textarea>元素不支持模式属性

所以解决方案是通过MVC内置验证(使用jquery.validate.js)和jquery.validate.unobtrusive.js

所以这个问题通过改变我的模型来解决:

[StringLength(150)]
    [RegularExpression("^[0-9]*$", ErrorMessage = "UPRN must be numeric")]
        public string Item{ get; set; }

并在我的视图或部分视图中包含jquery.validate.js

只需将我的html帮助器更改为文本框而不是文本区域,如下所示:

@Html.EditorFor(model => model.Item, new { htmlAttributes = new { @class = "form-control english_only", @placeholder = "Only english text", @required = "required", @Pattern = @"^[a-zA-Z0-9$@$!%*?&#^-_. +]+$", @title = "please insert english text only", @autocomplete= "off", @maxlength = "120" } })