2016-08-11 66 views
-3

我有一個創建一個簡單的SMS驗證器的要求,它檢查*是否是開頭符號,#是結尾。表單有一個文本輸入字段和一個按鈕驗證。jquery沒有從輸入文本中讀取特殊字符

單擊按鈕..它打印表單中的數據和第一個和最後一個字符。除了#和&以外,所有字符都可以正常工作。每當我輸入這樣的符號... jquery排除它。不知道這是什麼解決方法。

enter image description here

<script type="text/javascript"> 
$(document).ready(function(){ 
$("#transactionForm").submit(function(event){ 
    event.preventDefault(); 
     var amountToSend = $("input#txnID").val(); 
    //alert(amountToSend); 
    var length = $('input#txnID').val().length; 

    if(length > 3){ 
     document.getElementById("loadingBlock").style.display = 'block'; 
     document.getElementById("calculationBlock").style.display = 'none'; 
     document.getElementById("errorBlock").style.display = 'none'; 

     var url = '<?php echo API_URL ?>'; 
     url = url+'/v1/smsValidator.php?txnID='+amountToSend; 

//Start of AJAX call  
    $.ajax({ 
    type: 'POST', 
    dataType: 'json', 
    url: url, 
}).done(function(response) { 
    document.getElementById("loadingBlock").style.display = 'none'; 
    var desc1 = $('#desc1'); 
    var messageTitle = $('#messageTitle'); 

    if(response.error){ 
     document.getElementById("errorBlock").style.display = 'block'; 
     $("#errorTitle").html(response.message); 
    }else{ 
    document.getElementById("calculationBlock").style.display = 'block'; 
    $("#messageTitle").html(response.message); 
    } 

}); 
//End of AJAX call 
    } 
}); 
}); 
</script> 
+0

請發佈您的代碼。 –

+1

當然,因爲你不是在'url'中轉義這個特殊字符......'url = url +'/ v1/smsValidator.php?txnID ='+ encodeURIComponent(amountToSend);'。 –

+0

如何以及因爲您正在發送POST請求,爲什麼不在身體請求中設置此數據? '.ajax({data:amountToSend,...});' –

回答

1
  1. 你得到的DOM元素
  2. 你得到它的價值
  3. ,如果第一個元素是*和最後一個元素是#

    var inputText = document.getElementById("inputText").value; 
    // in jQuery it's something like: var inputText = $("#inputText).val() 
    if (inputText[0] === "*" && inputText[inputText.length - 1] === "#") { 
        // the number is valid 
    } 
    
  4. 你檢查
+0

我驗證這個服務器端...使用PHP ...但JavaScript不發送數據與&或# –

相關問題