2016-06-10 40 views
1

如何在HTML文本框中輸入以及前3個字符與設置變量不匹配 - 然後顯示錯誤?我需要失去錯誤和第三個字符後輸入旁邊顯示文本輸入html格式的輸入字符需要匹配 - jQuery?

我在想jQuery,AJAX,PHP - 不確定。我只是不想使用警告框。

而這需要一個用戶進入提交按鈕之前...

<form> 
    <input type="text" id="test"/><br> 
    <input type="button" id="txt" value="Submit" /> 
</form> 
$(document).ready(function(){ 
    $("#txt").click(function(){ 
     var text = $("#test").val(); 
     var comparingText = "yes"; 

     if (text != comparingText){ 
      alert($("#test").val()); 
     } 
    }); 
}); 
+0

你問你應該使用什麼技術? –

回答

2

將會寫入後顯示此警報是。 你可以隨心所欲地使用它。

$("#test").keyup(function() { 
    var test = $("#test").val(); 
    if(test == 'yes'){ 
     alert("your Error msg"); 
    } 
}); 
+0

它有效嗎? –

1

可以使用<span>元素,旁邊的<input>元素顯示錯誤消息,並且,正如你所說的,避免使用警告框。

JS

HTML

<form> 
    <input type="text" id="test" oninput="submitData(this.value)"/> 
    <span id="textError"></span><br/> 
    <input type="button" id="txt" value="Submit" /> 
</form> 

JS

function submitData(input) {   
    if (input != "yes") { 
     document.getElementById("textError").innerHTML = "Your Error MSG"; 
    } else { 
     document.getElementById("textError").innerHTML = "";  
    } 
} 

JS + jQuery的

在這種情況下,我服用Mamunur Rashid對代碼進行補充的答案。

HTML

<form> 
    <input type="text" id="test"/> <span id="textError"></span><br/> 
    <input type="button" id="txt" value="Submit" /> 
</form> 

jQuery的

$(document).ready(function(){ 
    $("#test").keyup(function(){ 
     var test = $("#test").val(); 
     if (test != "yes") { 
      $("#textError").html("Your Error MSG"); 
     } else { 
      $("#textError").html(""); 
     } 
    }); 
});