[英]Warn on non-ASCII Characters in Django Form
I'm trying to add some client-side Ajax validation of my Django form. 我正在尝试为我的Django表单添加一些客户端Ajax验证。 I want to warn users, as they are typing in a field, if any of the characters they just inputted are not ascii.
我要警告用户,因为他们正在输入字段,如果他们刚刚输入的任何字符都不是ascii。
I originally put the basic python to check for ascii characters in my form's clean
method. 我最初将基本的python用于检查表单的
clean
方法中的ascii字符。 I don't want to do that, though, as I don't want to produce an error but rather just give a warning message and let the user continue with the rest of the form. 但是,我不想这样做,因为我不想产生错误,而是发出警告消息,然后让用户继续执行表单的其余部分。
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
I want to show a small warning under the field as the user is typing. 我想在用户输入时在字段下显示一个小的警告。 I've tried using django-dajax, but am not sure.
我试过使用django-dajax,但不确定。 How can I do this?
我怎样才能做到这一点?
EDIT: To clarify, I want to show the warning before the user submits the form. 编辑:为了澄清,我想在用户提交表单之前显示警告。 So, as they are filling out the form...
因此,当他们填写表格时...
Use 采用
> <input type="text" pattern="^[\x20-\x7F]+$" id ....>
in html 在HTML
Use JavaScript to validate that form field. 使用JavaScript验证该表单字段。
Example (using jQuery): 示例(使用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 /^[\000-\177]*$/.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>
The above code will check the given field's value whenever it changes ( ie loses focus). 上面的代码将在给定字段更改时( 即失去焦点)检查其值。 You can use
.on('keyup', ...
instead of .on('change', ...
which will check the field's value as the user is typing. 您可以使用
.on('keyup', ...
而不是.on('change', ...
,它会在用户键入时检查字段的值。
Finally, the error message that is shown is just a browser alert. 最后,显示的错误消息只是浏览器警报。 Which is crappy.
太烂了。 You can display a beautiful error message, you just need to learn a little more of jQuery.
您可以显示一条漂亮的错误消息,您只需要学习更多jQuery。 But I hope I've given you a good starting point.
但我希望我给您一个很好的起点。
Credits: 学分:
The code for containsAllAscii
function is taken from this answer by Juan Mendes . JuanAlldes的 答案中
containsAllAscii
函数的代码。
In your django form, create a custom field and take advantage of the media class. 在django表单中,创建一个自定义字段并利用media类。
from django import forms
class MyWidget(forms.TextInput):
class Media:
css = {
'all': ('pretty.css',)
}
js = ('animations.js', 'actions.js')
and set the js to the javascript file you will use for validation. 并将js设置为用于验证的javascript文件。 something along the lines of the the other answer
与其他答案相似的东西
https://docs.djangoproject.com/en/1.9/topics/forms/media/ https://docs.djangoproject.com/zh-CN/1.9/topics/forms/media/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.