Problem with regular expressions.

genesisvh
genesisvh used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Don't use eregi as it is now a deprecated function - see http://www.php.net/eregi use preg_match instead.

Moving onward and looking at your code, you had no quantifiers in the expression. try the following UNTESTED code

//validate username with limited characters
	}else if (! preg_match('#[a-Z]{1}[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


Notice the {1} and + I have added. I have also removed a spurious comma
Just noticed another comma in the regex so try this instead

//validate username with limited characters
	}else if (! preg_match('#[a-Z]{1}[a-Z0-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

Arrgggghhhhh!!!!! Not my day today. Don't mix cases like a-Z use A-Z or a-z. The 'i' after the closing # will make the expression case insensitive.

Sorry for so many corrections......  :-(


//validate username with limited characters
	}else if (! preg_match('#[A-Z]{1}[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

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Expert of the Year 2008
Top Expert 2008
Commented:
try:

...
}else if (!(preg_match('/^[a-z][a-z0-9]*$/i',($_POST["username"])))){
...

Open in new window

Author

Commented:
You hit it on the money!
Expert of the Year 2008
Top Expert 2008

Commented:
NOTE: On my regex, if the word must contain MORE than ONE character, change the * to +:

}else if (!(preg_match('/^[a-z][a-z0-9]+$/i',($_POST["username"])))){

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial