2012-08-02 27 views
-5

可能重複之前:
Please help me VALIDATE Mobile no in php form在PHP中的手機號碼添加前綴,將它發送到數據庫

我的手機號碼來驗證,但我被困在前綴部分。

if(empty($_POST['mobileno'])) // phone number is not empty 
{ 
$error[] = 'Please enter mobile number'; 
} else { 
if(preg_match('/^\d{10}$/',$_POST['mobileno'])) // phone number is valid 
{ 
    $mobile = $_POST['mobileno']; 
    // your other code here 
} 
else // phone number is not valid 
{ 
    $error[] = 'Phone number invalid !'; 
} 
} 
$prefix = 0; 
$pmobile = $prefix . $mobile; 

這就是我想要做的前綴。這是正確的,因爲我不能在數字前加0。

+4

而你遇到的問題是......? – DaveRandom 2012-08-02 22:51:53

+0

這段代碼會發生什麼?有沒有錯誤? – Alfabravo 2012-08-02 22:52:22

+2

手機號碼在哪個國家? – 2012-08-02 22:53:12

回答

0

您是否嘗試過使用正則表達式?

//check the first section is singular or multi, check area code between 0 and 9, check any additional integers 
$pattern = "[0|00]+[0-9]+[^\\s]"; 
$phone_number = "000263712702879"; //international phone number 
if(preg_match($pattern, $phone_number)) { 
     echo "this is a number"; 
} else echo "this not a number"; 
+0

我得到的代碼工作檢查10位數字,但如何添加到數據庫之前添加0的前綴。請在$ mobile上查詢我修改了代碼 – user1567321 2012-08-02 23:10:58

+0

@ user1567321在php中使用'。='運算符,例如'$ string = 12345;'和'$ prefix = 0;'。使用'$ new_string = $ prefix更新字符串。 $ string;'或'$ prefix。= $ string;'(使用附加或更新的字符串添加到數據庫中)。問候 – Killrawr 2012-08-02 23:14:49

+0

好吧放慢我在這裏困惑。我在哪裏插入代碼。像檢查數字後,我會這樣做。 – user1567321 2012-08-02 23:16:25

相關問題