i want to validate the special characters like !@#$%^&*() entered in the auto complete box and possibly give an alert box message as "Please enter a valid name". Below is my snippet:
我想验证在自动完成框中输入的特殊字符!@#$%^&*(),并可能会发出一条警告框消息“请输入有效名称”。以下是我的代码:
$("#txtName").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Webservice.asmx/GetNames",
data: "{'prefixText':'" + request.term + "'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('|')[0],
val: item.split('|')[1]
}
}))
},
error: function (result) {
ServiceFailed(result);
},
failure: function (response) {
ServiceFailed(result);
}
});
},
select: function (event, ui) {
autoName(ui.item.val, ui.item.label);
},
minLength: 4
}).data("autocomplete")._renderItem = function (ul, item) {
var newText = String(item.value).replace(
new RegExp(this.term, "gi"),
"<span class='ui-state-highlight'>$&</span>");
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + newText + "</a>")
.appendTo(ul);
}
Any help would be much appreciated.
任何帮助将非常感激。
Thanks in advance
提前致谢
1 个解决方案
#1
1
See this question on blocking special characters in a textbox.
有关阻止文本框中的特殊字符的问题,请参阅此问题。
You could probably set up the validation before sending the request. Instead of setting up a whole validation plugin, which does everything on keypress
您可以在发送请求之前设置验证。而不是设置一个完整的验证插件,它在按键上执行所有操作
$.ajax({
url : url, //blah blah
beforeSend : function(){
if(!validateChars()){
return false;
}
}
});
OR
You could send the request by stripping out special characters instead of blocking. (more user friendly)
您可以通过删除特殊字符而不是阻止来发送请求。 (更加用户友好)
var alowedChars = stringToReplace.replace(/[^\w\s]/gi, '')
var alowedChars = stringToReplace.replace(/ [^ \ w \ s] / gi,'')
Pasted relevant code for reference
粘贴相关代码以供参考
#1
1
See this question on blocking special characters in a textbox.
有关阻止文本框中的特殊字符的问题,请参阅此问题。
You could probably set up the validation before sending the request. Instead of setting up a whole validation plugin, which does everything on keypress
您可以在发送请求之前设置验证。而不是设置一个完整的验证插件,它在按键上执行所有操作
$.ajax({
url : url, //blah blah
beforeSend : function(){
if(!validateChars()){
return false;
}
}
});
OR
You could send the request by stripping out special characters instead of blocking. (more user friendly)
您可以通过删除特殊字符而不是阻止来发送请求。 (更加用户友好)
var alowedChars = stringToReplace.replace(/[^\w\s]/gi, '')
var alowedChars = stringToReplace.replace(/ [^ \ w \ s] / gi,'')
Pasted relevant code for reference
粘贴相关代码以供参考