2016-08-11 64 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不发送数据与&或# –

相关问题