mvc3 validate input 'not-equal-to'

后端 未结 6 1520
滥情空心
滥情空心 2020-12-13 21:33

My forms have inputs with default helper text that guides the user on what to enter (rather than using labels). This makes validation tricky because the input value is never

6条回答
  •  隐瞒了意图╮
    2020-12-13 21:49

    It took a little while since your question was asked, but if you still like data annotations, this problem can be easily solved using this library:

    [Required]
    [AssertThat("FieldA != 'some text'")]
    public string FieldA { get; set; }
    

    Above, the field value is compared with some pre-defined text. Alternatively, you can compare fields values with each other:

    [AssertThat("FieldA != FieldB")]
    

    ...and when the case of the strings being compared does not matter:

    [AssertThat("CompareOrdinalIgnoreCase(FieldA, FieldB) != 0")]
    

提交回复
热议问题