Jump to content

How would I do this in a form...


phpnoobie9

Recommended Posts

Allowed: A-Za-z0-9.!?,"

 

For some reason when I do this:

<javascript>

 

It allows the < and >... but if I just enter one < or > it doesn't allow it.

 

if (!empty($title) && !empty($description)) {
	if (ereg('[A-Za-z0-9.!?,"]',$description)) {
		if (@mysql_query (htmlspecialchars($query))) {
		echo 'Yayayaya';
		}
		else {
		echo 'An error has occured please try again.';
		}
	}
	else {
	echo 'Some of the characters are not allowed.';
	}
}
else {
echo 'You have empty fields.';
}

[A-Za-z0-9.!?,"]

^ That only matches a single character.  Append a + to match one or more characters.

 

[A-Za-z0-9.!?,"]+

^ Matches one or more.  Prefix a caret and append a dollar sign to specify the beginning and end of the string.

 

^[A-Za-z0-9.!?,"]+$

^ Should be closer to what you want.

 

I normally use preg_match() and I'm not sure if it behaves any differently than ereg.  With preg_match() it'd be closer to:

$regexp = '/^[A-Za-z0-9.!?,"]+$/';
if(!preg_match($regexp, $stringToTest)){
  echo 'error';
}

AFAIK you have to begin and end the regexp with matching chars, in this case I use forward slashes.  I believe the characters you use are arbitrary, for example I think this is just as valid (though I've never tried it):

$regexp = '@^[A-Za-z0-9.!?,"]+$@';

 

I believe whichever char you use needs to be escaped within the regexp though.

 

For example, if I want to match two forward slashes, I can do this:

/\/\//

or I can do this:

@//@

Notice how in the second example I didn't have to escape the forward slashes with a backslash.

 

I'm going from memory here so I could be mistaken.  Someone else might be able to give a better or more concrete answer.

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.