Jump to content

Recommended Posts

advance thank you.

 

how the hell it letting the @ in, when only a-z 0-9 are allowed only.

 

madness

 

<?php

$message="my name is @ redarrow";

$err="Sorry you have not filled in the form correctly!";

if(!preg_match("/[a-z0-9]/i",$message)){

    echo $err;
   
}elseif(empty($message)){

echo $err;

}else{

echo $message;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/152993-regular-expression-problem/
Share on other sites

It's only matching one character.  /[a-z0-9]/i means "match anything that contains a letter or number".  What you want is /^[a-z0-9]*$/i or /^[a-z0-9]+$/i which will match either zero or more (if you use the *) or one or more (with the +) letters and numbers in between the beginning (^) and end ($) of the string (so nothing but letters and numbers will be accepted).

It's only matching one character.  /[a-z0-9]/i means "match anything that contains a letter or number".  What you want is /^[a-z0-9]*$/i or /^[a-z0-9]+$/i which will match either zero or more (if you use the *) or one or more (with the +) letters and numbers in between the beginning (^) and end ($) of the string (so nothing but letters and numbers will be accepted).

 

There is a missing element to the character list however.. Since the goal is the check a message, you need to allow spaces (otherwiseallyourmessagewordswillneedtobebunchedtogether). So, assuming there is no punctuation (which in itself is weird), and the message can contain zero or more characters (which I assume is also the case if there is a empty($message) check), simply add the space to the zero or more character list:

 

/^[ a-z0-9]*$/i

 

If we want to include various whitespace characters, we can use \s instead of a literal space.

 

But all in all, I question the entire message system and the pattern that implies, simply for the sake that messages in real life contain more than [ a-z0-9]. As mentioned previously, this means no punctuation (so kiss periods, question marks, commas and the like goodbye).

advance thank you.

 

how the hell it letting the @ in, when only a-z 0-9 are allowed only.

 

madness

 

<?php

$message="my name is @ redarrow";

$err="Sorry you have not filled in the form correctly!";

if(!preg_match("/[a-z0-9]/i",$message)){

    echo $err;
   
}elseif(empty($message)){

echo $err;

}else{

echo $message;
}

?>

 

$message="my name is @ redarrow";
if ( ctype_alnum ($message) ){
print "$message is alphanumeric\n";
}else{
print "'$message' is not alphanumeric\n";
}

$message="my name is @ redarrow";
if ( ctype_alnum ($message) ){
print "$message is alphanumeric\n";
}else{
print "'$message' is not alphanumeric\n";
}

 

ctype_alnum doesn't evaluate to true when there are spaces involved, so this would mean messages cannot have spaces.

Yeah, I know he mentions only a-z0-9, and if that's the case, then for sure, ctype_alnum would be the preferred method. I just found it strange that for a message, only those characters are permissible.

 

Same here.  If you use ctype_alnum, it would be more difficult to modify than using a regex from the start.  But, yes, in this case ctype_alnum, would be ideal.

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.