• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

PHP regular expression - Valid characters

Hi.  I am having trouble with what should be a simple regex validation..  I want to allow only the following characters in a telephone number input:

0-9 +-()

Attached is my code, can anybody tell me please what I am doing wrong?

Thanks, TheFoot
public function validateTelephoneNumber($v_tel){
	return preg_match("/^[0-9 +-()]/", $v_tel);
}

Open in new window

0
Barry Jones
Asked:
Barry Jones
  • 3
  • 2
1 Solution
 
saimazzCommented:
public function validateTelephoneNumber($v_tel){
	return preg_match("/[^0-9 +-()]/", $v_tel);
}

Open in new window

0
 
Barry JonesAuthor Commented:
Thanks for your reply.  I get the following error:

Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 8 in S:\Work\GGMR\branches\secretsanta\www\php\class.vf.utils.php on line 93
0
 
clampsCommented:
you have to escape some characters here...
function validateTelephoneNumber($v_tel){
  return preg_match("/^[0-9\+\-\(\) ]/", $v_tel);
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Barry JonesAuthor Commented:
Thanks clamps, tried this, but it allows the following:

'+44 (0)d 1793 756-456'

The 'd' should cause validation to fail.
0
 
saimazzCommented:
try this

function validateTelephoneNumber($v_tel){
	return preg_match("/^[0-9 \-\+()]+$/D", $v_tel);
        
}

Open in new window

0
 
Barry JonesAuthor Commented:
Bingo!  That worked.  Thanks...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now