2016-09-01 13 views
3

フォームを作成しました。@Html.TextBoxFor1以上の値を受け入れるようにします。つまり、値は0以下にはなりません。Html.TextBoxのみ0より大きい数値を受け入れる

モデル:

[Required(ErrorMessage = "You need to fill in a value.")] 
[RegularExpression(@"^[0-9]{1,3}$", ErrorMessage = "The value must be a number, with a length between 1 and 3.")] 
public string Duur { get; set; } 

ページ:

@Html.LabelFor(model => model.Duur)<span>*</span> 
@Html.TextBoxFor(model => model.Duur) 
@Html.ValidationMessageFor(model => model.Duur) 

私はDataAnnotationでこれを行う、あるいは他の方法があることはできますか?

+1

<input type="range" name="points" min="0" max="10"> 

を使用することができますプロパティは数値型( 'int')で' string'ではなく、 '[Range]'属性を適用する必要があります –

+0

@StephenMuecke数字のための特別なテキストボックスはありません。 'type =" number "'のような方法はありません。 – Arendax

+0

はい、ブラウザのHTML5番号を使用する場合は、 '@ Html.TextBoxFor(model => model.Duur、new {type =" number "、@ class =" form-control "...})'コントロール –

答えて

2

それはあなたがclass

[Range(1, 1000)] 
public int Duur { get; set; } 

[Range]を置いたときに動作し、あなたがView

@Html.TextBoxFor(model => model.TicketID, null, new { type = "number", @class = .....}) 

type = "number"を配置する場合は、あなたが何かを学んだ願っています。答えを

おかげステファン・ミュエッケ

2

あなたは1の最小値との数をしたい場合は、あなたの、その後、カミソリ

@Html.TextBoxFor(model => model.TicketID, null, new { @min="0" , @max="10" , type = "range", @class = .....}) 
+0

これは[IE 10以上でサポートされています](http://caniuse.com/#feat=input-range) – Liam

関連する問題