I have Asp.net mvc application where on submit form i am using Regex and StringLength Attribute to check string.Scenario is that i allow some kind of codes but their lengths are different. My Class item regex and attribute is here:`
[StringLength(13,ErrorMessage="Allowed 13 or 12 Characters",MinimumLength=12)]
[RegularExpression("^(01|04|05|06|pt|pT|Pt|PT|fg|fG|Fg|FG)[0-9]*(FR|fr|Fr|fR)?$", ErrorMessage = "Entered Format Is Incorrect")]
public string BarCode { get; set; }
the problem is that barcodes FG,01,04,06 are 12 in length when PT codes length must be 13. ussualy users use PT codes 90%.. sometimes they insert 12 length PT codes and its mistake. can i have 2 regex same time? and can i validate length for particular codes in regex?.i think [StringLength(13,ErrorMessage="Allowed 13 or 12 } atrribute is not what i need here..
[RegularExpression]attributes, but you can have a single regex that allows bar codes containingFGto contain 12 characters and those containingPTto containing 13 characters (using an|), and delete the[StringLength]attributeFRat the end that is optional will not count here.^(?:(?:[Ff][gG]|0[146])(?=.{10}$)|(?:05|[pP][Tt])(?=.{11}$))[0-9]*(?:[Ff][rR])?$will work exactly as you need.