2016-02-28 23 views
-2

有人可以幫助我一個工作的HTML/jQuery腳本,它將讀取html textarea框的文本輸入並測試框上的文本輸入是否包含11個字符的數字字符串作爲其部分內容,如果是這樣,腳本應該會出現一個對話框,詢問客戶是否希望重新格式化textarea框中的數字內容。如果客戶端選擇了對話框的「是」選項,則腳本應該在第3個,第6個和第9個字符之後加上空格,例如更改08293434565到082 934 345 65,然後重新格式化數字字符串,然後將重新格式化的數據返回到HTML textarea的框重新格式化和返回textarea輸入

function Confirm() { 
 

 
    var data = $('#fix').val(); 
 
    var arr = data.split(' '); 
 

 
    //check if numeric and 11 numbers 
 
    if (isNaN(arr[5]) == true && arr[5].length == 11) { 
 

 
    //show popup, if yes run the format function 
 
    var confirm_value = document.createElement("INPUT"); 
 
    confirm_value.type = "hidden"; 
 
    confirm_value.name = "confirm_value"; 
 
    if (window.confirm("Message contains numeric characters which might make the message not delivered to some networks. Do you want us to reformat the message ?. This might increase the numbers of pages of the message and the cost?")) { 
 
     confirm_value.value = "Yes"; 
 
     format(); 
 
    } else { 
 
     confirm_value.value = "No"; 
 
    } 
 
    document.forms[0].appendChild(confirm_value); 
 

 

 
    } 
 
} 
 

 
function format() { 
 

 
    var first = arr[5].substring(0, 4); 
 
    var second = arr[5].substring(4, 20); 
 
    second = second.replace(/(.{3})/g, "$1 ") 
 

 
    $('#fix').val("This is my mobile number " + first + " " + second); 
 

 
};
<input type="textbox" id="fix" name="fix" /> 
 
<button ID="button1" OnClick="Confirm();" runat="server">Confirm</button>

+0

你嘗試過什麼到目前爲止? –

+0

是的,我有。我剛發佈它 – Pope

+0

你的代碼看起來很接近。我不認爲你應該只關注拆分數組中的第6個元素(arr [5]),而應該遍歷所有元素或者嘗試RegExp。你的代碼似乎沒有重新格式化字符串,也沒有把它放回到textarea。 – Nate

回答

0

你的測試數值和11個數字是不正確的。

function Confirm() { 
 

 
    var data = $('#fix').val(); 
 
    
 
    //check if numeric and 11 numbers 
 
    if (!isNaN(data) == true && data.length == 11) { 
 

 
    //show popup, if yes run the format function 
 
    if (window.confirm("Message contains numeric characters which might make the message not delivered to some networks. Do you want us to reformat the message ?. This might increase the numbers of pages of the message and the cost?")) { 
 
     format(data); 
 
    } 
 
    } else { 
 
    alert('Check number format'); 
 
    } 
 
} 
 

 
function format(data) { 
 

 
    var first = data.substring(0, 4); 
 
    var second = data.substring(4, 20); 
 
    second = second.replace(/(.{3})/g, "$1 ") 
 

 
    $('#fix').val("This is my mobile number " + first + " " + second); 
 

 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="textbox" id="fix" name="fix" /> 
 
<button ID="button1" OnClick="Confirm();" runat="server">Confirm</button>

+0

謝謝。它工作 – Pope

+0

@戴夫安德森請如何使腳本能夠找到一個文本框中的電話號碼,其中包含與數字值一側的其他單詞。就像「請隨時撥打我的手機號碼08293434565」。請注意,文本框內容的長度是一個變量,它隨網頁上的每個客戶端而變化。這意味着電話號碼在文本框中的位置不固定 – Pope