Jump to content

[SOLVED] search for characters in a string


phpdragon

Recommended Posts

I am just trying to check the $username input of the form in the code below to make sure it only has the characters a-z, A-Z, 0-9, _, - and nothing more, if it contains a character outside those parameters I just want to spit out and error message with the variable $badchars containing the illegal chars. What I have below is not quite what I have asked for, so I would like some help modifying it to work that way. I

 

<?php
if (isset($_POST['submit'])) {
$username=$_POST['username'];
if (preg_match('#[^a-z0-9]#i', $username > 0)) {
$error="<font color='red'>$username contains unacceptable ( $badchars ) characters</font>"; } else {
$error="<font color='green'>$username checks out fine</font>"; }
}
if (isset($error)) { echo $error; }
?>
<br/>
<form name="test" action="nasty.php" target="_self" method="post">
<input type="text" size="20" name="username" value="<?php if (isset($username)) { echo $username; } ?>" />
<input type="submit" name="submit" value="check" />
</form>

if(preg_match("#[^\w\d_-]#i", $username)){

echo "Bad ($char)";
}else{

echo "Good";
}

That bit works great thank you.

 

In the "Bad" Section how would I echo the first bad character or even all bad characters that were input from $username as $char for example?

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.