0

I am trying to add 2 functions in one regex line. It is for a formula on a website, where the user must input 4 digits:

^[\d]*$
a{4}

The first line means "only digits" and the second is "exactly 5".

The first line of code is working in my system. But I cant seem to integrate the second line into working with it as well. So can anybody make the proper line of regex (something like ^[\d]a{4}*$) that actually works with both functions?

I am using Contour in Umbraco v6.1.6 (Assembly version: 1.0.5021.24867).

Grice
  • 1,345
  • 11
  • 24

2 Answers2

1

This regex should match only 4 digits: ^\d{4}$ . Example: 1234 will match, a123 and 12345 will not match.

Edit: Updated regex to match only 4 digits as specified by OP.

Grice
  • 1,345
  • 11
  • 24
  • It doesn´t seem to work. This is the exact link: http://www.etsundtarbejdsliv.dk/det-sker/konference-2015/Tilmelding I need the "Postnummer" string to match with 4 digits only (I have changed your answer to 4 instead of 5). Your code makes good sense tho, so I can´t see why the system wont accept the code? – Rasmus Monnerup Oct 22 '14 at 08:02
  • @RasmusMonnerup Can you provide some sample strings of value's that should and shouldn't match? The regex I provided will only match 4 digits and there cannot be anything else in the string. Let me know if there are additional values or anything else in the strings being checked and I can update the regex. – Grice Oct 22 '14 at 12:42
  • The regex that you send looks fine, and there are no other values needed than exactly 4 digits. But the problem is that it doesnt say "faulth" if I input "a123" or "12345". Try to use the link above, and input something in "Postnummer" - and it will accept it. Do you think that there is something wrong with Umbraco, or is there another Regex code that can function? – Rasmus Monnerup Oct 22 '14 at 14:24
  • I am unable to view your site due to firewall restrictions. What is the code you use to check the string against the regex? – Grice Oct 22 '14 at 14:35
  • @RasmusMonnerup Looked at the site, appears your regex is working. If you still have the issue, please give a sample of the code you use to match the string. – Grice Oct 23 '14 at 01:41
  • I still have the issue. If you input for instance "34433a" in "Postnummer", it will still accept it - even though it doesnt match the string I use: ^\d{4}$ – Rasmus Monnerup Oct 27 '14 at 11:38
  • I can add that the regex modul does work, because the following regex works in the e-mail string: ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$ – Rasmus Monnerup Oct 27 '14 at 11:39
  • Your site appears to validate fields in 2 phases. The first phase appears to only be checking that every field is filled with some data. I can input `34433a` in this step and it will give me no error. If all the fields are filled and I submit, it then throws an error on `Postnummer` roughly translated to "Zip code is not correctly filled out", which seems to be the intended result. The issue is not the regex but the process you are using to evaluate the form. I am unable to help unless I can see the code you use to evaluate your regex against your form data. – Grice Oct 27 '14 at 12:50
  • Ahh I seee! Thank you for the code and your help - everything seems to be working fine :) I will contact the firm that is responsible for our Umbraco solution and see if they can make the form process the regex data beore the form data. Thank you! – Rasmus Monnerup Oct 27 '14 at 13:08
0

Your request isn't so clear, but I guess this will work for you:

^(?:\d+|a{4})$

This will match a string that contains one or more digits or 4 times the letter a.

Change 4 into 5 depending on what you want.

Toto
  • 89,455
  • 62
  • 89
  • 125
  • I´am sorry if my request wasn´t so clear. This is the exact link: http://www.etsundtarbejdsliv.dk/det-sker/konference-2015/Tilmelding I need the "Postnummer" string to match with 4 digits only. – Rasmus Monnerup Oct 22 '14 at 08:04