Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg & preg_match help 1

Status
Not open for further replies.

AcidReignX

Programmer
Feb 28, 2005
32
US
Php.net is being entirely unhelpful in it's explanations of ereg & preg_match, but it appears that they are what I would like to have.

I am trying to create a registry form where when the user types in a username, he/she cannot enter special characters (only alpha-numeral characters allowed).

I've spent over an hour ripping my hair out testing it and reading through the php.net explanation, but everything I come up with doesn't work. Can somebody help me? This is what I tried:

if(!preg_match('([a-zA-Z0-9])', $nt_user))
{
$nt_regyes = false;
$nt_error .= "- Username may not contain special (non-alpha-numeric) characters.<br>";
}

// $nt_user is the value of the username after submitting the form.
 
Hi,

just try:
Code:
if (preg_match('/[^\w]/',$nt_user))
{
    $nt_regyes = false;
    $nt_error .= "- Username may not contain special (non-alpha-numeric) characters.<br>";

};

___
____
 
I have yet to figure out how and/or why that works (I hope to, alas that particular context confuses me), but it works perfectly!

Thanks a bunch!
 
Another question.. how would I do that to confirm an email address? For example, by checking for the '@' and '.'

I'm just taking a stab in the dark, but would it be something like this?

Code:
if(preg_match('/[^\w][^@][^\.]/', $email))
{
* CODE *
}
 
I'm ussing something like:

Code:
function is_valid_email($email) {
      if(preg_match("/^[\w\.\-]+@\w+[\w\.\-]*?\.\w{2,4}$/", $email))
         return true;
      return false;
}

___
____
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top