/    Sign up×
Community /Pin to ProfileBookmark

check a value is either numeric or a + or space

Hi,

I am trying to use the code below to only allow through numeric or a + sign or whitespace.

So it would let through this +0989 and 9879

but would not let through +989jkhj or kjhkj9089

I have this, now i know ctype_punt isn’t right but im not sure

[code=php]
$val = ‘9890 +’;
if (!ctype_digit(trim($val)) && !ctype_punct(trim($val))) {
echo ‘No’;
} else {
echo ‘Yes’;
}
[/code]

Thanks
k0r54

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelOct 24.2005 — You could use preg_match.
[code=php]$val = '9890 +';

if (!preg_match('#^[0-9+ ]+$#',trim($val)))
{
echo 'No';
}
else
{
echo 'Yes';
}[/code]
Copy linkTweet thisAlerts:
@k0r54authorOct 24.2005 — nice 1 ?

Thank you
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...