1

I'm facing a problem where I would like to give an user an immediate feedback that an input field already doesn't comply following a validation pattern and not after providing the whole value.

To give you an example: UK postcode might look like this [SW1W 0NY] and I would like to inform the user that everything looks good so far when he enters [SW] but give him immediate feedback when he enters for example [1].

How would you approach this? Since UK postcode can be up to 7 numbers I don't want to create 7 regular expressions to check the postcode against based on the postcode length but rather have some 'feedforward' machanism.

MelkorNemesis
  • 3,415
  • 2
  • 20
  • 20
  • Have you got the pattern that works for the final validation? – Wiktor Stribiżew Oct 25 '16 at 08:00
  • With 1.2K rep, you should know to post some effort and code. Look for on the fly validation and uk postcode validation - https://www.google.co.uk/search?q=on%20the%20fly%20validate%20uk%20postcode%20javascript - and note you will have an issue testing all postcodes: http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive - https://regex101.com/r/NUiHiD/2 – mplungjan Oct 25 '16 at 08:01
  • Let them type what they want and use setTimeout to show it is not yet valid. For example with a red border – mplungjan Oct 25 '16 at 08:12
  • That it easy to implement, I would rather give them a hint that "you're on a way to have a valid postcode" in the sense that they didn't put a wrong character yet. – MelkorNemesis Oct 25 '16 at 08:16

1 Answers1

0

The final validation could look like this:

/^[a-zA-Z]{1,2}[0-9]{1,2}[a-zA-Z]{0,1} ?[0-9][a-zA-Z]{2}$/

And for the partial validation I would try something like this:

^([a-zA-Z]{1,2}([0-9]{1,2}([a-zA-Z]{0,1}( ?([0-9]([a-zA-Z]{1,2})?)?)?)?)?)?$

all the parts are optional, just make sure, that even in the last part can be one or two chars.

Bicz
  • 1
  • 1
  • 1
    That was not the question. See my comment on why your regex likely is not enough – mplungjan Oct 25 '16 at 08:08
  • Ok, I was hoping in some kind of an mechanism (dunno, maybe regular expression flag that calls a callback when not following regular expression). – MelkorNemesis Oct 25 '16 at 08:09