We help IT Professionals succeed at work.
Get Started

Problem with regular expressions.

genesisvh
genesisvh asked
on
209 Views
Last Modified: 2012-05-11
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

Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE