RegEx to validate usernames - php

I am not very good in Regular Expression, and can't seem to understand them quite well.
I am looking for a regular expression which will match and allow following strings for a username, with these conditions:
username can: start with a number or with a alphabetic letter
username can contain special chars: dots, dashes, underscores
username must be in this range: from 3 chars up to 32 chars.
alphanumeric characters in the username can be both: lowercase and uppercase
cannot contain empty spaces
Almost similar to Twitter's and Facebook username patterns.
Please help me. Thank you.
FWI: I have tried this: /^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/ - and this does not satisfy my conditions.

Try this one
^[a-zA-Z0-9][a-zA-Z0-9\._-]{2,31}$
this results in the php code
if (preg_match('~^[a-zA-Z0-9][a-zA-Z0-9\._-]{2,31}$~', $username) {
//do something
}

Starts with digit or alphabetic
[a-zA-Z0-9]
can contain as above plus dots, dashes and underscores
[a-zA-Z0-9._-]
and all together
[a-zA-Z0-9][a-zA-Z0-9._-]{2, 31}

try this one this is working for me in every registration form
//username Validation
var usernameRegex = /^[a-zA-Z0-9\s\[\]\.\-#']*$/i;
var username=document.getElementById('username');
if(username.value==""){
document.getElementById('lblusername').innerHTML="Username Required!";
username.focus();
return false;
}
else if(usernameRegex.test(username.value)== false)
{
document.getElementById('lblusername').innerHTML="Allow Alphanumeric Only! (E.g Demo123)";
username.focus();
return false;
}
else
{
document.getElementById('lblusername').innerHTML="";
}

Try this:
^[0-9a-zA-Z][0-9a-zA-Z\-\._]{2,31}$

Related

PHP "preg_match" to check whether the text contains small characters [duplicate]

I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.

PHP - Password RegEx requirements

I am trying to validate if a new user account's password is matching these criterias:
Between 8-30 characters long
Contains at least 1 lowercase letter (a-z)
Contains at least 1 uppercase letter (A-Z)
Contains at least 1 of the following special characters: _-!#*#&
I have a function like this:
function validPassword($str) {
return preg_match("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_-!#*#&])[A-Za-z\d_-!#*#&]{8,30}$", $str);
}
But I am getting an error. It should return "true" for this password for example: HelloWorld123!
But instead it is returning false. Any idea what may be wrong?
if (validPassword($password) == true) {
// good password
}
You forgot to escape '-', and delimiters...
function validPassword($str) {
return preg_match("/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_\-!#*#&])[A-Za-z\d_\-!#*#&]{8,30}$/", $str);
}
Your regex is having errors which is why there is no match in the first place.
Change your regex to this:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_\-!#*#&])[A-Za-z\d_\-!#*#&]{8,30}$
Have a look at your regex in action here: https://regex101.com/r/ogPPeb/1

preg_match user validation

Im trying to make a preg_match rule for user validation
to only allow a-z - A-Z - 0-9
and the characters _-=?!#:.,
without spaces
I already tried a lot of combinations but no one seems to work
This is what im trying to get:
if(preg_match('idk what to use here', 'Myusername#123?')) {
return true;
}
if(preg_match('idk what to use here', '$Hello')) {
return false;
}
if(preg_match('idk what to use here', 'Hello 123')) {
return false;
}
Anyone knows the regex for this?
Thanks :)
Use a class, but make sure to either escape the hyphen or put it as the last character, otherwise it signifies a range. Use ^ and $ to require that all input characters match the pattern:
$regex = "/^[A-Za-z0-9_=?!#:.,-]*$/";
var_dump(preg_match($regex, 'Myusername#123?'));
var_dump(preg_match($regex, '$Hello'));
var_dump(preg_match($regex, 'Hello 123'));

PHP preg_match regex to find letters numbers and spaces?

When people sign up to my site I validate their names with this code:
if (preg_match("[\W]", $name))
{
$mess = $mess . "Your name must contain letters only.<br>";
$status = "NOTOK";
}
This is because your actual name cannot contain symbols unless your parents were drunk when they named you.
However, this regex doesn't detect spaces. How can I fix it?
You can use the following regular expression:
^[\w ]+$
This matches any combinations of word characters \w and spaces , but as the guys said be careful because some names might contain other symbols.
So you can use it like this:
if (preg_match("/^[\\w ]+$/", $name)) {
// valid name
}
else {
// invalid name
}
Try this:
<?php
$user_input = 'User_name';
if (!preg_match('/^[a-z0-9_\s]+$/i', $user_input)) {
// Matches English letters, numbers underscores(_) and spaces
$mess = $mess . "Your name must contain letters only.<br>";
$status = "NOTOK";
}
?>
You just missed regexp separators. I do this sometimes even after 10 years of programming.
if (preg_match("/[\W]/", $name)) ...

preg_match with international characters and accents

I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.

Categories