Avatar of genesisvh
genesisvh

asked on 

Problem with regular expressions.

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

PHP

Avatar of undefined
Last Comment
hielo
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of genesisvh
genesisvh

ASKER

You hit it on the money!
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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"])))){
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo