suttercain Posted May 11, 2009 Share Posted May 11, 2009 Hi everyone, I am validating a form field and sometimes the string will have a + sign in it. Sadly I cannot get the regex to work with the preg_match. Here is how I am trying it: <?php if (!preg_match('/^[A-Za-z0-9\/\\+\-\_]$/', $username) ?> So the user can enter uppercase, lowercase, numbers, a minus sign or underscore but it still comes up with an error if they use a plus sign. Any suggestions? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/ Share on other sites More sharing options...
Maq Posted May 11, 2009 Share Posted May 11, 2009 Try: if (!preg_match('/^[A-Za-z0-9\+\-\_]+$/i', $username)) You were escaping the escape for the plus sign. You also were missing case-insensitive parameter 'i' and a parenthesis. Do you want it to start or end with a specific character, cause you don't need the '^' and '$' if so. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-831909 Share on other sites More sharing options...
nrg_alpha Posted May 11, 2009 Share Posted May 11, 2009 If using the i modifier, you don't need to list both upper and lower cases.. one or the other will work. The underscore doesn't require escaping, and the dash doesn't if placed as the first or last character in a character class: if (!preg_match('/^[a-z0-9\+_-]+$/i', $username)) Generally, PCRE \w covers a-zA-Z0-9_ if (!preg_match('/^[\w\+-]+$/i', $username)) So I suppose you could sub all those characters with that.. but it may return more than bargained for (locale dependent I believe.. you would have to test it).. Safer bet is the first example. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-831914 Share on other sites More sharing options...
suttercain Posted May 12, 2009 Author Share Posted May 12, 2009 Thanks guys. I tried using your code Maq but the plus sign is still not validating. Here is the custom function I am using. <?php function check_username($username) { global $taken_usernames; $resp = array(); $username = strtoupper(trim($username)); if (!$username) { $resp = array('ok' => false, 'msg' => "Please a VDEC"); } else if (!preg_match('/^[A-Za-z0-9\+\-\_]+$/i', $username)) { $resp = array('ok' => false, "msg" => "Your username can only contain alphanumerics and period, dash and underscore (.-_)"); } else if (in_array($username, $taken_usernames)) { $resp = array("ok" => false, "msg" => "The VDEC has been verified."); } else { $resp = array("ok" => true, "msg" => "The VDEC cannot be found."); } return $resp; } ?> When I enter a string with a plus one of two things happens. 1. If I write 55h+ I get no problems, but if I add another character after the plus like 55h+5 I'll then get the "Your username can only contain alphanumerics and period, dash and underscore (.-_)" error message. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832354 Share on other sites More sharing options...
Maq Posted May 12, 2009 Share Posted May 12, 2009 Hmm, it looks right, you sure you're passing through the right username? It works for me in my test scenario: $username = "55h+5"; if(!preg_match('/^[A-Za-z0-9\+\-\_]+$/i', $username)) { echo "bad"; } else { echo "good"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832364 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Maq - you don't need both A-Z and a-z if you're using the i modifier. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832373 Share on other sites More sharing options...
Maq Posted May 12, 2009 Share Posted May 12, 2009 Maq - you don't need both A-Z and a-z if you're using the i modifier. Yeah I know, I just copied and pasted a portion of the OP's code to run a test, but thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832384 Share on other sites More sharing options...
suttercain Posted May 12, 2009 Author Share Posted May 12, 2009 Ahhh.... I think I may know the issue. That PHP is part of some AJAX code. User enters some info, it's instantly checked against a MySQL database and tells the user if it's a match or not. I wonder if the + sign is having an issue when being passed through the javascript. this.timer = setTimeout(function () { $.ajax({ url: 'comform.php', data: 'action=check_username&username=' + t.value, dataType: 'json', type: 'post', success: function (j) { Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832388 Share on other sites More sharing options...
suttercain Posted May 12, 2009 Author Share Posted May 12, 2009 Yup, that's the issue. I entered dd+d but when it's echoed it shows with whitespace, dd d instead. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-832399 Share on other sites More sharing options...
rea|and Posted May 13, 2009 Share Posted May 13, 2009 Yup, that's the issue. I entered dd+d but when it's echoed it shows with whitespace, dd d instead. The whitespace is url-encoded as plus (+) so I guess you need to urlencode your "t.value" before passing it to json. Try to search urlencode in the js section of this forum. Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-833195 Share on other sites More sharing options...
Adam Posted May 14, 2009 Share Posted May 14, 2009 I'm not sure where I read this but you can only put the hyphen in a character set if its on the far left or right side, and you don't escape it. Also I don't think you need to escape underscores, but if you just use \w you don't need to worry about it. So it could be: /^[\w\+-]+$/ ??? Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-833957 Share on other sites More sharing options...
Maq Posted May 14, 2009 Share Posted May 14, 2009 I'm not sure where I read this but you can only put the hyphen in a character set if its on the far left or right side, and you don't escape it. Also I don't think you need to escape underscores, but if you just use \w you don't need to worry about it. So it could be: /^[\w\+-]+$/ ??? I think nrg_alpha covered this in his post (not sure about the dash): http://www.phpfreaks.com/forums/index.php/topic,251852.msg1182619.html#msg1182619 Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-834015 Share on other sites More sharing options...
Adam Posted May 14, 2009 Share Posted May 14, 2009 Ah yeah my mistake! Haha I wouldn't be surprised if I read his reply a few days ago and that's where it came from! Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-834019 Share on other sites More sharing options...
Maq Posted May 14, 2009 Share Posted May 14, 2009 Ah yeah my mistake! Haha I wouldn't be surprised if I read his reply a few days ago and that's where it came from! haha, that would be funny and sad at the same time. It would also indicate to say no to drugs, at least more often... Quote Link to comment https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/#findComment-834024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.