如何验证文本框MVC3中必须包含字符串开始与字符"公关QUOT; [英] How to validate textbox in MVC3 that must contain string started with characters "PR"

查看:185
本文介绍了如何验证文本框MVC3中必须包含字符串开始与字符"公关QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常新MVC3和MVC3剃刀应用工作。我需要验证上查看的文本框以这样的方式即,文本框将仅接受那些被开始,字符PR和该字符串的第4个字符必须是2的字符串。如果有人可以帮助我,这将是巨大的。在此先感谢

i am very much new to MVC3 and working with MVC3 razor application. I need to validate a textbox on View in such a way that, the textbox will accept only those strings which are starting with characters "PR" and 4th character of that string must be "2". It would be great if anybody helps me. Thanks in advance

推荐答案

那么你需要的正则表达式。我不完全知道什么是正则表达式将会与你的字符串包含。但是,如果你需要有2场比赛在那里,你可以把它分解,并使用2文本框和加入后的值。

Well you need regex. I'm not exactly sure what the regex would be and what your string contains. But if you need to have 2 matches in there, you could split it and use 2 textboxes and join the values on post.

视图模型:

public class MyViewModel 
{
    [RegularExpression("^PR[A-Za-z0-9]", ErrorMessage= "Invalid Text1")]
    public string MyText1 { get; set; }

    [RegularExpression("^2[A-Za-z0-9]", ErrorMessage= "Invalid Text2")]
    public string MyText2 { get; set; }
}

警告,此正则表达式可能出现故障。我邀请其他人编辑/发表评论,我可以用正则表达式正确更新

Warning, this regex may be faulty. I invite others to edit/post comments and i can update it with correct regex

查看:

@model MyProject.Models.MyViewModel

@using (Html.BeginForm())
{

     @Html.TextBoxFor(m => m.MyText1)
     @Html.TextBoxFor(m => m.MyText2)

     <button type="submit">Submit</button>

}

希望这有助于

这篇关于如何验证文本框MVC3中必须包含字符串开始与字符&QUOT;公关QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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