Jump to content

eregi validation help!


robcrozier

Recommended Posts

ok, im trying to validate some form input using eregi, only i cant seem to allow the user to enter a return character without the eregi function discarding it and the error handler being raised. here's the code that im using to validate the string:

[code]

if (eregi ("^[[:alpha:]. ()_,'-]{1,}$", THE RETURNED STRING HERE)
{
// valid
}
else
{
  // error handler raised
}

[/code]

Any suggestions as to why this will not allow a the return character?

Cheers
Link to comment
https://forums.phpfreaks.com/topic/28232-eregi-validation-help/
Share on other sites

  • 3 months later...
Sorry to bring this old post up, but well, no one answered; and it is something I would like to know. I am trying to validate a phone number. Well I want that to allow both ( and ) . Among other things....here is the code. I get an error that says: [color=red]eregi(): REG_BADBR[/color]

[code]
if (eregi ('^[0-9.\(\)-]{10-15}$', stripslashes(trim($_POST['user_contact'])))) {
$ucontact = escape_data($_POST['user_contact']);
} else {
$ucontact = FALSE;
echo '<span class="error">Please enter a valid contact number.<br /></span>';
}
[/code]

I have tried quite a few things from ...0-9\.\(\)\- and other stuff like ...0-9.()-.. which i knew wouldn't work because eregi uses ( ) to section out things. So I've tried "escaping" the ( and ), like about, but nothing works...
Link to comment
https://forums.phpfreaks.com/topic/28232-eregi-validation-help/#findComment-197531
Share on other sites

Thanks, I hate it when I make a stupid mistake. Just in case someone wants to know the final eregi() code...here it is

[code]if (eregi ('^[0-9.\(\)-]{10,15}$', stripslashes(trim($_POST['user_contact'])))) {[/code]

I am going to modify the code soon to make it where it requires '(' 3 digits ')' 3 digits '.' 4 digits (111)111.1111 so it will fully validate a phone number. Not full sure how to do it, but shouldn't be hard.
Link to comment
https://forums.phpfreaks.com/topic/28232-eregi-validation-help/#findComment-197549
Share on other sites

If you require area code it's simple.. and here you need to escape the brackets.

[code=php:0]'\(?[0-9]{3}\)?[0-9]{3}[.-]?[0-9]{4}'[/code]

You might want to strip spaces first in case someone adds them in.  I made some things optional with "?", and allowed either "." or "-".  This regexp might be too strict, since people might use other characters as seperators.
Link to comment
https://forums.phpfreaks.com/topic/28232-eregi-validation-help/#findComment-197563
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.