如何使用键码验证允许使用字母数字字符的空格?

时间:2018-02-20 04:11:41

标签: javascript jquery jquery-ui javascript-events javascript-objects

这是下面的代码,只允许使用字符和数字。但我想在字符和数字旁边留出空间。请帮忙。提前谢谢。

x <- data.frame("SN" = 1:4, "Age" = c(21,15,30,40), "Name" = c("Isa","Dora","Luisa","Daniela"))
x

y <- data.frame("SN" = 1:4, "Age" = c(22,17,36,41), "Name" = c("Isa","Cristian","Peter","Juan"))
y

y$Name

i

for (i in "y$Name"){
  if (i = "x$Name" ){
    print(i)
  }
 next
}

3 个答案:

答案 0 :(得分:0)

只需要将(空格)的keyCode添加到此函数

function IsAlphaNumericTour(e) {
      var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
      var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || keyCode == 32 ||(specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
      document.getElementById("error_tour").style.display = ret ? "none" : "inline";
      return ret;
 }

答案 1 :(得分:0)

空格的关键代码是32,所以你要将条件改为:

var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode) || keyCode == 32);

答案 2 :(得分:0)

如果您愿意,也可以使用正则表达式测试来执行此操作:

function IsAlphaNumericTour(e) {
  var key = e.which;
  var regex = /[a-zA-Z0-9 ]/;
  var ret = key.test(regex);
  // This method returns true if it finds a match, otherwise it returns false.
  $("#error_tour").css("display", (ret ? "none" : "inline"));
  return ret;
}