Data annotation validation for integer value

Data annotation validation for integer value



I have a property called ID_No and would like validation on this: the length of this needs to be 13 digits, not more nor less


ID_No



I have tried the following:


[Required]
[RegularExpression(@"[0-9]13", ErrorMessage = "Invalid ID Number")]
[Range(1000000000000, 9999999999999, ErrorMessage = "ID number cannot be longer than 13 digits")]
[Display(Name = "ID Number")]
public int ID_No get; set;



However it does not seem to work. It keeps saying the value "whatever 13 digits I enter" is invalid.



Any suggestions or help on how I can get this to work?






A [RegularExpression] attribute on a int makes no sense - the property would need to be typeof string. And the maximum value for an int is 2147483647 so an int cannot have 13 digits

– user3559349
Sep 15 '18 at 8:41


[RegularExpression]


int


string


int


2147483647


int






What datatype you suggest? @StephenMuecke

– Daniel
Sep 15 '18 at 10:08






You could use a long with a [Range] attribute (but definitely not a [StringLength] attribute) or a string with your [RegularExpression] and a [StringLength] attribute. It depends on what the value is actually for (are you doing numeric calculations with it? - it sound like its really some kind of identifier property?)

– user3559349
Sep 15 '18 at 10:12


long


[Range]


[StringLength]


string


[RegularExpression]


[StringLength]






@StephenMuecke It is used on the registration page, The user just enters their ID number and I store it in the DB

– Daniel
Sep 15 '18 at 10:23






Then a string is probably more appropriate (albeit you requirement for an ID for an ID to be exactly 13 characters is a bit odd)

– user3559349
Sep 15 '18 at 10:25


string


ID


ID




1 Answer
1



try this


[Required]
[Display(Name = "ID Number")]
[Range(1000000000000, 9999999999999, ErrorMessage = "ID number cannot be longer than 13 digits")]
[StringLength(13, MinimumLength=13, ErrorMessage = "ID number must be 13 char")]
public long ID_No get; set;



Check this fiddle: https://dotnetfiddle.net/g3ruR2






Seriously?? How could a 13 digit number be converted to an int - you just get an error in the POST method

– user3559349
Sep 15 '18 at 9:57


int






use 'long` if you get error as int cannot hold that high value, check updated answer

– Nitin Sawant
Sep 15 '18 at 10:02







Its not my question :) And what is the point of claiming to solve one issue and then create another

– user3559349
Sep 15 '18 at 10:06






And if you are going to create fiddle, then show all the code to make it work - not omit the code that cause exceptions - https://dotnetfiddle.net/u4MawN

– user3559349
Sep 15 '18 at 10:28






OP had issues with textbox length only

– Nitin Sawant
Sep 15 '18 at 15:59



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)