c# - EditorFor helper: show no negative values -


i have value:

[displayformat(applyformatineditmode = true, dataformatstring = "{0}")] public int mcardnumber { get; set; } 

and in view i'm rendering this:

card number: @html.editorfor(_item => _item.mcardnumber, model.mcardnumber)<br /> 

the reason being value bound model. however, if cannot insert negative value, still possible go in negative. error message shown. i've been asked not allow editor go in negative. can me this?

you're going need leverage javascript. add javascript function markup or shared .js file.

function disallownegativenumber(e)  {     var charcode = (e.which) ? e.which : event.keycode     if (charcode == 45) {         return false;     }     return true; } 

and call onkeypress="return disallownegativenumber(event)". add via htmlattributes parameter on textboxfor method types. ones more specific editorfor. you'd build object this:

new { onkeypress = "return disallownegativenumber(event)" } 

Comments