Jump to content

Problem with preg_match (Have no clue how to achieve it)


Nuv

Recommended Posts

Hi guys,

 

Ive been coding a script and really stuck with preg.Hope you guys will help me out.Below is my problem :-

 

I want people to enter their usernames in a field while registration but it should only accept the range from a-z,A-Z and

 

`1
`2
`3
`4
`5
`6
`7
`8
`9
`!
`@
`# 
`$ 
`% 
`^ 
`&
`* 
`( 
`)

 

Please Note

 

Suppose i input `%

 

then there should be no gap between ` and % . Likewise,

 

If i enter `4 , there should be no gap between ` and 4.

I dont want my users to enter numbers but if that number is with ` like `4 or `6....it should be allowed.

Also no space in between usernames should be allowed.

okay so let me get this straight:

 

1) The string should always start with `

2) After the ` the string can contain 1 or more (is there a limit?) of any letter, number or the following symbols: !@#$%^&*()

3) No spaces anywhere (which this is technically defined by its absence in #2)

 

 

okay so let me get this straight:

 

1) The string should always start with `

2) After the ` the string can contain 1 or more (is there a limit?) of any letter, number or the following symbols: !@#$%^&*()

3) No spaces anywhere (which this is technically defined by its absence in #2)

 

 

 

1,2.Not the string, numbers from 1-9 and !@#$%^&*() should start with ` . That to only one number(or symbol) not many.Like -

 

`$ <--- Right
`$% <--- Wrong (As it has more than 1 number[or symbol])

 

3.Yes, i dont want spaces anywhere in the string.

 

However, Range from a-zA-z should NOT start from `

 

Few examples to understand better..

 

craY`5on <-- Right (as a-zA-Z range has no ` in front and number 5 has `)
cr`$ayon<--Right(as a-zA-Z range has no ` in front and symbol $ has `)

cr`aYon<--Wrong(a which comes under range a-zA-Z has `)
cr`$%ayon<--Wrong(2 symbols have one `.% should have had ` too for it to be correct)
cr`5$ayon<--Wrong(1 symbol and 1 number has one `.$ should have had `too for it to be correct)

soo...revised rules:

 

1) username can have 0 or 1 of !@#$%^&*()  but it must be preceded by a `

2) username can have 0 or 1 number in it, but it must be preceded by a `

3) username can have 0 or more letters, case insensitive

 

 

soo...revised rules:

 

1) username can have 0 or 1 of !@#$%^&*()  but it must be preceded by a `

2) username can have 0 or 1 number in it, but it must be preceded by a `

3) username can have 0 or more letters, case insensitive

 

 

 

Yes you got it right.

/*
  function based on the following rules:
    1) username can have 0 or 1 of !@#$%^&*()  but it must be preceded by a `
    2) username can have 0 or 1 number in it, but it must be preceded by a `
    3) username can have 0 or more letters, case insensitive

  returns true if valid, false if not
*/
function validateUsername($username) {
  // check to see if $username only contains valid chars
  if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username))
    return false;

  // check if more than one number or symbol
  preg_match_all('~([0-9])|([!@#$%^&*()])~',$username,$matches);
  if ((count(array_filter($matches[1]))>1)||(count(array_filter($matches[2]))>1)) 
    return false;

  // check if ` precedes symbol or number
  if ($matches[1]||$matches[2])
    if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) 
      return false;

  // valide username
  return true;
} // end validateUsername

 

 

/*
  function based on the following rules:
    1) username can have 0 or 1 of !@#$%^&*()  but it must be preceded by a `
    2) username can have 0 or 1 number in it, but it must be preceded by a `
    3) username can have 0 or more letters, case insensitive

  returns true if valid, false if not
*/
function validateUsername($username) {
  // check to see if $username only contains valid chars
  if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username))
    return false;

  // check if more than one number or symbol
  preg_match_all('~([0-9])|([!@#$%^&*()])~',$username,$matches);
  if ((count(array_filter($matches[1]))>1)||(count(array_filter($matches[2]))>1)) 
    return false;

  // check if ` precedes symbol or number
  if ($matches[1]||$matches[2])
    if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) 
      return false;

  // valide username
  return true;
} // end validateUsername

 

 

 

Ah brilliant code.I was wondering though how the code would change or how the code would look like if

 

1) username can have 0 or more of !@#$%^&*()  but it must be preceded by a `

    2) username can have 0 or more number in it, but it must be preceded by a `

    3) username can have 0 or more letters, case insensitive

/*  function based on the following rules:
    1) username can have 0 or [b]more[/b] of !@#$%^&*()  but it must be preceded by a `
    2) username can have 0 or [b]more [/b]number in it, but it must be preceded by a `
    3) username can have 0 or more letters, case insensitive

  returns true if valid, notaccepted if not
*/function validateUsername($username) {

// check to see if $username only contains valid chars
  if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username))
    return notaccepted;

//checking if 0 or more of [1-9!@#$%^&*()] if present, is preceded by a ` or not 
$counter = strlen($username);
$array = str_split($username);

for ($i = 0; $i <= $counter; $i++) {
    if($array[$i] == 1 || $array[$i] == 2 || $array[$i] == 3 || $array[$i] == 4 || $array[$i] == 5 || $array[$i] == 6 || $array[$i] == 7 || $array[$i] == 8 || $array[$i] == 9 ) {
	$j = $i-1;
	if($array[$j] != "`") {
		return notaccepted;
		}
	}	
    if($array[$i] == '!' || $array[$i] == '@' || $array[$i] == '#' || $array[$i] == '%' || $array[$i] == '^' || $array[$i] == '&' || $array[$i] == '*' || $array[$i] == '(' || $array[$i] == ')' ) {
	$j = $i-1;
	if($array[$j] != "`") {
		return notaccepted;
		}
	}
}
return true;	
}	

 

Well above code works but im wondering if there are any intelligent/better ways to do it using preg.

Ah brilliant code.I was wondering though how the code would change or how the code would look like if

 

1) username can have 0 or more of !@#$%^&*()  but it must be preceded by a `

    2) username can have 0 or more number in it, but it must be preceded by a `

    3) username can have 0 or more letters, case insensitive

 

/*
  function based on the following rules:
    1) username can have 0 or more of 0-9!@#$%^&*()  but it must be preceded by a `
    2) username can have 0 or more letters, case insensitive

  returns true if valid, false if not
*/
function validateUsername($username) {
  // check to see if $username only contains valid chars
  if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username))
    return false;

  // check if ` precedes symbol or number
  if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) 
    return false;

  // valide username
  return true;
} // end validateUsername

Archived

This topic is now archived and is closed to further replies.

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