Jump to content

Regex for limiting data input


JakeCarter

Recommended Posts

Afternoon all,

I am having issues with some regex creation for one of our forms and wanted some pointers or if anyone has had to do anything similar to this.

 

For recruitment they have 3 different types of codes they need to capture when customers raise a request. I have made them separately but now they need to be put on to one field.

 

123-ABC-A-1234-A

^([0-9]{3})-([A-Z]{3})-([A-Z]{1})-([0-9]{4})-([A-Z]{1})$

 

123-ABC-1234

^([0-9]{3})-([A-Z]{3})-([0-9]{4})$

 

123-ABC-A-1234

^([0-9]{3})-([A-Z]{3})-([A-Z]{1})-([0-9]{4})$

 

I've spent a few hours trying to combine these in to one statement. Has anyone got any pointers or examples?

 

Many Thanks in advance

 

 

Link to comment
Share on other sites

Hi @Jake Carter

Give this one a try

^(\d{3})-([A-Z]{3}-)([A-Z]{1}-)?(\d{4})(-[A-Z]{1})?$

Broken down...

(\d{3})- = 3 digits followed by a dash

([A-Z]{3}-)  = 3 upper case characters followed by a -

([A-Z]{1}-)? = 1 optional upper case character and optional -

(\d{4}) = 4 digits

(-[A-Z]{1})? = 1 optional - and uppercase character

 

I'm also no RegEx expert, so I can't say for sure if this covers absolutely every case.  See how you go with this and let us know if it works.

 

  • Like 1
Link to comment
Share on other sites

9 hours ago, James Ainsworth said:

Hi @Jake Carter

Give this one a try

^(\d{3})-([A-Z]{3}-)([A-Z]{1}-)?(\d{4})(-[A-Z]{1})?$

Broken down...

(\d{3})- = 3 digits followed by a dash

([A-Z]{3}-)  = 3 upper case characters followed by a -

([A-Z]{1}-)? = 1 optional upper case character and optional -

(\d{4}) = 4 digits

(-[A-Z]{1})? = 1 optional - and uppercase character

 

I'm also no RegEx expert, so I can't say for sure if this covers absolutely every case.  See how you go with this and let us know if it works.

 

This has worked beautifully! Thank you so much

 

The breakdown has really helped, I have got a few more captures to do and its cleared some things up for me

 

Thank you both again for your help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...