How to create validation with special characters

2

I would like to know how to create a validation with preg_macth() where you have to accept these keyboard characters:

/^([a-zA-Z0-9]{6,40})$/


'~!@#$%^&*()_-+={}[]\|:;"'<>,.?/

Less spaces.

    
asked by anonymous 14.03.2015 / 16:05

2 answers

2

You need to include these characters in the accepted list (the part between [ and ] ). Since in your list some characters have special meaning in regular expressions, you must escape them with \ :

^(['~!@#$%^&*()_\-+={}[\]\\|:;"'<>,\.\?\/a-zA-Z0-9]{6,40})$

See a working example

    
14.03.2015 / 16:15
0

From the analysis I made of your need, using the ASCII table , and the attack command for hexadecimal \x## you can reduce your regex to simply:

^([\x21-\x7E]{6,40})$

That will encompass all your characters.

view:

!	21
"	22
#	23
$	24
%	25
&	26
'	27
(	28
)	29
*	2A
+	2B
,	2C
-	2D
.	2E
/	2F
0-9	30 a 39
:	3A
;	3B
<	3C
=	3D
>	3E
?	3F
@	40
A-Z	41 a 5A
[	5B
\	5C
]	5D
^	5E
_	5F
'	60
a-z	61 a 7A
{	7B
|	7C
}	7D
~	7E
    
14.12.2015 / 15:12