Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/157732-allowing-the-plus-sign-in-a-sting/
Share on other sites

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.

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.

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.

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

?>

 

 

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

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.

 

 

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

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

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.