2

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..

5
  • You cannot have 2 [RegularExpression] attributes, but you can have a single regex that allows bar codes containing FG to contain 12 characters and those containing PT to containing 13 characters (using an |), and delete the [StringLength] attribute Commented Feb 5, 2018 at 12:30
  • Try something like this, but note that FR at the end that is optional will not count here. Commented Feb 5, 2018 at 12:32
  • 1
    Perhaps, ^(?:(?:[Ff][gG]|0[146])(?=.{10}$)|(?:05|[pP][Tt])(?=.{11}$))[0-9]*(?:[Ff][rR])?$ will work exactly as you need. Commented Feb 5, 2018 at 14:41
  • big thnx Wiktor Stribiżew - everythings works fine but ,if i will have code that starts with three symbols like KLM07489869FR--length 13,how can i add it to your regex?we are going to add this code later.ofc i am trying by myself but i dont want to mess all your good stuff. Commented Feb 6, 2018 at 7:43
  • Let me post, I will explain. Commented Feb 6, 2018 at 7:49

1 Answer 1

1

You may use

^(?:(?:[Ff][gG]|0[146])(?=.{10}$)|(?:05|[pP][Tt])(?=.{11}$))[0-9]*(?:[Ff][rR])?$

See the regex demo.

Explanation:

  • ^ - start of string
  • (?: - start of a non-capturing alternation group
    • (?:[Ff][gG]|0[146]) - either an fg, Fg, fG or FG sequence or 0 followed with 1, 4 or 6
    • (?=.{10}$) - a positive lookahead that requires any 10 more chars immediately to the right of the current location (10 is used because FG or 01 already take 2 chars making 12 in total)
    • | - or
    • (?:05|[pP][Tt]) - 05, pt, Pt, pT or PT
    • (?=.{11}$) - a positive lookahead that requires any 11 more chars immediately to the right of the current location
  • ) - end of the alternation group
  • [0-9]* - 0+ digits
  • (?:[Ff][rR])? - an optional sequence (1 or 0 times), fr, Fr, fR or FR
  • $ - end of string.

Now, adding KLM07489869FR format support (KLM is 3 char long, thus the rest must be 10, so we place the KLM matching part to the alternative with (?=.{10}$)):

^(?:(?:[Ff][gG]|0[146]|[Kk][Ll][Mm])(?=.{10}$)|(?:05|[pP][Tt])(?=.{11}$))[0-9]*(?:[Ff][rR])?$
                       ^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.