PHP check specific characters

Asked

Viewed 42 times

3

I created a formula that lets you create code like this

12345-6789-101112

What I want now is, when validating the form I want you to vertifique if the code is correct for example.

Se contem 5(00000) em cada 3(00000-00000-00000).

Is there any way to do it?

  • Yes, do you know regular expression? In fact, the example given would be invalid, since it only has 4 digits in the second group?

1 answer

2


One way is to use a regular expression to validate the specific format. [a-z0-9] means that only letters (a-z) and numbers (0-9) will enter the capture.

$str = '12345-67819-10111';
$regex = '/^[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{5}$/i';

$valido = preg_match($regex, $str);
  • A doubt.. it does not accept letters only numbers.. and I wanted everything.. Letters and Numbers..

  • @user106225 in the original pergutna did not have and that information. It is easy to correct.

  • I know, and I’m sorry I didn’t put it better, you could help me then?

  • @user106225 I edited the answers. You can also do, with explode(), str_len() and a foreach is another way :)

  • thank you!!!!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.