Jump to content

Recommended Posts

I need to validate a text area using php.

I want to allow numbers, text, and these characters only:

#=|/€$£^"&;_,?.@-:()%+*\[]><]+$/

 

<?php
if(!preg_match('/^[a-z0-9#=|/€$£^"&;_,?.@-)%+*\[]><]+$/'), $_POST['message']) )
?>

 

This is what i have so far but all i get is:

 Parse error: syntax error, unexpected ',' in C:\wamp\www\send.php on line 38

(the above line of code is line 38).

 

Also the original php codes does not have those numbers &#8364;$&#163 in the string, the phpfreaks forum adds them for some reason.

 

I dont have much experience using the preg_match function, any help would be good!

 

cheers.

Link to comment
https://forums.phpfreaks.com/topic/121568-preg_match-validation/
Share on other sites

It seems you have a few inherent problems with your pattern.

 

a) Notice this part of your pattern: @-:  That dash, I assume is meant to be a dash.. but dashes in a character class need to be set first, otherwise, you are telling regex engine that this is a range (much like a-z).

b) I think you need to escape your parethesis characters, as well as your square brackets.. otherwise regex thinks this is a group and a character class respectively.

c) you need to escape your '/' character, because you are using these as delimiters as well.

 

So here is the pattern corrected of those issues:

if(!preg_match('/^[-a-z0-9#=|\/€$£^"&;_,?.@:\(\)%+*\[\]><]+$/'), $_POST['message']))

thanks for the case sensitive suggestion, i hadn't even noticed!

 

ive taken the extra bracket away but same problem.

 

I think it has something to do with the £.

 

I now have this:

if(!preg_match('/^[a-z0-9!?+&%\,\'@#() \$\"]+$/i', $_POST['message'])

 

it works, but I would like to include £ and €.

 

Also, another problem I have is that i need to skip white space, because when I press enter in the textfield this shows up as an error (because of the newline/white space).

 

Ive read that you can use the s flag :"s - Includes New line".

 

How do i do this with the i flag as well? Will this fix my problem?

 

Thanks

thanks nrg_alpha,

 

your explanation did help, but the validation still shows as error when i type £ and also $. I think most of the other symbols are fine though.

 

one main problem i still have is when i type enter/return, that shows up as an error and i need to allow them.

 

 

thanks nrg_alpha,

 

your explanation did help, but the validation still shows as error when i type £ and also $. I think most of the other symbols are fine though.

 

one main problem i still have is when i type enter/return, that shows up as an error and i need to allow them.

 

You have those errors because you are missing those elements in your last snippet you provided..

try:

 

if(!preg_match('/^[a-z0-9!?+&\/£%,\'@#()\$\"]+$/i', $str))

This should work, and allows linebreaks (by removing them before the validation):

 

<?php
$_POST['message'] = str_replace("\n", '', str_replace("\r", '', $_POST['message']));
if (!preg_match('~^[-0-9a-z#=|/€$£"&;_,?.@)%+*^[\]>\\\<]+$~iD', $_POST['message'])) {
echo 'Error!';
}
?>

 

A backslash is escaped with two backslashes (that's what worked for me at least) and the only other escaped character is ].

 

If you also wanna allow single quotes, just add one after the double quote.

Of course you don't want to remove the linebreaks from the original string, just do it for the string to test. E.g. use this instead:

 

<?php
$string = str_replace("\n", '', str_replace("\r", '', $_POST['message']));
if (!preg_match('~^[-0-9a-z#=|/€$£"&;_,?.@)%+*^[\]>\\\<]+$~iD', $string)) {
//error
} else {
//$_POST['message'] is OK, process it further
}
?>

nrg_alpha, i was talking about the first one you gave me, tryd them both but both give errors when i type £ or €, which they should be allowing!

 

Oh well, its not too important i have the majority of the symbols which are useful. Thanks.

 

thebadbad, thanks, but i found this somewhere and it works pretty well:

 

 

<?php function clean($str){//Removes leading and trailing white space and removes any multiple white space in the source string
return preg_replace('/(\s\s+)/', ' ', trim($str));
} ?>

 

but your example still wont except £ or €!

My code does accept € and £ - you must have some problems with the character encoding. Be sure that the PHP file and the actual HTML handling the form use the same character encoding (UTF-8 is recommended).

 

If you want to allow spaces, add \s inside the character class (square brackets), after the underscore for example.

For special characters, you might consider the unicode reprentation \uXXXX, where XXXX is a 4-digit number (with filler-zeros if necessary) that represents the unicode character.

 

Here is a chart of the codes: http://www.geocities.com/Athens/Academy/4038/graph/fontset.htm

 

So, for instance, the code for the pound symbol is 163, so try replacing the pound symbol with \u0163 in your regex string.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.