Jump to content

[SOLVED] Check for a string validation


egorig

Recommended Posts

Hi,

  I want to check if a string validates some of my rules. My string must consist only : letters from a to z and A to Z, all the digits 0-9 and these symbols - '@','_','.','!',':' and space. The only thing i know is that a 'ereg' function must be used. I've seen so many examples but i can't get how exactly does ereg work. Another rule for the string is using cyrillic letters.

 

Thanks so much in advance !!!

Link to comment
https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/
Share on other sites

what about this

<?php
$input = "whatever@nothing";
$input= preg_replace('/[^0-9a-z@,_.!:\'\s]+/im', '', $input);
echo $input; // "whatevernothing";
?>

 

edit:

opps to match use

<?php
if (preg_match('/[^0-9a-z@,_.!:\'\s]+/im', $input)) {
# Successful match
} else {
# Match attempt failed
}
?>

 

also added 0-9

it doesn't work with spaces :( !

Here's the function i've made

 

	function CheckField()
{
	foreach($_POST as $val)
	{
		if (strlen($val) == 0)
		{
			$result = "You've missed a field !";
			break;
		}
		if (!preg_match('/[^0-9a-z@,_.!:\'\s]+/im', $val))
		{
			$result = "You've used irregular symbols";
			break;
		}
	}
	return $result;
}

Works fine

<?php
$input = "this 'should' WORK@phpfreaks 555-1234";
if (preg_match('/[^0-9a-z@,_.!:\'\s]+/im', $input)) {
echo "Successful match";
} else {
echo "Match attempt failed";
}

die;
?>

Successful match

 

 

you could try

		if (!preg_match('/[0-9a-z@,_.!:\'\s]+/im', $val))
		{
			$result = "You've used irregular symbols";
			break;
		}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.