javascript 验证阻止某些词
javascript validation prevent certain words
我试图阻止用户在 "and"、"or"、“/”、“\”等文本字段中输入某些单词和特殊字符。如何将变量设置为两个或多个条件,或者我应该如何更好地处理这个问题。
首先,我试图阻止用户输入 "and" 或文本字段。
function watchForWords(text) {
if (!text) {
return true;
}
var isValid = (text.value != "and" || text.value != "or");
if (!isValid) {
text.style.backgroundColor = "#ff8";
}
return isValid;
}
您有几个选项,具体取决于您要如何定义 "word." 如果您指的是一串文本,您可以使用一些简单的东西,例如 indexOf
检查一个字符串是否包含另一个.如果你的字面意思是单词,在 space 分隔的意义上,你可能需要一个正则表达式。
简单:
var blacklist = ["and", "or", "/", "\"];
function validate(input) {
for (var i = 0; i < blacklist.length; ++i) {
if (input.indexOf(blacklist[i]) >= -1) {
// String is present
return false;
}
}
// No blacklisted strings are present
return true;
}
console.log("this is a clean string", validate("this is a clean string")); // true
console.log("and this is a dirty string", validate("and this is a dirty string")); // false
console.log("andthis is also dirty", validate("andthis is also dirty")); // false
正则表达式:
var blacklist = ["and", "or", "/", "\"];
function validate(input) {
for (var i = 0; i < blacklist.length; ++i) {
var expr = new RegExp("\b" + blacklist[i] + "\b");
if (expr.exec(input)) {
// String is present
return false;
}
}
// No blacklisted strings are present
return true;
}
console.log("this is a clean string", validate("this is a clean string")); // true
console.log("and this is a dirty string", validate("and this is a dirty string")); // false
console.log("andthis is also dirty", validate("andthis is also dirty")); // true, note the difference from the previous because of no word sep
var isValid = !/(\w+)?and|or|\/|\\w+/.exec(myString)
如果 isValid 不包含 "and" 或 "or" 或您声明的任何其他字符,则为 true;如果至少包含其中一个,则为 false
我试图阻止用户在 "and"、"or"、“/”、“\”等文本字段中输入某些单词和特殊字符。如何将变量设置为两个或多个条件,或者我应该如何更好地处理这个问题。
首先,我试图阻止用户输入 "and" 或文本字段。
function watchForWords(text) {
if (!text) {
return true;
}
var isValid = (text.value != "and" || text.value != "or");
if (!isValid) {
text.style.backgroundColor = "#ff8";
}
return isValid;
}
您有几个选项,具体取决于您要如何定义 "word." 如果您指的是一串文本,您可以使用一些简单的东西,例如 indexOf
检查一个字符串是否包含另一个.如果你的字面意思是单词,在 space 分隔的意义上,你可能需要一个正则表达式。
简单:
var blacklist = ["and", "or", "/", "\"];
function validate(input) {
for (var i = 0; i < blacklist.length; ++i) {
if (input.indexOf(blacklist[i]) >= -1) {
// String is present
return false;
}
}
// No blacklisted strings are present
return true;
}
console.log("this is a clean string", validate("this is a clean string")); // true
console.log("and this is a dirty string", validate("and this is a dirty string")); // false
console.log("andthis is also dirty", validate("andthis is also dirty")); // false
正则表达式:
var blacklist = ["and", "or", "/", "\"];
function validate(input) {
for (var i = 0; i < blacklist.length; ++i) {
var expr = new RegExp("\b" + blacklist[i] + "\b");
if (expr.exec(input)) {
// String is present
return false;
}
}
// No blacklisted strings are present
return true;
}
console.log("this is a clean string", validate("this is a clean string")); // true
console.log("and this is a dirty string", validate("and this is a dirty string")); // false
console.log("andthis is also dirty", validate("andthis is also dirty")); // true, note the difference from the previous because of no word sep
var isValid = !/(\w+)?and|or|\/|\\w+/.exec(myString)
如果 isValid 不包含 "and" 或 "or" 或您声明的任何其他字符,则为 true;如果至少包含其中一个,则为 false