-2

I create MVC project, and I have table tblEmp stucture like this :

Id          int
Name        nvarchar
NIK         int
Point       numeric(18,2)
JointDate   date

In view create page, i want NIK field can only input by integer with maximum length 3 digit real time [not until i press button "create"], how i can solve this issue with the simple way ? Need your help, please. thanks

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
Deddy H
  • 252
  • 2
  • 6
  • 19
  • Check with `MaxLengthAttribute` in MVC 5 or use RegularExpression `DataAnnotation` in mvc – Murali Murugesan Feb 05 '14 at 16:50
  • 2
    Start here http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-helpers,-forms-and-validation – Satpal Feb 05 '14 at 16:50
  • Better http://www.asp.net/mvc/overview/getting-started – Murali Murugesan Feb 05 '14 at 16:52
  • @Murali, `MaxLengthAttribute` worked when i press button "Create" submited, and not when i press key in that NIK textbox. Is i need is when i pressed the keys in NIK textbox, it directly protected with integer value and max value = 3 digit, example = 123 – Deddy H Feb 12 '14 at 03:26

1 Answers1

1

You can use DataAnnotations to ensure that the allowed values are from 0-9 and that it is only 3 digits long.

Then wire up jQuery Validator on your view and you will have what you need, real time validation.

See the following for RegularExpressions Data Annotations (NOTE: you will need to figure out the Regular Expression for your case) DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

Community
  • 1
  • 1
Gjohn
  • 1,261
  • 1
  • 8
  • 12
  • yes i already use DataAnnotation as you suggested to me, plus combined with jquery, it will work for me. – Deddy H Feb 13 '14 at 01:31