$pattern="/[a-z]*[a-z]*/i";
if(!preg_match($pattern, $value)){
$this->error_name="The name should contain at least two letters.";
}
I am trying to check if the user types his name with at least two letters. So basically, he cant enter his name as such 111111111111.. it must have two letters.
The regular expression that I wrote doesnt work..why?
You can use:
$pattern="/^[a-z]{2,}$/i";
This will ensure that the name has only letters and there are at least 2 letters in the name.
Edit:
Looks like you want the name to contain at least two letter and can contain other non-letters as well:
$pattern="/^.*[a-z].*[a-z].*$/i";
Try this (your code modified):
$pattern="/^[a-z]{2}.*/i";
if(!preg_match($pattern, $value)){
$this->error_name="The name should contain at least two letters.";
}
Returns true when at least two alphabets are used in a string:
preg_match_all('/[a-z]/', $str, $m) >= 2;
$pattern="/[a-z].*[a-z]/i";
if(!preg_match($pattern, $value)){
$this->error_name="The name should contain at least two letters.";
}
Your code didn't work because * means zero or more times, so it would also match none or one character.
$pattern="/^([a-z]{2})|([a-z].*[0-9].*[a-z].*).*/i";
I think the answer should be the one above.. your answers gave me a clue..
now it will match also those names:
a1111111111111a
Related
This php code is not catching numeric characters or symbols as it should.
$firstname = check_input($_POST['firstname'], "Please enter your first name");
$firstname = ucwords($firstname);
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
show_error("Name should have only alpha characters and white space");
}
For example, the results should be Geronimo. If ge5on*mo is entered, that's what gets returned.
Any suggestions?
Well, no full test cases in your example. So I will try to walk blindly in the dark. At first your expression is flawed, because it can capture just empty spaces without any name. Second - it doesn't captures unicode characters. I would start with such expression for name capture :
^(?:\p{Lu}\p{Ll}+[-\h]?){1,}$
Explanation:
^(?:
\p{Lu} // first letter in name must be in upper-case
\p{Ll}+ // after that goes at least 1 non-upper case letter
[-\h]? // we allow separators between names
){1,} // human can have multiple names (at least 1)
$
I want to check is the name valid with regex PHP, but i need a unique regex that allows:
Letters (upper and lowercase)
Spaces (max 2)
But there can't be a space after space..
For example:
Name -> Dennis Unge Shishic (valid)
Name -> Denis(space)(space) (not valid)
Hope you guys understand me, thank you :)
First, it's worth mentioning that having such restrictive rules for the names of persons is a very bad idea. However, if you must, a simple character class like this will limit you to just uppercase and lowercase English letters:
[A-Za-z]
To match one or more, you need to add a + after it. So, this will match the first part of the name:
[A-Za-z]+
To capture a second name, you just need to do the same thing preceded by a space, so something like this will capture two names:
[A-Za-z]+ [A-Za-z]+
To make the second name optional, you need to surround it by parentheses and add a ? after it, like this:
[A-Za-z]+( [A-Za-z]+)?
And to add a third name, you just need to do it again:
[A-Za-z]+( [A-Za-z]+)? [A-Za-z]+
Or, you could specify that the latter names can repeat between 1 and 2 times, like this:
[A-Za-z]+( [A-Za-z]+){1,2}
To make the resulting code easy to understand and maintain, you could use two Regex. One checking (by requiring it to be true) that only the allowed characters are used ^[a-zA-Z ]+$ and then another one, checking (by requiring it to be false) that there are no two (or more) adjacent spaces ( ){2,}
Try following working code:
Change input to whatever you want to test and see correct validation result printed
<?php
$input_line = "Abhishek Gupta";
preg_match("/[a-zA-Z ]+/", $input_line, $nameMatch);
preg_match("/\s{2,}/", $input_line, $multiSpace);
var_dump($nameMatch);
var_dump($multiSpace);
if(count($nameMatch)>0){
if(count($multiSpace)>0){
echo "Invalid Name Multispace";
}
else{
echo "Valid Name";
}
}
else{
echo "Invalid Name";
}
?>
A regex for one to three words consisting of only Unicode letters in PHP looks like
/^\p{L}+(?:\h\p{L}+){1,2}\z/u
Description:
^ - string start
\p{L}+ - one or more Unicode letters
(?:\h\p{L}+){1,2} - one or two sequences of a horizontal whitespace followed with one or more Unicode letters
\z - end of string, even disallowing trailing newline that a dollar anchor allows.
I am taking an input from the user in form field. I wanna make sure that the given input should be in the pattern like "2012-xxx-111" i-e 1st four should be integers and there should be a "-" sign after that there should be two or three alphabets after that the "-" and at the end any integer value consisting of 3 numbers. Help me doing all this in php. Thanks
your help would be appreciated .
You can use a regex for that.
[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}
This will match any number between 0 and 9 four times, following by a -, followed by 2 or 3 letters from a to z, lowercase or uppercase, and finally another - and 3 more numbers.
http://regexr.com/3bo81
In PHP, you can use preg_match() to see if a string matches a given pattern.
Use preg_match with a regex
if (preg_match("/^[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}$/", "Search String Here")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Alright, so I want the user to be able to enter every character from A-Z and every number from 0-9, but I don't want them entering "special characters".
Code:
if (preg_match("/^[a-zA-Z0-9]$/", $user_name)) {
#Stuff
}
How is it possible for it to check all of the characters given, and then check if those were matched? I've tried preg_match_all(), but I didn't honestly understand much of it.
Like if a user entered "FaiL65Mal", I want it to allow it and move on. But if they enter "Fail{]^7(,", I want it to appear with an error.
You just need a quantifier in your regex:
Zero or more characters *:
/^[a-zA-Z0-9]*$/
One or more characters +:
/^[a-zA-Z0-9]+$/
Your regex as is will only match a string with exactly one character that is either a letter or number. You want one of the above options for zero or more or one or more, depending on if you want to allow or reject the empty string.
Your regular expression needs to be changed to
/^[a-zA-Z0-9]{1,8}$/
For usernames between 1 and 8 characters. Just adjust the 8 to the appropriate number and perhaps the 1.
Currently your expression matches one character
Please keep in mid that preg_match() and other preg_*() functions aren't reliable because they return either 0 or false on fail, so a simple if won't throw on error.
Consider using T-Regx:
if (pattern(('^[a-zA-Z0-9]{1,8}$')->matches($input))
{
// Matches! :)
}
I'm newest and happy to be here in Stackoverflow. I am following this website since a long time.
I have an input/text field. (e.g. "Insert your name")
The script starts the following controls when the user sends data:
1) Control whether the field is empty or not.
2) Control whether the field goes over maximum characters allowed.
3) Control whether the field contain a wrong matched preg_match regex. (e.g. it contains numbers instead of only letters - without symbols and accents -).
The question is: why if i put this characters "w/", the script doesn't make the control? And it seems like the string bypass controls?
Hello to all guys and sorry if I'm late with the answer (and also for the non-code posted ^^).
Now, talking about my problem. I checked that the problem is on ONLY if I work offline (I use EasyPhp 5.3.6.1). Otherwise the regEx tested online is ok.
This is the code I use to obtain only what I said above:
if (!preg_match('/^[a-zA-Z]+[ ]?[a-zA-Z]+$/', $name)) {
echo "Error";
}
As you can see, this code match:
A string that start (and finish) with only letters;
A string with only 0 or 1 empty space (for persons who has two name, i.e.: Anna Maria);
...right?!
(Please correct me if I am wrong)
Thanks to all!
Wart
My reading of the requirements is
Only letters (upper or lower) can be provided.
Something must be provided (i.e. a non-zero length string).
There is a maximum length.
The code below checks this in a very simple manner and just echos any errors it finds; you probably want to do something more useful when you detect an error.
<?php
$max = 10;
$string = 'w/';
// Check only letters; the regex searches for anything that isn't a plain letter
if (preg_match('/[^a-zA-Z]/', $string)){
echo 'only letters are allowed';
}
// Check a value is provided
$len = strlen($string);
if ($len == 0) {
echo 'you must provide a value';
}
// Check the string is long to long
if ($len > $max) {
echo 'the value cannot be longer than ' . $max;
}
You can also try this:
if (preg_match('/^[a-z0-9]{1,12}/im', $subject)) {
\\ match
}
The above will only match similar to #genesis' post, but will only match if the $subject is between and including 1 - 12 characters long. The regex is also case insensitive.
It works fine.
<?php
$string = '\with';
if (preg_match('~[^0-9a-z]~i', $string)){
echo "only a-Z0-9 is allowed"; //true
}
http://sandbox.phpcode.eu/g/18535/3
You have to make sure you don't put user input into your regex, because that would mean you'll probably check something wrong.