Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate pattern in input string?

Tags:

validation

php

If I had a webpage and I neeed to ensure user input for a variable is only letters (upper and lower), numbers and dashes and the length had to be exactly 20 chars in length, how would one perform that?

like image 548
WildBill Avatar asked Mar 07 '26 08:03

WildBill


1 Answers

This is pretty easy to do using regular expressions:

echo preg_match('/^[0-9a-zA-Z\-]{20}$/', 'abcd');
0
echo preg_match('/^[0-9a-zA-Z\-]{20}$/', 'abcdefghijkmlnopqrst');
1
like image 199
spencercw Avatar answered Mar 09 '26 22:03

spencercw