Hi I am having trouble trying to create a regex that will allow me to have the field empty or a string with length 9 if populated.
The current regex im using is: ^[0-9]{9}
Which works if the field is mandatory, however we have a new requirement that this can also be empty. I've tried ^[0-9]{0|9
} but obviously that doesnt work.
Thanks in advance
^(|[0-9]{9})$
Should do the trick.
Edit: But seriously; do what @Felix Kling suggests in a comment: test the string via a .length
property, .equals("")
or the like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With