Reputation: 57
I have a jquery ajax post and when a user wirte and press enter in a messagebox this ajax triggers. but when the user writes Nothing and press enter this ajax is triggered and return something. but how can I catch the empty strings from user?
cliKeyPressed : function(e) {
if(typeof e === "undefined" ) e = window.event;
if(e.keyCode == 13) {
var previousLine = "";
if($('#terminal').text()!== ""){
previousLine = $('#terminal').text();
previousLine = previousLine.replace("you entered: ", " ");
}
var inputData = $(e.currentTarget).val();
$('#terminal').text(previousLine + "\r\n" + inputData + "\r\n");
AjaxPost(inputData);
}
AjaxPost : function(data) {
$.ajax({
type : "POST",
url : "/api/user",
datatype : "application/json",
contentType: " text/plain",
data : dataAttribute,
success : function(data) {
},
error : function(error) {
},
Upvotes: 0
Views: 103
Reputation: 13529
You are already doing it. Just move the calling of the AjaxPost inside the if statement:
if(typeof e === "undefined" ) e = window.event;
if(e.keyCode == 13) {
var previousLine = "";
if($('#terminal').text() !== "" && $(e.currentTarget).val() !== ""){
previousLine = $('#terminal').text();
previousLine = previousLine.replace("you entered: ", " ");
var inputData = $(e.currentTarget).val();
$('#terminal').text(previousLine + "\r\n" + inputData + "\r\n");
AjaxPost(inputData);
}
}
}
Upvotes: 3