c# - The RegularExpression data annotation is not being recongnized by the Validator -


i believe code pretty self-explanatory. why isn't regularexpression attribute being used validator?

license.cs:

public class license {   [required]   [regularexpression("([0-9a-f]{4}\\-){4}[0-9a-f]{4}")]   public string code { get; set; } } 

licensetest.cs

[testmethod] public void testvalidationofcodeproperty() {     // these tests pass know regex not issue     assert.istrue(regex.ismatch("abcd-ef01-2345-6789-ffff", "([0-9a-f]{4}\\-){4}[0-9a-f]{4}"));     assert.isfalse(regex.ismatch("abcd-ef01-2345-6789-ff00", "([0-9a-f]{4}\\-){4}[0-9a-f]{4}"));     assert.isfalse(regex.ismatch("3331313336323034313135302020202020212121", "([0-9a-f]{4}\\-){4}[0-9a-f]{4}"));      // setup validator     license lic = new license();     var ctx = new validationcontext(lic);     var results = new list<validationresult>();      // passes - tryvalidateobject returns false because required field empty     lic.code = "";     assert.isfalse(validator.tryvalidateobject(lic, ctx, results));      // passes - tryvalidateobject returns true     lic.code = "10d0-4439-0002-9ed9-0743";     assert.istrue(validator.tryvalidateobject(lic, ctx, results));      // fails - tryvalidateobject returns true     lic.code = "3331313336323034313135302020202020212121";     assert.isfalse(validator.tryvalidateobject(lic, ctx, results)); }       

use validator.tryvalidateobject(lic, ctx, results, true)

msdn explains last argument: https://msdn.microsoft.com/en-us/library/dd411772(v=vs.110).aspx

true validate properties; if false, required attributes validated


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -