Jump to content

How would I do this in a form...


phpnoobie9

Recommended Posts

If a user enters in certain characters like: *$% I want to do an if with an error statement. IE:

if (user enters these characters *$%){
don't insert data into database and give error message
}
else {
if none of the above characters are in the form. insert data.
}

Link to comment
Share on other sites

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.';
}

Link to comment
Share on other sites

[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';
}

Link to comment
Share on other sites

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.

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.