重新格式化并返回textarea输入

时间:2016-02-28 21:17:47

标签: javascript asp.net vb.net

有人可以帮助我使用一个工作的html / jquery脚本来读取html textarea框的文本输入,并测试框中的文本输入是否包含11个字符的数字字符串作为其内容的一部分,如果它,脚本应该会出现一个对话框,询问客户端是否要重新格式化textarea框的数字内容。如果客户端选择了对话框的yes选项,则脚本应该通过在第3个,第6个和第9个字符之后添加空格来重新格式化数字字符串,例如将08293434565更改为082 934 345 65,然后将重新格式化的数据返回到html 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您对数字和11个数字的测试是不正确的。

&#13;
&#13;
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);

};
&#13;
<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>
&#13;
&#13;
&#13;