Jump to content

regex not working


kwame123
Go to solution Solved by Jacques1,

Recommended Posts

this is my code 

$re = '/[^\W][a-zA-Z\d.-]{3,20}/';
if(!preg_match_all($re, $username)){
 do something
  }else{
 do somthing else
}


$username is a $_POST['username'];
when i submit a 3 letter word it works
but when i use symbols like $ and ! and etc it still goes through,
im trying to get a regex where the username is 4-20 characters long and only the alphabet and numbers and
dots and dash but dots and dashs cant be next to each other
 
thx :(
Link to comment
Share on other sites

  • Solution

Your regex doesn't say that at all. You have no anchors (which means a substring match is sufficient), and for some strange reason you're using preg_match_all(), as if you expected multiple matches.

 

If you only want to allow dashes and dots as delimiters between alphanumerics, that's

'/\\A[a-z\\d]+(?:[.-][a-z\\d]+)*\\z/i'

Length checks are the job of strlen() or mb_strlen().

Edited by Jacques1
Link to comment
Share on other sites

Your regex doesn't say that at all. You have no anchors (which means a substring match is sufficient), and for some strange reason you're using preg_match_all(), as if you expected multiple matches.

 

If you only want to allow dashes and dots as delimiters between alphanumerics, that's

'/\\A[a-z\\d]+(?:[.-][a-z\\d]+)*\\z/i'

Length checks are the job of strlen() or mb_strlen().

oh my goodness thank you so much. :) not its fixed!

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.