Check text box for spaces - php

I need help with code to output an error if a text box contains a space or hyphen. I have the following:
elseif($_REQUEST['students']['FIRST_NAME']!= "CONTAINS SPACE OR HYPHENS")
{
$insert_error = 'No spaces, hyphens, or spaces allowed in first name';
}
What function could I use? I have other functions that work for similar tasks such as:
elseif($_REQUEST['students']['PASSWORD']!=$_REQUEST['verify_password'])
{
$insert_error = 'Passwords did not match.';
}
I know it's quite straight forward, but I'm not sure what to use. Forgive me, I'm very rusty.

Use regular expressions and the preg_match() function.
if ( preg_match('/[\s-]/', $_REQUEST['students']['FIRST_NAME']) ) {
$insert_error = 'You cannot enter spaces or dashes';
}
The [\s-] inside of preg_match() is called a regular expression. The \s is for any whitespace character, and the - is for the dash.
More information here: http://php.net/manual/en/function.preg-match.php

$first_name = $_REQUEST['students']['FIRST_NAME'];
elseif((preg_match("/\\s/", $first_name) === true)
")
{
$insert_error = 'No spaces, hyphens, or spaces allowed in first name';
}

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.

How to Use preg_match for New Line

I prevent people from entering characters other than the one on preg_match with this code:
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$input)) {
$error = "Error";
}
It only permits:
A to Z
0 to 9
(space)
But, if people click ENTER. data can not be sent. How do I have to change the code?
You can use \r\n for enter or new line in preg_match.
if (!preg_match("/^[a-zA-Z0-9 \r\n]*$/",$input)) {
$error = "Error";
}
try this
&[^a-zA-Z0-9\s]&
the \s should find any type of whitespace, including newlines and spaces. if you only want spaces and newlines (not tabs or other whitespace) you can do /^[a-zA-Z0-9 ]*$/ instead.
I think the above will work fine... but if it fails you can try this:
if (!preg_match("/(?!\n)^[a-zA-Z0-9\s]*$/",$input)) {
$error = "Error";
}
(?!\n) Negative look ahead will prevent \n from appearing in the string..

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)) ...

Checking to see if a string contains any characters

I would like to check and see if a given string contains any characters or if it is just all white space. How can I do this?
I have tried:
$search_term= " ";
if (preg_match('/ /',$search_term)) {
// do this
}
But this affects search terms like this as well:
$search_term= "Mark Zuckerburg";
I only want a condition that checks for all white space and with no characters.
Thanks!
ctype_space does this.
$search_term = " ";
if (ctype_space($search_term)) {
// do this
}
The reason your regular expression doesn’t work is that it’s not anchored anywhere, so it searches everywhere. The right regular expression would probably be ^\s+$.
The difference between ctype_space and trim is that ctype_space returns false for an empty string. Use whatever’s appropriate. (Or ctype_space($search_term) || $search_term === ''…)
Use trim():
if(trim($search_term) == ''){
//empty or white space only string
echo 'Search term is empty';
}
trim() will cut whitespace from both start and end of a string - so if the string contains only whitespace trimming it will return empty string.

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