egorig Posted May 26, 2007 Share Posted May 26, 2007 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 More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262122 Share on other sites More sharing options...
egorig Posted May 26, 2007 Author Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262152 Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262155 Share on other sites More sharing options...
egorig Posted May 26, 2007 Author Share Posted May 26, 2007 <?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 https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262165 Share on other sites More sharing options...
MadTechie Posted May 26, 2007 Share Posted May 26, 2007 you could try if (!preg_match('/[0-9a-z@,_.!:\'\s]+/im', $val)) { $result = "You've used irregular symbols"; break; } Link to comment https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262167 Share on other sites More sharing options...
egorig Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks, i figured it out where the problem was Link to comment https://forums.phpfreaks.com/topic/53060-solved-check-for-a-string-validation/#findComment-262178 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.