tags:

views:

82

answers:

2

The value is AbcDefg_123.

Here is the regex:

function checkAlphNum($alphanumeric) {
   $return = false;
   if((preg_match('/^[\w. \/:_-]+$/', $alphanumeric))) {
      $return = true;
   } 
   return $return;
}

Should allow a-zA-Z0-9.:_-/ and space in any order or format and does need all but at least one character.

EDIT: Sorry again, looks like var_dump() is my new best friend. I'm working with XML and it's passing the tag as well as the value.

@SilentGhost thnx for the tips.

A: 
function checkAlphNum($alphanumeric) {
   $return = false;
   if((preg_match('/^[A-Za-z0-9\w. \/:_-]+$/', $alphanumeric))) {
      $return = true;
   }
   return $return;
}

print checkAlphNum("AbcDefg_123");

Returns true.

Andrew Sledge
and no different from what OP already has
SilentGhost
except for semi-colon, of course.
SilentGhost
A: 

It works for me too.

<?php

class RegexValidator
{
    public function IsAlphaNumeric($alphanumeric)
    { 
     return preg_match('/^[\w. \/:_-]+$/', $alphanumeric);
    }
}
?>

and this is how I am testing it.

<?php
require_once('Classes/Utility.php');

$regexVal = new RegexValidator();


$list = array("abcd", "009aaa", "%%%%", "0000(", "aaaa7775aaa", "$$$$0099aaa", "kkdkdk", "aaaaa", "..0000", " ");

foreach($list as  $value)
{

    if($regexVal->IsAlphaNumeric($value))
    {
     echo  $value . " ------>passed";
     echo "<BR>";
    }
    else
    {
     echo $value . "------>failed";
     echo "<br>";
    }

}
?>
Shiva