Jump to content


error checking, regex help...


3 replies to this topic

#1 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 10 May 2006 - 10:55 PM

Im horrible with regex, and my name/subject inputs keep failing sometimes and sometimes it works...

name is supposed to only allow letters/number/comma and period...

subject allows mostly all characters besides <> and a few others...

so is this all correct?

function checkContact(&$_POST, &$errors) {
// Check the contact form and return errors
	$errors = new Object;
	$msg = "";

	// Checks valid name
	if (empty($_POST["name"])) {
		$errors->name = true;
		$msg .= "<li>Enter your name!</li>"; 
		
		} elseif ($_POST["name"] == "Name") {
			$errors->name = true;
			$msg .= "<li>That is not your real name!</li>"; 
		
		} elseif (!preg_match("/^[a-zA-Z0-9,\.]+$/", $_POST["name"])) {
			$errors->name = true;
			$msg .= "<li>No tricky hacking, thanks!</li>";  }
	
	// Checks valid email
	if (empty($_POST["email"])) {
		$errors->email = true;
		$msg .= "<li>Enter your email!</li>";  
			
		} elseif ($_POST["email"] == "Email") {
			$errors->email = true;
			$msg .= "<li>That is not your real email!</li>";  
			
		} elseif (!validEmail($_POST["email"])) { 
			$errors->email = true;
			$msg .= "<li>How can I reply with an invalid email?</li>"; }
	
	// Check Subject
	if (!empty($_POST["subject"])) {
		if (!preg_match("/^[a-zA-Z0-9\!@#$%&()\-_\=\+\[\];:\'\",\.\?]+$/", $_POST["subject"])) {
			$errors->subject = true;
			$msg .= "<li>No hacking my subject!</li>";  }
	}
			
	// Check Website
	if (!empty($_POST["website"])) {
		if ($_POST["website"] == "Website") {
			$errors->website = true;
			$msg .= "<li>Either enter a website or leave it blank!</li>";
			
		} elseif (!eregi("^((http|https|ftp)://)?([[:alnum:]-])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_.~?-]*)$", stripslashes(trim($_POST["website"])))) {
			$errors->website = true;
			$msg .= "<li>Enter a valid website for me to view!</li>"; }
	}
					
	// Checks valid message
	if (empty($_POST["message"])) {
		$errors->message = true;
		$msg .= "<li>How can we talk if you dont write anything?</li>"; 
		
		} elseif ($_POST["message"] == "Message") {
			$errors->message = true;
			$msg .= "<li>Please be more descriptive!</li>";
		
		} elseif (preg_match("/^\<\>$/", $_POST["message"])) {
			$errors->message = true;
			$msg .= "<li>No html allowed!</li>"; }
			
	return $msg;
}


#2 Wybe

    Jedi In Training

  • Members
  • PipPip
  • 399 posts
  • Gender:Male
  • Location:the Netherlands
  • Interests:Graphic design, digital and traditional, street style, graffiti, guerilla drawing, typography, coding, sex

Posted 11 May 2006 - 01:41 AM

View Posttiki, on May 11 2006, 05:55 AM, said:

so is this all correct?

Haven't you tested it? If it doesn't work and gives you a PHP error, you should come and ask again

#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 May 2006 - 04:32 PM

A slightly better description of "sometimes it works" would be a little more useful.

Other then that, i would recommend no passing $_POST to that function, as $_POST is an autoglobal, so passing it just takes time.

#4 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 11 May 2006 - 09:54 PM

I dont know? Wybe I just said...

Sometimes it says error with my name or with my subject, even when it is just letters.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users