Im validating a form and its easy to validate numbers, letters a-z and so on but when i got to the point where i need to validate a field which must contain only the characters (a-z and special characters such as öçğüiş) with at least one space only I am really stuck!
I tried the following and some other techniques without success:
function validateAlphaSpecial($value) {
if (ereg("/^[\p{L}\s]+$/", $value, $regs)) {
echo 'true';
} else {
echo 'false';
}
}
Has anyone got a solution for this. Thank you.
Ereg has been deprecated as of PHP 5.3.0 like others said and you shouldn't use EREG.
The snippet below fits your validating requirement: a-z and special characters such as öçğüiş with at least ONE SPACE.
if( preg_match("/[\p{L}]\s{1,}+/u", $value) > 0 ) {
echo 'Valid';
} else {
echo 'Not valid';
}
About /u modifier [ from documentation ] :
This modifier turns on additional functionality of PCRE that is
incompatible with Perl. Pattern strings are treated as UTF-8.
Related
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.
I have a strange question maybe you could help me with. I am trying to check whether the given string contains special characters. The code below is working however one character seems to get exempted on the condition which is the square brackets [ ]. Can you help me with this? thank you.
$string = 'starw]ars';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string)) {
echo 'Output: Contains Special Characters';
}else{
echo 'Output: valid characters';
}
Please note: I can't use below condition since I need to accept others characters from other languages like in arabic, chinese, etc. so it means I need to specify all characters that is not allowed.
if (!preg_match('/[^A-Za-z0-9]/', $string))
Appreciate your help. Thanks.
You forgot to add square brackets [] in your expression. I have added this \[\] in your current expression.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = 'starw]ars';
if (preg_match('/[\[\]\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
Use strpos:
$string = 'starw]ars';
if (strpos($string, ']') !== false) {
echo 'true';
}
Please see the following answer for additional information:
How do I check if a string contains a specific word in PHP?
You should add escaped square brackets to your expression.
preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-\[\]]/', $string)
EDIT: Apologies to #Thamilan, I didn't see your comment.
EDIT 2: You could also use the preg_quote function.
preg_match(preg_quote('\'^£$%&*()}{##~?><>,|=_+¬-[]', '/'), $string);
The preg_quote function will escape your special characters for you.
Try this example:
<?php
$string = 'starw]]$%ars';
if (preg_match('/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
?>
Output:
Output: Contains Special Characters
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}$
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.
i need to write a case which only except the a-zA-Z0-9 characters with underscore and white space(1 or more than 1) and ignore all rest of the characters.I wrote a code but its not working properly.
In those case should be wrong but its show OK
1) test msg#
2) test#msg
3) test!msg
also those should be OK but currently shows wrong.
1) test msg.-(Two white space)
what i should to change in my code .pls help and see my code below.
$message=$_GET['msg'];
if(preg_match('/[^A-Za-z0-9]\W/',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}
Here's an optimized version of the one left by riad:
$message = $_GET['msg'];
if ( preg_match('/^[a-z0-9_ ]+$/i', $message) )
{
echo 'Ok';
}
else
{
echo 'Wrong';
}
I've removed the A-Z (uppercase) from the regular expression since the i modifier is used.
I'd also like to explain what you did wrong in the example you provided.
First, by putting the ^ inside the square brackets ([]), you're essentially doing the opposite of what you were trying to do. Place a ^ inside the square brackets means "not including."
You were missing a *, + or ? at the end of the square bracket, unless you only wanted to match a single character. The * character means 0 or more, + means 1 or more and ? means 0 or 1.
The \W means any non-word character. That's probably not what you wanted.
Finally, to starting a regular expression with ^ means that the beginning of the string you're string to match must start with whatever is after the ^. Ending the regular expression with a $ means that the string must end with the characters preceding the $.
So by typing /^[a-z0-9_ ]+$/i you're saying match a string that starts with a-z0-9_ or a space, that contains at least of those characters (+) and ends.
PHP has a lot of documentation of the PCRE regular syntax which you can find here: http://ca2.php.net/manual/en/reference.pcre.pattern.syntax.php.
$message=$_GET['msg'];
if(preg_match('/^[a-zA-Z0-9_ ]+$/i',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}