Space validation in full name field - php

I want to place check in Full name field that full name field should accept space between first and last name using i am using strrpos() function for it but not working

You could use a regex...
if (preg_match("/(.+)( )(.+)/", $full_name))
{
// returns true if name is formed of two words with a space between
}
For even better validation, you can use \w although keep in mind that it will only match English word characters. See here for more info: Why does \w match only English words in javascript regex?
preg_match("/(\w+)( )(\w+)/", $full_name)

Related

How to replace the initial form of an Arabic letter using PHP?

I have an Arabic text and want to replace "Initial" & "Medial" forms of some letters (not all forms) with other letters or characters;
Example:
$text = '...وقد تم تصميم وبناء جميع مكونات الطائرة';
I need to replace the Initial form of letter "ت" which is in the word "تم" with another letter; available "ت" in "مكونات" that is the Final form of this letter shall not be replaced.
It seems character codes (Unicode) cannot be used in str_replace() to find a specific form of a letter and replace it.
Note:
Most Arabic letters have different froms:
Initial form: used in the start of a word, like "ت" in "تم".
Medial form: used in the middle of a word, like "ت" in "نستعين".
Final form: used in the last of a word, like "ت" in "مكونات".
see wikipedia.org for more information.
Here i have given piece of code..hope will work for you$text = '...وقد تم تصميم وبناء جميع مكونات الطائرة';
$a=array('ت','تم');
$b=array('ت','مكونات');``
echo str_replace($a,$b,$text);
Letter forms are used for output only. They shouldn't be stored and/or manipulated in this way. You should find another way to do what you want.
Try RegExp.
This might help you. http://php.net/manual/en/regexp.reference.unicode.php
Or, if you insist on using str_replace, you can do this.
str_replace(
' ت',
' وت',
$string
);

Accented character validation in PHP

What is a good one-liner php regex for checking first/last name fields with accented characters (in case someone's name was Pièrre), that could match something like:
<?php
$strErrorMessage = null;
if(!preg_match('/\p{L}0-9\s-+/u', trim($_POST["firstname"])))
$strErrorMessage = "Your first name can only contain valid characters, ".
"spaces, minus signs, or numbers.";
?>
This tries to use unicode verification, from this post, but doesn't work correctly. The solution seems pretty hard to google.
Aside from the difficulty to validate a name, you need to put your characters into a character class. /\p{L}0-9\s-+/u matches only on a sequence like "Ä0-9 ------". What you wanted to do is
/^[\p{L}0-9\s-]+$/u
Additionally I added anchors, they ensure that the regex tries to match the complete string.
As ex3v mentioned you should probably add \p{M} to that class to match also combination characters. See Unicode properties.
/^[\p{L}\p{M}0-9\s-]+$/u

REGEX at last one uppercase and one number

I searched everywhere but i couldn't find the right regex for my verificaiton
I have a $string, i want to make sure it contains at last one uppercase letter and one number. no other characters allowed just numbers and letter. is for a password require.
John8 = good
joHn8 = good
jo8hN = good
I will use preg_match function
The uppercase and letter can be everywhere in the word, not only at the begging or end
This should work, but is a bit of a mess. Consider using multiple checks for readability and maintainability...
preg_match('/^[A-Za-z0-9]*([A-Z][A-Za-z0-9]*\d|\d[A-Za-z0-9]*[A-Z])[A-Za-z0-9]*$/', $password);
Use lookahead:
preg_match('/^(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$/', $string);
Use this regex pattrn
^([A-Z]+([a-z0-9]+))$
Preg_match
preg_match('~^([A-Z]+([a-z0-9]+))$~',$str);
Demo
Your requisition need "precise syntax description", and a lot of examples for assert your description. Only 3 or 4 examples is not enough, is very open.
For last confirmed update:
preg_match('/^([a-z]*\d+[a-z]*[A-Z][a-z]*|[a-z]*[A-Z][a-z]*\d+[a-z]*)$/',$str)
History
first solution preg_match('/^[A-Z][a-z]+\d+$/',$str)
After your edit1: preg_match('/^[a-z]*[A-Z][a-z]*\d+$/',$str)
After your comment about utf8: hum... add at your question the valid language. Example: "José11" is a valid string?
After your edit2 ("jo8hN" is valid): and about number, can repeat? Well I suppose not. "8N" is valid? I suppose yes. preg_match('/^([a-z]*\d+[a-z]*[A-Z][a-z]*|[a-z]*[A-Z][a-z]*\d+[a-z]*)$/',$str) you can add more possibilities with "|" in this regex.

what is the proper way of using an if condition with RegEx

I have been trying to validate a form where the input is the first and last name using regex in PHP. All I need the regex to do is check to make sure that there are no numbers. This is what I have right now:
if (preg_match('/\A\b[^0-9]*\W[^0-9]*\b\Z/sm', $name)) {
# Successful match
$nameError = "";
echo $name;
} else {
# Match attempt failed\
$nameError = "No Numbers";
}
The $name variable holds First and last name. I have been trying to make this work and I have not been able to get the input to match the regex. Am I using this correctly or do I need to input it in another way. Thank you for your help
if name is surename and first name you should use condition depending on country for example in Poland it would be
preg_match('/[a-z]+ [a-z]+/i',$name);
It means that all the names that contains two part that are alphabetic with space separating them are good. If you want first letter of name to be upper you should change it to
preg_match('/[A-Z][a-z]+ [A-Z][a-z]+/',$name);
Preg_match returns true if $name is validated by regular expression that you provide in the first argument.
So your usage of this function is okay, you should check your expression.
http://pl1.php.net/preg_match
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
You can always check your regex on online checker for example
http://www.solmetra.com/scripts/regex/
If you just want two words separated by one space, this will do what you want: if (preg_match('/^[A-Za-z]+ [A-Za-z]+$/', $name))
Thank you all for your replies, I found the answer in the most obvious place though and it didn't have anything to do with the regex. I forgot to setup the variables correctly for using them on the same page as the form. Stupid mistake. Anyway, thank you again.

Using regex to filter strings by length

I am trying to validate a input field with regex with this pattern [A-Za-z]{,10}
I want it to only find a match if 10 or less chars was sent in the input field, the problem I get is that it will match all words that is less then 10 chars.
Is there a way to say if there is more then 10 chars in the input field come back as false, or is it just better to do a strlen with php?
If you need to validate that it's alphabetic only, don't use strlen(). Instead, put boundaries (^$) on your regex:
/^[A-Za-z]{,10}$/
There is an important syntax mistake being made here by the OP and all the current regex answers: When using the curly brace quantifier with PHP (PCRE), you need to specify the first number. (i.e. The expression: {,10} is NOT a valid quantifier!) Although the comma and second number are optional, the first number in a curly brace quantifier is required. Thus the expression should be specified like so:
if (preg_match('/^[A-Za-z]{0,10}$/', $text))
// Valid input
else
// Invalid input
If you just match ^[A-Za-z]{,10}$ it will check that the whole string is 10 or less.
^[A-Za-z]{,10}$
$##$^&$a36#&$^ will pass your regexp because a is substring of it and a pass [A-Za-z]{,10}
If you are only interested in the length of the string, regex is heavy-handed and you should use strlen instead.

Categories