PHP preg_match - only allow alphanumeric strings and - _ characters
Code:
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
echo "not valid string";
}
Explanation:
- [] => character class definition
- ^ => negate the class
- a-z => chars from 'a' to 'z'
- _ => underscore
- - => hyphen '-' (You need to escape it)
- 0-9 => numbers (from zero to nine)
The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z
How to use preg_match to only allow strings containing alphabetic and numeric characters?
preg_match('/^[0-9A-Z]*([0-9][A-Z]|[A-Z][0-9])[0-9A-Z]*$/', $subject);
If you want to allow small and capital letters, add an i
at the end of the pattern string.
Explanation:
[0-9][A-Z]
matches one digit followed by one capital letter
[A-Z][0-9]
matches one capital letter followed by one digit
([0-9][A-Z]|[A-Z][0-9])
matches one of these two sequences
[0-9A-Z]*
matches 0-n digits and/or capital letters
The idea is: A string which contains both (and only), letters and numbers, has at least one subsequence where a letter follows a digit or a digit follows a letter. All the other characters (preceding and following) have to be digits or letters.
Allow only 11 or 16 alphanumerical characters PHP
Instead of outlining the rest of your requirements in comments you should update your question. Based on what I read, try this: /^[a-zA-Z0-9]{5,16}$/
Alpha-numeric, minimum 5 and maximum 16 characters repetition.
PHP regex to match alphanumeric and hyphens (not spaces) for use as subdomain
Your pattern matches an ASCII letter or digit or -
anywhere inside a string, and if found, returns true. E.g., it will return true if you pass $#$%$&^$g
to it.
A pattern that will match a string consisting of just alphanumeric characters and hyphens is
if (preg_match('/^[A-Za-z0-9-]+$/D', $subdomain)) {
// valid subdomain
}
Details:
^
- start of string[A-Za-z0-9-]+
- 1 or more chars that are either ASCII letters, digits or-
$
- end of string/D
- a PCRE_DOLLAR_ENDONLY modifier that makes the$
anchor match at the very end of the string (excluding the position before the final newline in the string).
php only allow letters, numbers, spaces and specific symbols using pregmatch
Use
preg_match('/^[a-z0-9 .\-]+$/i', $firstname)
PHP Regular expression for only allowing alphanumeric, dashes, underscores
The regex is incorrect. Move -
in last or beginning or escape it in character class because -
have special meaning inside character class. It denotes range
within character class. You can use
^[-a-zA-Z0-9_]+$
Also
\w = [a-zA-Z0-9_]
So, you can use
^[\w-]+$
Also, there is no need of i
modifier and finally yes it will allow only A-Z both upper and lowercase, numbers, dashes, and underscores
This will suffice in PHP
preg_match('/^[\w-]+$/', $_POST['sign_up_username'])
Regex only allow alphanumeric and at least two characters
I would look at
^[a-zA-Z0-9]{2,}$
start string, any character in a-z or A-Z or 0-9, matched two or more times, end string.
http://regex101.com/r/sN7tX4
PHP preg_match to allow only numbers,spaces '+' and '-'
You have to add a *
as a quantifier to the whole character class and add anchors to the start and end of the regex: ^
and $
means to match only lines containing nothing but the inner regex from from start to end of line. Also, the i
modifier is unnecessary since there is no need for case-insensitivity in this regex.
This should do the work.
if(!preg_match('/^[0-9 +-]*$/', $var)){
//variable contains char not allowed
}else{
//variable only contains allowed chars
}
Related Topics
Use Curl with Sni (Server Name Indication)
Expose "Use" Classes to Included File
A Script to Change All Tables and Fields to the Utf-8-Bin Collation in MySQL
How to Post Multiple <Input Type="Checkbox" /> as Array in PHP
What Are the Risks of PHP Sessions
Setting Environment Variables in Mamp
How to Store Images in MySQL Database Using PHP
Google-Like Search Engine in PHP/Mysql
PHP $Stmt->Num_Rows Doesnt Work by Prepared Statements
How to Implement Single Sign-On (Sso) Using Microsoft Ad for an Internal PHP App
Pdo Multiple Named Placeholders Doesnt Retrieve Data
Apple Push Notification with Sending Custom Data
How to Only Use Created_At in Laravel
Strategy to Override a Class in a Library Installed with Composer
PHP Sessions Login with Remember Me
Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."