troubleshooting Question

Problem with regular expressions.

Avatar of genesisvh
genesisvh asked on
PHP
6 Comments1 Solution211 ViewsLast Modified:
So this is the the third time posting this question, but this time I'm uploading the entire script.  I'm still having problems. This is what I need:
I want a function to echo a message if a username does not pass my requirments.

 - Only letters and / or numbers (No other characters)
 - Must start with a letter (not a number)
 - No spaces

The current code:
//validate username with limited characters
	}else if (!(eregi('/[a-Z][a-Z,0-9]/i,',($_POST["username"])))){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Username must only contain numbers and letters. No spaces.<p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";

Open in new window

is not working for me. I keep getting the echoed message with any thing I input into the field. Please help.
<?php

	$servername	= "localhost";
	$username	= "";
	$password	= "";

	//validate fields
	if(!$_POST["username"] || !$_POST["email"] || !$_POST["password1"] || !$_POST["password2"]){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please fill the required fields, <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
	
	//validate password
	}else if($_POST["password1"] != $_POST["password2"]){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please go back, PASSWORDS do not match! <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
	
	//validate password length
	}else if(strlen($_POST["password1"]) < 6){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please go back, PASSWORD is too short! <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
		
	//validate username length
	}else if(strlen($_POST["username"]) < 5){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please go back, USERNAME is too short! MUST BE BETWEEN 5 to 10 characters.<p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
		
	//validate username with limited characters
	}else if (!(eregi('/[a-Z][a-Z,0-9]/i,',($_POST["username"])))){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Username must only contain numbers and letters. No spaces.<p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";	
		
	//validate email	
	}else if(!(eregi("^.+@.+\\..+$", $_POST['email']))){	
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please enter a valid EMAIL! <a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
	
	//validate captcha
	}else if(($_SESSION['security_code'] != $_POST['security_code']) || (empty($_SESSION['security_code'])) ){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />CAPTCHA CODE does not match! <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
	
	}else{
		$conn=  mysql_connect($servername,$username,$password)or die(mysql_error());
		mysql_select_db("",$conn);
		
		// check if already registered
		$sql_check = "SELECT * FROM users WHERE email = '".$_POST[email]."'";
		$result_check = mysql_query($sql_check,$conn) or die(mysql_error());		
		$row = mysql_fetch_assoc($result_check);
		if($row['id']){
			echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />EMAIL already registered! <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
		}else{
			// check if already registered
		$sql_check = "SELECT * FROM users WHERE username = '".$_POST[username]."'";
		$result_check = mysql_query($sql_check,$conn) or die(mysql_error());		
		$row = mysql_fetch_assoc($result_check);
		if($row['id']){
			echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />USERNAME already registered! <p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";
		}else{
		
			// insert in db
			$sql = "INSERT INTO users (username, email, password, company, phone, services) VALUES ('".mysql_real_escape_string($_POST[username])."', '".mysql_real_escape_string($_POST[email])."', '".mysql_real_escape_string($_POST[password1])."', '".mysql_real_escape_string($_POST[company])."', '".mysql_real_escape_string($_POST[phone])."', '".mysql_real_escape_string($_POST[services])."')";
			$result = mysql_query($sql,$conn) or die(mysql_error());
			$subject = "You have been registered with";
			$headers4=""; ///// Change this address within quotes to your address ///
            $headers.="Reply-to: $headers4\n";
            $headers .= "From: $headers4\n"; 
            $headers .= "Errors-to: $headers4\n";
			$headers .= "Content-type: text/html\r\n"; 
			$message = '<style type="text/css">
body {
	background-color: #999;
}
</style>
			<p><img src="" width="400" height="167"></p>
<p>THANK YOU!!!</p>
<p> <span style="color: #000">You have successfully registered </span><span style="font-size: 18px"><span style="color: #00F; font-weight: bold;"US</span><span style="color: #FFF">!</span></span></p>

<p><span style="color: #000"> If you need to contact us you can always reach us through the</span> <span style="color: #00F; font-weight: bold;">CONTACT US</span> <span style="color: #000">page. </span></p>';
			
			mail( $_POST['email'], $subject, $message, $headers );

			echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br /><h1>You Have Successfully Registered</h1>";
			echo "<p>&nbsp;</p><p>&nbsp;</p><br /><a href='main_login.php'>go to login page</a>";
		}
	}
}

?>

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros