-3

I have a regular expression which compares if a string is having both alpha and numerical values. But i need to compare if the string is having any special characters and length of the string should be 6. my current regular expression is

$val = 'A457718';
preg_match('/^[A-Z]|[0-9A-Z]*([0-9][A-Z]|[A-Z][0-9])[0-9A-Z]*$/i', $val)

But i need to compare if there are any special characters are there and string length should be 6. Any help would be greatly appreciated.

user3408779
  • 981
  • 2
  • 19
  • 43
  • Provide some sample valie and invalid inputs. – anubhava Jul 21 '15 at 14:25
  • What are `special characters`? Do you only want the string to be 6 characters long and alphanumerical? Sample input and expected outcome would be useful. – chris85 Jul 21 '15 at 14:54
  • any characters other than alpha numeric – user3408779 Jul 21 '15 at 14:55
  • So you just want to check that the string is 6 characters long? Post a valid input sample and an invalid sample. – chris85 Jul 21 '15 at 14:57
  • valid is : 457718, Invalidis : 457718A(alphabet) or 4577188(more than 6 char), 4577_18(special char) but any way this last one comes under more than 6 digits – user3408779 Jul 21 '15 at 15:10
  • Questions or issues with any of the provided answers? – chris85 Jul 21 '15 at 18:34
  • This question is very Unclear. The coding attempt in your question and the alphanumeric tag are far removed from the list of valids/invalids in your comment. Look at how different the answers are. The accepted answer does not permit letters, but your regex pattern intends to allow them. This page will only lead to researcher confusion. – mickmackusa Nov 02 '19 at 21:06

4 Answers4

1

You don't need a regex to check for string that is 6 characters long and only numerical.

$val = 'A457718';
if (is_numeric($val) && strlen($val) == 6) {
     echo 'true';
} else {
     echo 'false';
}

Demo: http://sandbox.onlinephpfunctions.com/code/25c426d1bbbfce4a96c8c1ba74cc4a84b66c2435 Functions:
http://php.net/manual/en/function.is-numeric.php
http://php.net/manual/en/function.strlen.php

If for some reason you require it in regex.

preg_match('~^\d{6}$~', $val);

Demo: https://regex101.com/r/oR9bT4/1

chris85
  • 23,846
  • 7
  • 34
  • 51
0

The pattern /^[a-zA-Z0-9]{6}$/ will match six characters that are alphanumeric.

if(preg_match('/^[a-zA-Z0-9]{6}$/', 'A45BBB')){
  // Is valid
}
HenryTK
  • 1,287
  • 8
  • 11
  • The comments under the question contradict this logic -- but I can understand your confusion because the question is Unclear. – mickmackusa Nov 02 '19 at 21:15
-1

for the length, put at the end {0,6} .. this will limit string to be from 0 to 6 characters

Ehab Eldeeb
  • 722
  • 4
  • 12
  • yes,i gave {0,6} at the end.preg_match('/^[A-Z]|[0-9A-Z]*([0-9][A-Z]|[A-Z][0-9])[0-9A-Z]{0,6}*$/i', $val). But i i am getting error like this : preg_match(): Compilation failed: nothing to repeat at offset 52 – user3408779 Jul 21 '15 at 14:41
  • The question is Unclear AND this answer is incorrect. – mickmackusa Nov 02 '19 at 21:14
-1

This would match any 6-length alphanumeric string:

\b[a-zA-Z0-9]{6}\b

or including the underscore character:

\b\w{6}\b

This should be clarified:

But i need to compare if there are any special characters

Angelo Tricarico
  • 1,333
  • 1
  • 19
  • 36
  • No need for the `,6`, `{6}` means 6 characters. The `\w` also matches `_` so it's not just alphanumeric. – chris85 Jul 21 '15 at 14:56