Reputation: 2112
I need to verify if user have entered desired characters in the input field and if he didn't, I need to format input like this:
Start variable: +42 88 66 44 3, +42 (4) 87/654321; +42 3 123456
Desired variable: +428866443, +42487654321, +423123456
So basically I need to clear out all characters from variable that are not numbers and allow only signs like +
,
;
-
.
Because I'm using an AJAX request, I would appreciate code for both PHP and Javascript.
Upvotes: 0
Views: 581
Reputation: 43552
For striping invalid chars from your string use regular expression in PHP and JAVASCRIPT.
PHP
$s = '+42 88 66 44 3, +42 (4) 87/654321; +42 3 123456';
$n = preg_replace('~[^\d\+,;-]~', '', $s);
print_r($n);
JAVASCRIPT
var s = '+42 88 66 44 3, +42 (4) 87/654321; +42 3 123456';
var n = s.replace(/[^\d\+,;-]/g, "");
console.log(n);
But better practice is that you show error, if user enters wrong format. You will need some validating to todo...
Upvotes: 3