|
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@$^�-9\s\S]{2}";
const string AN70Optional = @"[a-zA-Z@$^�-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
|
|
|
|
|
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)
|
|
|
|
|
i am also trying the 2 approach and for that i have to write a custom attribute.
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
{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)
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
No it does not get hit.
ClientName="dd";
|
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
|
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")]
|
|
|
|
|
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.
|
|
|
|
|
|
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.
|
|
|
|
|
|
I have one html document . I want to highlight text in html document but which text I want to highlight that is repeated text in html document . But I want to highlight only exact text according to my requirement then how can I determine the exact text which I want to highlight . Manually, I know that this text should have to highlight but how programmatically determine exact text to highlight between duplicates.
Please some one can help to me.
My exact scenario is giving below-
I want explain my scenario I have some pdf and converting in epub file.After converting some text font name or font style missing then I have to compare with pdf and epub and highlight the exact text which font is missing.But because of duplicates I am not able to determine the text for highlight.
modified 28-Jan-14 5:01am.
|
|
|
|
|
If you don't know which text to highlight then how do you expect anyone else to know?
Veni, vidi, abiit domum
|
|
|
|
|
I want explain my scenario I have some pdf and converting in epub file.After converting some text font name or font style missing then I have to compare with pdf and epub and highlight the exact text which font is missing.But because of duplicates I am not able to determine the text for highlight.
|
|
|
|
|
I wish I could, but my brain melted trying to work out exactly who does know which text you want to highlight...
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|