格式化文本框以在MVC上获取IP地址 [英] Format textbox to take an IP Address on MVC

查看:53
本文介绍了格式化文本框以在MVC上获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用验证方法等对C#MVC4项目进行整理,除IP地址文本框外,大多数都很好.在这里admin添加工作站的IP地址.我不知道如何将文本框格式化为""."."."空格.有什么想法吗?

I'm currently tidying up my C# MVC4 project with validation methods etc, majority is fine except for the IP address textboxes. Here admin add in the IP address of the workstations. I dont know how to format the textbox into the """"".""."".""" spaces. Any ideas?

    <div class="editor-label">
        @Html.LabelFor(model => model.AssignedIP)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AssignedIP)
        @Html.ValidationMessageFor(model => model.AssignedIP)
    </div>

模型

      [Required]
    [StringLength(30)]
    [Display(Name = "Location Name")]
    public string LocationName { get; set; }

    public string AssignedIP { get; set; }

推荐答案

如果要在mvc中屏蔽输入,我建议为此使用jquery lib并为AssignedIP创建扩展方法.

If you want masked input in mvc I would suggest using jquery lib for that and creating extension method for your AssignedIP.

http://digitalbush.com/projects/masked-input-plugin

类似这样的东西:

<script type="text/javascript" src="http://jquery-joshbush.googlecode.com/files/jquery.maskedinput-1.2.1.pack.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#AssignedIP").mask("999.999.999.999");
        });
    </script>

或者,如果您只需要验证,则可以使用正则表达式:

Or if you just need validation then you can use regex for that:

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

它与IPv4地址匹配.

it match the IPv4 addresses.

[RegularExpression(@"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")]
public string AssignedIP { get; set; }

在您的ViewModel中

in your ViewModel

这篇关于格式化文本框以在MVC上获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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