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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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;
		}

Link to comment
Share on other sites

<?php

$input = "this";

if (preg_match('/[^0-9a-z@,_.!:\'\s]+/im', $input)) {

echo "Successful match";

} else {

echo "Match attempt failed";

}

die;

?>

 

It says match attempt failed :(, but this string consist of regular symbols.

Link to comment
Share on other sites

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.