F11*_*F11 2 c# asp.net-mvc jquery razor asp.net-mvc-4
@Scripts.Render("~/bundles/jquery", "~/bundles/jqueryui", "~/bundles/WorkbenchScripts")
@Styles.Render("~/Content/WorkbenchCss")
<script type="text/javascript">
$(document).ready(function () {
checkSpecialChars('#txtCreateSet');
$('#btnCancelSet').click(function (e) {
window.close();
});
});
//this function is used to allow only specific characters in function
function checkSpecialChars(textboxID) {
debugger;
return textboxID.each
(
function () {
var allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
allowedChars += "abcdefghijklmnopqrstuvwxyz";
allowedChars += "0123456789";
allowedChars += " "; //Space
//Check if try to specifying allowed special characters for Name fields
allowedChars += "-_";
//on keypress
$(textboxID).keypress(function (e) {
var keyUniCode = !e.charCode ? e.which : e.charCode;
var charValue = String.fromCharCode(keyUniCode);
if (keyUniCode != 13) {
if (allowedChars.indexOf(charValue) == -1)
e.preventDefault();
}
});
//On paste
$(textboxID).bind('paste', function (e) {
setTimeout(function () {
var newText = textboxID.val();
$.each(newText, function (i) {
if (allowedChars.indexOf(newText.charAt(i)) == -1)
{ textboxID.val(textboxID.val().replace(newText.charAt(i), '')); }
});
}, 1);
});
}
);
};
</script>
<table>
<tr>
<td>
@Html.Label("Create Set")
</td>
<td>
@Html.TextBoxFor(model => model.SetName, new { id = "txtCreateSet" })
</td>
</tr>
<table>
Run Code Online (Sandbox Code Playgroud)
在上面的代码我得到错误"对象不支持此属性",我使用MVC4,在这里我已经在mvc视图中编写代码,在文本框中只允许的字符是0-9 az AZ - _和空格字符我哪里做错了?谁能帮助我找到理想的解决方案?
那是因为你将函数传递给选择器字符串而不是对象.
看这里:
checkSpecialChars('#txtCreateSet');
Run Code Online (Sandbox Code Playgroud)
这意味着当您执行以下操作时,您将收到该错误,因为each字符串上没有方法:
return textboxID.each
Run Code Online (Sandbox Code Playgroud)
试试这个,传递jQuery对象:
checkSpecialChars($('#txtCreateSet'));
Run Code Online (Sandbox Code Playgroud)