admin管理员组文章数量:1125612
I would like to issue an alerted message when a Wordpress content is blank. For example, this is part of the codes:
if(content === '') {
alert('Please write description of your post.');
}
The above codes work well when the WP content is blank for both Visual and HTML Modes of Classic Editor.
The problemm is alert() is browser-related function: it displays a checkbox along with error message above like:
"Don't allow www.mywebsite to prompt you again."
in Firefox, but not in Opera (not tested other browsers yet). If users enable that checkbox, no more error messages will be displayed in the future.
This is not really good because that error message is important for users to complete the WP content before they can publish their posts.
I have also tried the followings:
if(content === '') {
$("Please write description of your post.").dialog();
}
It is quite good as it removes the above checkbox from the error box.
But it has an issue for a long form because it does not show where the field needs filling out, and if I use function focus(), the error box stays where it is while the cursor is moved away to an intended field - then that error box disappears from being seen. It means the cursor move before closing error box with dialog().
Another issue with dialog() is it allows users to type while it is still displaying error box. Alert() wont let users do that before error box is closed.
What is a simple way to display error messages without having issues mentioned above?
Any help is very appreciated.
I would like to issue an alerted message when a Wordpress content is blank. For example, this is part of the codes:
if(content === '') {
alert('Please write description of your post.');
}
The above codes work well when the WP content is blank for both Visual and HTML Modes of Classic Editor.
The problemm is alert() is browser-related function: it displays a checkbox along with error message above like:
"Don't allow www.mywebsite.com to prompt you again."
in Firefox, but not in Opera (not tested other browsers yet). If users enable that checkbox, no more error messages will be displayed in the future.
This is not really good because that error message is important for users to complete the WP content before they can publish their posts.
I have also tried the followings:
if(content === '') {
$("Please write description of your post.").dialog();
}
It is quite good as it removes the above checkbox from the error box.
But it has an issue for a long form because it does not show where the field needs filling out, and if I use function focus(), the error box stays where it is while the cursor is moved away to an intended field - then that error box disappears from being seen. It means the cursor move before closing error box with dialog().
Another issue with dialog() is it allows users to type while it is still displaying error box. Alert() wont let users do that before error box is closed.
What is a simple way to display error messages without having issues mentioned above?
Any help is very appreciated.
Share Improve this question edited Jan 27, 2024 at 17:57 daro2013 asked Jan 27, 2024 at 17:47 daro2013daro2013 251 silver badge6 bronze badges 3 |1 Answer
Reset to default 0Thank you Jacob Peattie for suggesting. It is very simple to use confirm() to replace alert() although confirm() has two options for users to select: OK and Cancel.
When users click OK or Cancel button, I use function focus() to direct users where to correct that empty Content. Here are my working sample codes:
if(content === '') {
confirm('Please write description of your post.');
tinymce.activeEditor.focus(); // for Visual Mode of WP Classic Editor
$('#content').focus(); // for HTML Mode of WP Classic Editor
}
Many thanks
本文标签: validationSimple way to alert error messages in Javascript
版权声明:本文标题:validation - Simple way to alert error messages in Javascript 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736666804a1946708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
window.confirm
instead ofwindow.alert
? – Jacob Peattie Commented Jan 28, 2024 at 2:14