I'm trying to check user password with this regular exp:
$regex='/^(?=.*[A-Za-z0-9#])(?=.*\d)[a-zA-Z0-9#]{6,12}$/';
if(isset($_POST['password']) && strlen($_POST['password'])>=6 &&
strlen($_POST['password']<=12) && preg_match($regex, $_POST['password'])){
echo 'ok';
}else{echo 'invalid password';}
I'd like the password to be from 6 to 12 chars, at least one digit and at least one Uppercase.
It doesn't work if the password is something like 12Hello instead it works with Hello12 , someone could please help?
Thanks
Your character class is too broad. You need to check for things separately.
^(?=.*[A-Z])(?=.*\d).{6,12}$
(?=.*[A-Z]) is at least one upper case character.
(?=.*\d) is at least one number
and .{6,12} is 6-12 characters (that aren't new lines)
The ^$ are anchors ensuring the full string matches.
In your regex your character class [A-Za-z0-9#] allows an uppercase character, lowercase, number, or # (which doesn't ensure you have 1 uppercase character). You also don't need the strlen functions if using this regex.
Try this one:-
Minimum 6 character
Atleast 1 uppercase character
At least one digit
Expression:
"/^(?=.*?[0-9])(?=.*[A-Z]).{6,12}$/"
Related
Looked at 2 other regex related questions and both were vastly beyond what I need help with X'D.
<?php
$userName = trim($_POST['username']);
if(preg_match("/^[a-zA-Z].*(?(\d)).{8,}$/", $userName)){
echo 'woot!';
I'm not really sure why its failing. I know its checking the first character must be alpha, then go more * but check at the end if its a digit ?(\d)... But why wont it check if the length of my $userName is 8 or more?
if I do
... *(?(\d){8,}
Its going to look for 8 or more digits as opposed to if the string itself is 8 or more.
User must: start with letter, only end in numbers, and can only contain alphaNumeric, must be at least characters 8 long, and treat lower and uppercase the same. Which is to say I need to add an i at the end of my regex.
As so there is no way to work it into a regex aside form look aheads? ^...$.{8,} wouldn't work?
User must: start with letter, only end in numbers, and can only contain alphaNumeric, must be at least characters 8 long, and treat lower and uppercase the same.
You may use this simple regex to meet this requirement:
^[A-Za-z][a-zA-Z\d]{6,}\d$
RegEx Details:
^: Start
[A-Za-z]: Match an ASCII letter at the start
[a-zA-Z\d]{6,}: Match a letter or digit 6 or more times
\d: Match a digit before end
$: end
RegEx Demo
Use the following regex pattern:
^(?=.{8,}$)[A-Z][A-Z0-9]*[0-9]+$
Sample script:
$userName = trim($_POST['username']);
if (preg_match("/^(?=.{8,}$)[A-Z][A-Z0-9]*[0-9]+$/i", $userName)) {
echo "MATCH";
}
Of note, the regex pattern uses (?=.{8,}$) at the start of the pattern. This is a positive lookahead, which asserts that the match is at least 8 characters.
If you don't want to use a lookahead, you may just explicitly check the username length using strlen():
$userName = trim($_POST['username']);
if (preg_match("/^[A-Z][A-Z0-9]*[0-9]+$/i", $userName) &&
strlen($userName) >= 8) {
echo "MATCH";
}
I am trying to validate a USERNAME in PHP using Regular Expression. But I am failed. My pattern is: /[^a-z0-9_]/
Rules:
username must start number or small letters
username support number, small letters and _
the end of username character is not be _
The following pattern will work: ^[a-z0-9][a-z0-9_]*[a-z0-9]$
^[a-z0-9]: first character may not be an underscore
[a-z0-9_]*: all the others may be anything...
[a-z0-9]$: ...except the last one which can't be an underscore.
Assuming the minimum number of characters is two (if less than three, the underscore never would be permitted), here is the correct pattern:
^[a-z0-9][a-z0-9_]*[a-z0-9]$
First character is small letters or digit (there can be more of them, that's why the + sign. Then any amount (including zero) of characters including underscore. At the end again, at least one letter or digit.
You can test it here:
https://regex101.com/r/C1zPfu/1
You do not want username of two letters or username that has unlimited characters, do you? Consider this solution if you need to limit the number of characters in your username under your situation:
/^[a-z0-9][a-z0-9_]{2,28}[a-z0-9]$/
https://www.tinywebhut.com/regex/4
The part [a-z0-9] exactly matches one character which can be only small letter or number. The middle part [a-z0-9_]{2,28} matches any small letter or number up to 2 or 28 characters including underscore. The final part [a-z0-9] exactly matches one character which can be only small letter or number. Therefore, this regular expression matches username that has at least 4 characters and 30 characters at the most. If you change your mind and want to include both small and capital letters, you'll have to add a modifier i:
/^[a-z0-9][a-z0-9_]{2,28}[a-z0-9]$/i
https://www.tinywebhut.com/regex/5
So I thought I had this right?
if(!preg_match('^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{7,}$', $passOne)) {
$msg = "Password does not contain at least 1 number/letter, 8 character minimum requirement.";
}
I test it over at https://regex101.com/ and put ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+.{7,}$ to work and things like RubyGlue12 pass and is a match and other things that aren't.
But no matter what, I cannot make any match in the actual PHP code. Even if the POST is a variable manually.
edit: $_POST['password'] is $passOne
Help?
You have .+.{7,} that does not make much sense since it means match any characters 1 or more times, and then any characters 7 or more times.
1 letter, 1 digit and min 8 characters regex will look like
(?i)^(?=.*[a-z])(?=.*\d).{8,}$
Regex explanation:
(?i) - Turning case sensitivity off (unless you need to check for 1 uppercase and 1 lowercase letters - then, remove the i flag and replace (?=.*[a-z]) with (?=.*[a-z])(?=.*[A-Z]))
^ - Start of string
(?=.*[a-z]) - Positive look-ahead requirement to have at least 1 letter
(?=.*\d) - Positive look-ahead requirement to have at least 1 digit
.{8,} - At least 8 characters
$ - End of string.
And PHP code is:
$re = "/^(?=.*[a-z])(?=.*\\d).{8,}$/i";
$passOne = "RubyGlue12";
//$passOne = "eee"; // uncomment to echo the error message
if(!preg_match($re, $passOne)) {
echo "Password does not contain at least 1 number/letter, 8 character minimum requirement.";
}
And yes, with preg_match function, you must use some regex delimiters. I am using / here.
Here's a regex that tests for what the password should not be, instead of what it should be.
/^(.{0,7}|[^a-z]*|[^\d]*)$/i
Example:
if (preg_match('/^(.{0,7}|[^a-z]*|[^\d]*)$/i', $passOne)) {
echo "Validation failed.\n";
}
Explanation:
There are essentially 3 separate tests within the regex (each separated by a |, and each are case-insensitive due to the i option at the end). If it passes any of the tests, then the entire validation fails.
Test 1: Does the entire string only contain 0-7 characters? Fail.
Test 2: Does the entire string contain no alpha characters? Fail.
Test 3: Does the entire string contain no digits? Fail.
I need a regular expression which should have at least one numeric character, both uper and lower case letters allowed, special characters also allowed I am using this expression
/^.*(?=.{6,10})(?=.*\d)(?=.*[a-zA-Z]).*$
but it is not valid for max characters 10.
Seems like you want something like this,
^(?=.*\d)(?=.*?[a-zA-Z])(?=.*?[\W_]).{6,10}$
The above regex would allow 6 to 10 characters only. And it also checks for at-least one digit, upper or lowercase letter and at-least one special character (characters other than letters and numbers).
The following regular expression will limit your length and allow special characters.
^(?=.*\d)(?=.*[a-zA-Z]).{6,10}$
I need to check if string has anything else than what specified below
it must begins from letter (either uppercase or lowercase)
can have alphabetical characters
can have numeric characters
can have dashes
can have minus sign -
can have underscore _
can have comas ,
can have dots .
length from 4 to 35 caracters no more no less
everything else should not be in this string have have
i am stuck on this:
preg_match('/^[\w]{4,35}$/i', $username)
if( !preg_match('/^[a-zA-Z][a-zA-Z0-9_,.-]*$/', $username ))
echo "failed\n";
preg_match('/^[\w]{4,35}$/i', $username)
OK lets see what this doesn't work :
Match the beginning of the string, followed by [a-zA-Z0-9_] at least 4 times with a maximum of 35 times. This is quite different from your requirements.
Instead what you should use :
/^[a-zA-Z][-,.\w]{3,34}$/
The case sensitivity i modifier is not needed. Also I don't think this is exactly what you want. Usually you would need to specify a minimum length which you don't. This can match "a" for example (not a good username)