使用默认值验证输入

发布于 2024-12-13 05:15:47 字数 2984 浏览 2 评论 0原文

问题: 如何验证输入中设置的默认值? 我的意思是我想忽略默认值,如果用户没有插入任何不同的值,验证将提醒他没有插入任何东西。 型号:

public class ContactUsViewModel
{
    [Required(ErrorMessage = "please enter full name")]
    public string FullName { get; set; }

    [Required(ErrorMessage = "enter email")]
    [Email(ErrorMessage = "enter email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "enter phonenumber")]
    [Mobile(ErrorMessage = "enter phonenumber")]
    public string PhoneNumber { get; set; }

    [Required(ErrorMessage = "please address the issue")]
    public string Issue { get; set; }

        public IDictionary<string, object> FullNameProperties
        {
            get
            {
                return new Dictionary<string, object>()
                             {
                                 {"Value", "FullName"}
                             };
            }
        }
}

视图:

<div class="ui-contact-us-form">
    @using (Html.BeginForm("Contact","Home",FormMethod.Post))
    {
        <ul>
            <li>
                <label for="FullName" class="form-ui-icon-name ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.FullName, Model.ContactUsViewModel.FullNameProperties)
                    @Html.ValidationMessageFor(x => x.ContactUsViewModel.FullName)
                </div>  
            </li>
            <li>
                <label for="PhoneNumber" class="form-ui-icon-phone ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.PhoneNumber, Model.ContactUsViewModel.PhoneNumber)
                </div> 
            </li>
            <li>
                <label for="Email" class="form-ui-icon-email ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.Email, Model.ContactUsViewModel.EmailProperties)
                    <input type="text" value="מייל" name="Email"/>
                </div> 
            </li>
            <li>
                <label for="Issue" class="form-ui-icon-pen ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.Issue, Model.ContactUsViewModel.IssueProperties)
                </div> 
            </li>
        </ul>
        <div class="spacer"></div> 

        <div class="ui-text-area">
            @Html.TextAreaFor(x => x.ContactUsViewModel.Notes)
        </div>
        <div class="spacer"></div> 

        <div class="form-ui-btn">
            <input type="submit" value="" />
        </div>
    }
</div>

Question:
How do i validate default values that set into the inputs?
I mean i want to ignore the default values and if the user haven't inserted any different value , the validation will alert him that he haven't inserted any thing.
Model:

public class ContactUsViewModel
{
    [Required(ErrorMessage = "please enter full name")]
    public string FullName { get; set; }

    [Required(ErrorMessage = "enter email")]
    [Email(ErrorMessage = "enter email")]
    public string Email { get; set; }

    [Required(ErrorMessage = "enter phonenumber")]
    [Mobile(ErrorMessage = "enter phonenumber")]
    public string PhoneNumber { get; set; }

    [Required(ErrorMessage = "please address the issue")]
    public string Issue { get; set; }

        public IDictionary<string, object> FullNameProperties
        {
            get
            {
                return new Dictionary<string, object>()
                             {
                                 {"Value", "FullName"}
                             };
            }
        }
}

View:

<div class="ui-contact-us-form">
    @using (Html.BeginForm("Contact","Home",FormMethod.Post))
    {
        <ul>
            <li>
                <label for="FullName" class="form-ui-icon-name ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.FullName, Model.ContactUsViewModel.FullNameProperties)
                    @Html.ValidationMessageFor(x => x.ContactUsViewModel.FullName)
                </div>  
            </li>
            <li>
                <label for="PhoneNumber" class="form-ui-icon-phone ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.PhoneNumber, Model.ContactUsViewModel.PhoneNumber)
                </div> 
            </li>
            <li>
                <label for="Email" class="form-ui-icon-email ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.Email, Model.ContactUsViewModel.EmailProperties)
                    <input type="text" value="מייל" name="Email"/>
                </div> 
            </li>
            <li>
                <label for="Issue" class="form-ui-icon-pen ui-block-inline"></label>
                <div class="input-black">
                    @Html.TextBoxFor(x => x.ContactUsViewModel.Issue, Model.ContactUsViewModel.IssueProperties)
                </div> 
            </li>
        </ul>
        <div class="spacer"></div> 

        <div class="ui-text-area">
            @Html.TextAreaFor(x => x.ContactUsViewModel.Notes)
        </div>
        <div class="spacer"></div> 

        <div class="form-ui-btn">
            <input type="submit" value="" />
        </div>
    }
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2024-12-20 05:15:47

对此问题有不止一种潜在的解决方案。

可能最简单的解决方案是将默认值设置为占位符属性,而不是输入元素的 value 属性:

@Html.TextBoxFor(x => x.SomeField, new { id = "someFieldId", placeholder = "DefaultVal"})

生成以下 HTML:

<input data-val="true" data-val-required="required" id="someFieldId" name="SomeField" placeholder="DefaultVal" value="">

模型的正则表达式验证:

[RegularExpression(@"^(?!.*DefaultVal).*$", ErrorMessage = "invalid")]

如果您必须在输入元素上使用 value 属性,则此正则表达式会执行否定前瞻,以将除文字“DefaultVal”之外的任何值视为有效。话虽这么说,这个灵魂要求您选择一个默认值,该默认值永远不应被视为来自用户的实际输入。

与 [REQUIRED] 属性相结合,用户可以输入除“DefaultVal”之外的任何值、null 或空。

There is more than one potential solution to this.

Probably the simplest solution is to set your default value to placeholder attribute instead of value attribute of the input element:

@Html.TextBoxFor(x => x.SomeField, new { id = "someFieldId", placeholder = "DefaultVal"})

Which yields the following HTML:

<input data-val="true" data-val-required="required" id="someFieldId" name="SomeField" placeholder="DefaultVal" value="">

RegularExpression Validation on Model:

[RegularExpression(@"^(?!.*DefaultVal).*
quot;, ErrorMessage = "invalid")]

If you must use the value attribute on your input element, This regular expression does a negative lookahead to consider ANY value EXCEPT the literal "DefaultVal" as valid. That being said, this soultion requires you to choose a default value that should never be expected as an actual input from the user.

Coupled with the [REQUIRED] attribute users can input any value except the "DefaultVal", null or empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文