Jump to content

Recommended Posts

Hi,

 

I have a few expressions that aren't working how I would like them to. Currently I have:

               

$validate1 = eregi('[^a-z\s\-]', $FirstName);
	$validate1 = (int)$validate1;
	$validate2 = eregi('[^a-z\s\-]', $Surname);
	$validate2 = (int)$validate2;
	$validate3 = eregi('[^0-9\s+]', $HomePhone);
	$validate3 = (int)$validate3;
	$validate4 = eregi('[^0-9\s+]', $MobilePhone);
	$validate4 = (int)$validate4;
	$validate5 = eregi('^[^0-9][a-z0-9_]+([.][a-z0-9_]+)*[@][a-z0-9_]+([.][a-z0-9_]+)*[.][a-z]{2,4}', $EmailAddress);
	$validate5 = (int)$validate5;
	$validate6 = eregi('[^0-9]', $Postcode);
	$validate6 = (int)$validate6;
	$validate7 = eregi('[^a-z\s\-]', $Suburb);
	$validate7 = (int)$validate7;
	$validate8 = eregi('[^0-9a-z\s\-]', $Address);
	$validate8 = (int)$validate8; 

 

which are later called by an if statement and then an echo is done for each failure.

 

if ($validate1 == 0 && $validate2 == 0 && $validate3 == 0 && $validate4 == 0 && $validate5 == 0 && $validate6 == 0 && $validate7 == 0 && $validate8 == 0) {

} //send email etc.

}else{
		//Explanation of Fail
		echo "Sorry, you seem to have entered invalid information into the form.  Below is an explanation of the error/s:<ul>";
		   if ($validate1 == 1) {
		echo "<li>The first name you have entered contains invalid characters.  It can only contain letters and - (hyphen/dash).</li>";

...
...
...

}

 

I've checked that all my variables relate to the correct fields.

 

Firstly, none of these seem to be working if I enter a space. I've tried using \s but it's not working.

 

My email address validates any ol thing. I would ultimately like something that will allow addresses with .com.au, .co.uk etc.

 

With address and suburb I would also like to add an apostrophe ('). I've tried '  and \' but neither seem to be working.

 

I've been looking up stuff for days but aren't having any luck. Would be very very thankful for any help.

 

Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/157195-solved-noob-needs-some-regex-help/
Share on other sites

I can see some issues for sure. I wouldn't use ereg and instead learn PCRE (Perl Compatible Regular Expressions) by using preg instead, as ereg (which is part of POSIX [Portable Operating System Interface] ) won't be included in the core as of PHP 6. So it's best to future proof your code while you can.

 

I am noticing that you are typecasting stuff like $validate1..($validate1 = (int)$validate1;) but you don't need to as the results of regex are returned as true or false.

As a short example:

$firstName = 'Steve ';
$firstName = trim($firstName); // get rid of any initial or trailing spaces
$validation1 = preg_match('#^[a-z]+$#i', $firstName); // instead of + (one or more times), you can instead use a minimum interval quanitfier such as {3,} by example (be careful though - too short is not good).
echo ($validation1)? $firstName . ' is valid!' : $firstName . ' is NOT valid!' ;

 

As for email format validation, this is a tough one.. You can read this post (Reply #3) to see links to an email parser already built for this very purpose. As for me personally, I don't even bother using strict email format validations and instead vouch for a much more relaxed filter_var in conjunction with FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL pre defined constants.

 

Example:

function validate_email($the_email){ 
    return(filter_var(filter_var($the_email, FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL))? true : false ; 
} 

 

Granted, this isn't bulletproof (but then again, it isn't meant to be). It's just a much less tightfisted version (more relaxed). But if you need ultra strict email validation, then by all means, the link I provided above would do nicely.

 

If you're interested in learning PCRE, below are some resources to help get you started in learning preg:

 

Regular Expressions

WeblogToolsCollection

Mastering Regular Expressions

PhpFreaks Resources

PHPFreaks Tutorial

Thanks for your reply.

 

I used the integer firstly because it was in a tut I was looking at and secondly because I find it an easy way for me to visualise the true false. I appreciate that it's not required but it's not going to mess up my code, is it?

 

I had a quick look at the preg/pcre info. I think I'd get it better if I had time to sit down and go through it properly but unfortunately time is of essence... and I'm a Flash designer by trade haha. Lord, I don't know what have I got myself into here :) I think I'll try again a little later and hopefully I can get the syntax down. I appreciate knowing that the eregi is going to be outdated soon, tho it's a bummer I've been wasting my time with it.

 

With the email sanitisation that you've quoted, how would I call it so I can do different things if it's true or false? Is it:

 

if (validate_email == "true"){

//true statement

}else{

//false statement

}

 

I'm not looking for something too specific, just something that will allow more than just .com adresses.

 

Re: Ken2k7, I believe filter_input() is available for me. I know my host uses a recent enough version of PHP for it, I can't check my clients because I don't have his login details at work, but I'm pretty sure it's the same as mine.

 

 

As for email format validation, this is a tough one.. You can read this post (Reply #3) to see links to an email parser already built for this very purpose. As for me personally, I don't even bother using strict email format validations and instead vouch for a much more relaxed filter_var in conjunction with FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL pre defined constants.

 

Example:

function validate_email($the_email){ 
    return(filter_var(filter_var($the_email, FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL))? true : false ; 
} 

 

Granted, this isn't bulletproof (but then again, it isn't meant to be). It's just a much less tightfisted version (more relaxed). But if you need ultra strict email validation, then by all means, the link I provided above would do nicely.

 

If you're interested in learning PCRE, below are some resources to help get you started in learning preg:

 

Regular Expressions

WeblogToolsCollection

Mastering Regular Expressions

PhpFreaks Resources

PHPFreaks Tutorial

Thanks for your reply.

 

I used the integer firstly because it was in a tut I was looking at and secondly because I find it an easy way for me to visualise the true false. I appreciate that it's not required but it's not going to mess up my code, is it?

 

It shouldn't. After all.. preg / ereg statements return true or false based on whether the pattern was ultimately found or not. By example, if you have a look as preg_match in the php manual, you'll see this section:

 

Return Values

 

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match....

 

 

With the email sanitisation that you've quoted, how would I call it so I can do different things if it's true or false? Is it:

 

if (validate_email == "true"){

//true statement

}else{

//false statement

}

 

I'm not looking for something too specific, just something that will allow more than just .com adresses.

 

Well, you don't need it to be a function.. you could just check on your email within a typical if statement, perhaps along the lines of:

if(filter_var(filter_var($EmailAddress, FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL)){
// valid, do something...
} else {
// invalid, do something else...
}

 

But again, this version is not as tight fisted as the email parser in the other link I provided.. It just checks for a 'general' format.. so I think it lets many things pass through (some of which you may or may not want). You would have to test this by plugging in various test emails and see what and what doesn't pass through (I don't think it cares about .com or .de by example. If you absolutely need a rigid parser, then I recommend the link I previously posted for that..seems quite thorough). I only suggest that version as many people start trying to build these regex patterns to test for emails only to find out that their pattern blocks out certain legit formats.. I personally prefer the relaxed version, just to make sure I don't inadvertently block some people out.

 

 

Why do you care about your clients? The file is on your server.  o.O

 

I'm testing on my server then I have to upload to theirs so would it not be important if their PHP version differed to mine? They bought their hosting before talking to me. I'm not hosting any of their stuff on my personal domain.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.