I'm trying to construct a regex that basically allows only numbers, 8 characters long and cannot be empty ie "" or have 8 blank spaces
I've been able to get two separate regex that will nearly do what I'm after: ^(?!\s*$).+ which does not allow empty strings, but permits white space. Also: ^[0-9]+$ which lets me only search for numbers.
I would like to combine these regex expression and also and in a clause to match strings that are 8 characters long.
Any advice on how I could combine what I have so far?
Just place ^(?!\s*$)
at start of your regex. Try this way ^(?!\s*$)[0-9\s]{8}$
?
^(?!\s*$)
as you know will check if entire string is not only white spaces[0-9\s]
will match any digit and white space{8}
means exactly 8 occurrences of element before it (in our case digit or white space)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