警告 Django 表单中的非 ASCII 字符
Warn on non-ASCII Characters in Django Form
我正在尝试为我的 Django 表单添加一些客户端 Ajax 验证。我想警告用户,当他们在字段中输入时,如果他们刚刚输入的任何字符不是 ascii。
我最初在表单的 clean
方法中放置了基本的 python 来检查 ascii 字符。不过,我不想这样做,因为我不想产生错误,而只是给出一条警告消息,让用户继续填写表单的其余部分。
try:
field_value.decode('ascii')
except UnicodeEncodeError:
#raise forms.ValidationError("Non-ASCII characters may cause issues in registering your data. Consider removing these characters. You may still submit at your own risk.")
# Just want to show a warning so the user can still submit the form if they wish
我想在用户输入时在字段下方显示一个小警告。我试过使用 django-dajax,但不确定。我该怎么做?
编辑:
澄清一下,我想在用户提交表单之前显示警告。因此,当他们填写表格时...
使用 JavaScript 验证该表单域。
示例(使用 jQuery):
<form>
<input name="whatever" id="fieldId">
...
</form>
<script type="text/javascript">
/* Define a function to check the form field value */
function containsAllAscii(str) {
return /^[[=10=]0-7]*$/.test(str); // returns true if all are ASCII characters. false otherwise.
}
/* Now a little jQuery */
$('#fieldId').on('change', function() {
str = $(this).val();
is_valid = containsAllAscii(str);
if (!is_valid) {
window.alert("There are Non-ASCII characters in the input field");
}
});
</script>
上面的代码将在给定字段的值发生变化时检查它(即失去焦点)。您可以使用 .on('keyup', ...
而不是 .on('change', ...
,这将在用户输入时检查字段的值。
最后,显示的错误消息只是浏览器警告。这很糟糕。你可以显示一个漂亮的错误信息,你只需要多学一点jQuery。但我希望我给了你一个好的起点。
学分:
containsAllAscii
函数的代码取自this answer by Juan Mendes。
在您的 django 表单中,创建自定义字段并利用媒体 class。
from django import forms
class MyWidget(forms.TextInput):
class Media:
css = {
'all': ('pretty.css',)
}
js = ('animations.js', 'actions.js')
并将 js 设置为您将用于验证的 javascript 文件。与另一个答案类似的东西
使用
> <input type="text" pattern="^[\x20-\x7F]+$" id ....>
在html
我正在尝试为我的 Django 表单添加一些客户端 Ajax 验证。我想警告用户,当他们在字段中输入时,如果他们刚刚输入的任何字符不是 ascii。
我最初在表单的 clean
方法中放置了基本的 python 来检查 ascii 字符。不过,我不想这样做,因为我不想产生错误,而只是给出一条警告消息,让用户继续填写表单的其余部分。
try:
field_value.decode('ascii')
except UnicodeEncodeError:
#raise forms.ValidationError("Non-ASCII characters may cause issues in registering your data. Consider removing these characters. You may still submit at your own risk.")
# Just want to show a warning so the user can still submit the form if they wish
我想在用户输入时在字段下方显示一个小警告。我试过使用 django-dajax,但不确定。我该怎么做?
编辑: 澄清一下,我想在用户提交表单之前显示警告。因此,当他们填写表格时...
使用 JavaScript 验证该表单域。
示例(使用 jQuery):
<form>
<input name="whatever" id="fieldId">
...
</form>
<script type="text/javascript">
/* Define a function to check the form field value */
function containsAllAscii(str) {
return /^[[=10=]0-7]*$/.test(str); // returns true if all are ASCII characters. false otherwise.
}
/* Now a little jQuery */
$('#fieldId').on('change', function() {
str = $(this).val();
is_valid = containsAllAscii(str);
if (!is_valid) {
window.alert("There are Non-ASCII characters in the input field");
}
});
</script>
上面的代码将在给定字段的值发生变化时检查它(即失去焦点)。您可以使用 .on('keyup', ...
而不是 .on('change', ...
,这将在用户输入时检查字段的值。
最后,显示的错误消息只是浏览器警告。这很糟糕。你可以显示一个漂亮的错误信息,你只需要多学一点jQuery。但我希望我给了你一个好的起点。
学分:
containsAllAscii
函数的代码取自this answer by Juan Mendes。
在您的 django 表单中,创建自定义字段并利用媒体 class。
from django import forms
class MyWidget(forms.TextInput):
class Media:
css = {
'all': ('pretty.css',)
}
js = ('animations.js', 'actions.js')
并将 js 设置为您将用于验证的 javascript 文件。与另一个答案类似的东西
使用
> <input type="text" pattern="^[\x20-\x7F]+$" id ....>
在html