Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validation and Data storing in one step

Validation and Data storing in one step

Scheduled Pinned Locked Moved C#
regex
21 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nitin_ion

    I am reading file and after validation i am saving data in properties Now i was hoping to use properties for validation and then save if validation succeed for e.g.

    const string AN2Mandatory = @"[a-zA-Z@$^&#0-9\s\S]{2}";
    const string AN70Optional = @"[a-zA-Z@$^&#0-9\s\S]{70}";

    these are for validations and these are properties

    public string VersionNumber { get; set; }
    public string ClientName { get; set; }

    this is sample logic

    Match m = Regex.Match(Line, AN70Optional);
    VersionNumber = m.Value;

    can i apply attributes or something so that while puting value in property my validation is evaluated first and then fill property. instead of validating separately and then storing it

    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu Peter
    wrote on last edited by
    #2

    I can offer you two ways... 1 - Put the validation into the property setter (simple). 2 - Write (or find) an attribute that gets the regex as parameter and validates the property decorated with (much powerful).

    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

    N 1 Reply Last reply
    0
    • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

      I can offer you two ways... 1 - Put the validation into the property setter (simple). 2 - Write (or find) an attribute that gets the regex as parameter and validates the property decorated with (much powerful).

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      N Offline
      N Offline
      nitin_ion
      wrote on last edited by
      #3

      i am also trying the 2 approach and for that i have to write a custom attribute.

      Kornfeld Eliyahu PeterK 1 Reply Last reply
      0
      • N nitin_ion

        i am also trying the 2 approach and for that i have to write a custom attribute.

        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu PeterK Offline
        Kornfeld Eliyahu Peter
        wrote on last edited by
        #4

        Great - when you ready made it into an article to share with all!!!

        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

        N 1 Reply Last reply
        0
        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

          Great - when you ready made it into an article to share with all!!!

          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

          N Offline
          N Offline
          nitin_ion
          wrote on last edited by
          #5

          This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function

          Kornfeld Eliyahu PeterK M 2 Replies Last reply
          0
          • N nitin_ion

            This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #6

            {70} doesn't mean exactly 70, but up to 70!!! The correct format of {} is {m,n} where m is from and n is to... When you use only one number it interpreted as {0,n}. For exactly 70 characters try {70,70}!!!

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            N 1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              {70} doesn't mean exactly 70, but up to 70!!! The correct format of {} is {m,n} where m is from and n is to... When you use only one number it interpreted as {0,n}. For exactly 70 characters try {70,70}!!!

              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

              N Offline
              N Offline
              nitin_ion
              wrote on last edited by
              #7

              yeah that's fine but issue is that it is not fired even if i pass null or less than 70 char less than 70 so it should return false or should not accept value in that property. but currently it is not happenin

              Kornfeld Eliyahu PeterK 1 Reply Last reply
              0
              • N nitin_ion

                yeah that's fine but issue is that it is not fired even if i pass null or less than 70 char less than 70 so it should return false or should not accept value in that property. but currently it is not happenin

                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu Peter
                wrote on last edited by
                #8

                You attribute's Match method got hit?

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                N 1 Reply Last reply
                0
                • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                  You attribute's Match method got hit?

                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                  N Offline
                  N Offline
                  nitin_ion
                  wrote on last edited by
                  #9

                  No it does not get hit. ClientName="dd";

                  Kornfeld Eliyahu PeterK 1 Reply Last reply
                  0
                  • N nitin_ion

                    No it does not get hit. ClientName="dd";

                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu Peter
                    wrote on last edited by
                    #10

                    Go and read this: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^] It may also bee of interest: http://msdn.microsoft.com/en-us/library/ff649907.aspx[^] http://msdn.microsoft.com/en-us/library/ee707335(v=vs.91).aspx[^] And also read some regex - yours is a mess...

                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                    N 1 Reply Last reply
                    0
                    • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                      Go and read this: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^] It may also bee of interest: http://msdn.microsoft.com/en-us/library/ff649907.aspx[^] http://msdn.microsoft.com/en-us/library/ee707335(v=vs.91).aspx[^] And also read some regex - yours is a mess...

                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                      N Offline
                      N Offline
                      nitin_ion
                      wrote on last edited by
                      #11

                      this is good thanks but data i will have is something like this RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 RA is one field 01 is one field XYZ is on and so on so i will have to extract and then validate any suggestions

                      Kornfeld Eliyahu PeterK 1 Reply Last reply
                      0
                      • N nitin_ion

                        this is good thanks but data i will have is something like this RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 RA is one field 01 is one field XYZ is on and so on so i will have to extract and then validate any suggestions

                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu Peter
                        wrote on last edited by
                        #12

                        With regex you can validate without breaking it apart...

                        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                        N 1 Reply Last reply
                        0
                        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                          With regex you can validate without breaking it apart...

                          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                          N Offline
                          N Offline
                          nitin_ion
                          wrote on last edited by
                          #13

                          yeah but then we will have to validate through regex and store it in property separately i guess this is what they call catch 22 ;)

                          Kornfeld Eliyahu PeterK 1 Reply Last reply
                          0
                          • N nitin_ion

                            yeah but then we will have to validate through regex and store it in property separately i guess this is what they call catch 22 ;)

                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu Peter
                            wrote on last edited by
                            #14

                            I think not. You should pass the regex as a property to your new attribute...

                            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                            N 1 Reply Last reply
                            0
                            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                              I think not. You should pass the regex as a property to your new attribute...

                              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                              N Offline
                              N Offline
                              nitin_ion
                              wrote on last edited by
                              #15

                              that's what i am doing but see i need to first extract validate store [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}", ErrorMessage = "ok")] public string RecordType { get; set; } [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}",ErrorMessage="ok1")] public string VersionNumber { get; set; } [StringLengthValidator(1, 50, MessageTemplate = "Last Name must be between 1 and 70 characters")] public string ClientName { get; set; } ----------------- string Line="RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 SENDINGENTITYIDENTIFIER SENDERNAMESIZE70 "; p.RecordType = Line; p.VersionNumber = Line; Now issue is that it will always start from 1 character it will not increment RecordType will start from 1 to 2 so VersionNumber should start from 3 to 4 and ClientName from 5 to 50 i guess i'll try mentioning this using StringLength validator but then datatype validation will be a case

                              Kornfeld Eliyahu PeterK 1 Reply Last reply
                              0
                              • N nitin_ion

                                that's what i am doing but see i need to first extract validate store [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}", ErrorMessage = "ok")] public string RecordType { get; set; } [RegexValidator(@"[a-zA-Z@$^�-9\s\S]{2}",ErrorMessage="ok1")] public string VersionNumber { get; set; } [StringLengthValidator(1, 50, MessageTemplate = "Last Name must be between 1 and 70 characters")] public string ClientName { get; set; } ----------------- string Line="RA01XYZ 201401231445012345611012345678998765432101234DESTINATIONNAMESIZEIS70 SENDINGENTITYIDENTIFIER SENDERNAMESIZE70 "; p.RecordType = Line; p.VersionNumber = Line; Now issue is that it will always start from 1 character it will not increment RecordType will start from 1 to 2 so VersionNumber should start from 3 to 4 and ClientName from 5 to 50 i guess i'll try mentioning this using StringLength validator but then datatype validation will be a case

                                Kornfeld Eliyahu PeterK Offline
                                Kornfeld Eliyahu PeterK Offline
                                Kornfeld Eliyahu Peter
                                wrote on last edited by
                                #16

                                I see. For that you have to write an other class/method to split the string into fields... You may use these: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(v=vs.110).aspx[^] http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute(v=vs.110).aspx[^]

                                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                N 1 Reply Last reply
                                0
                                • N nitin_ion

                                  This is a win form application i have created a class public class ValidateProperty : Attribute { string _pattern; bool _mandatory; public ValidateProperty(string Pattern, bool Mandatory = false) { _pattern = Pattern; _mandatory = Mandatory; } public override bool Match(object obj) { Match m = Regex.Match((string)obj, _pattern); if (_mandatory == true) { if (string.IsNullOrWhiteSpace(m.Value)) return false; else { return true; } } else return true; } } and then applied on property [ValidateProperty(@"[a-zA-Z@$^�-9\s\S]{70}",true)] public string ClientName { get; set; } but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function

                                  M Offline
                                  M Offline
                                  Matt T Heffron
                                  wrote on last edited by
                                  #17

                                  Just attaching a custom attribute to the property doesn't automatically cause some validation. You must have something that looks for an "appropriate" attribute, using Reflection, typically, and causes the actual validation.

                                  Kornfeld Eliyahu PeterK 1 Reply Last reply
                                  0
                                  • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                    I see. For that you have to write an other class/method to split the string into fields... You may use these: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(v=vs.110).aspx[^] http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.fieldoffsetattribute(v=vs.110).aspx[^]

                                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                    N Offline
                                    N Offline
                                    nitin_ion
                                    wrote on last edited by
                                    #18

                                    thanks now i guess i cant do evs in one step so i am extracting first using substring and then validating and storing using attributes [EL] [RegexValidator(@"[a-zA-Z@$^�-9\S]{2}", MessageTemplate = "RecordType Validation failed")]

                                    1 Reply Last reply
                                    0
                                    • M Matt T Heffron

                                      Just attaching a custom attribute to the property doesn't automatically cause some validation. You must have something that looks for an "appropriate" attribute, using Reflection, typically, and causes the actual validation.

                                      Kornfeld Eliyahu PeterK Offline
                                      Kornfeld Eliyahu PeterK Offline
                                      Kornfeld Eliyahu Peter
                                      wrote on last edited by
                                      #19

                                      See here... http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^]

                                      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                      M 1 Reply Last reply
                                      0
                                      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                        See here... http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx[^]

                                        I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                        M Offline
                                        M Offline
                                        Matt T Heffron
                                        wrote on last edited by
                                        #20

                                        I see... However, the OP didn't derive from ValidationAttribute, they derived directly from Attribute:

                                        public class ValidateProperty : Attribute

                                        and that's NOT going to work.

                                        Kornfeld Eliyahu PeterK 1 Reply Last reply
                                        0
                                        • M Matt T Heffron

                                          I see... However, the OP didn't derive from ValidationAttribute, they derived directly from Attribute:

                                          public class ValidateProperty : Attribute

                                          and that's NOT going to work.

                                          Kornfeld Eliyahu PeterK Offline
                                          Kornfeld Eliyahu PeterK Offline
                                          Kornfeld Eliyahu Peter
                                          wrote on last edited by
                                          #21

                                          He started with simple attribute, but moved on after this: http://www.codeproject.com/Messages/4746945/Re-Validation-and-Data-storing-in-one-step.aspx[^]

                                          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                          "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups