有人可以幫助我一個工作的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>
你嘗試過什麼到目前爲止? –
是的,我有。我剛發佈它 – Pope
你的代碼看起來很接近。我不認爲你應該只關注拆分數組中的第6個元素(arr [5]),而應該遍歷所有元素或者嘗試RegExp。你的代碼似乎沒有重新格式化字符串,也沒有把它放回到textarea。 – Nate