Block Characters e.g. @, numbers, dot etc. in text fields / multiline text
-
Is it possible to block certain words / characters in the Profile edit boxes of text fields or multiline text fields?
Also is it possible that if someone enter such values then the administrator should be notified?
The word / characters that I do not want people to insert are : @, dot , any numerals,
-
Hi,
Please try this code:
jQuery(document).ready(function(){ jQuery('#input').bind('keydown', function (event) { var regex = new RegExp("^[0-9.@]+$"); var key = event.key; if (regex.test(key)) { event.preventDefault(); return false; } }); });
#input will be unique identifier of the input element, please replace with yours.
Thanks
Thank you for your quick response. Is this code valid for “Legacy” template?
Also I do not get about #input . Can you please elaborate more?
Yes, it is valid for legacy template.
Also in place of #input you have to use (.profile-edit #field_number).
‘field_number’ represents fields like field_1, field_2 so you have to just replace field_number with real field id. You can find it in inspection like this: https://prnt.sc/l61ajf
I tried with one text field but keeps giving me the error mentioned below. Though I do not see any problem in your code.
Don't Panic The code snippet you are trying to save produced a fatal error on line 2: syntax error, unexpected ')', expecting variable (T_VARIABLE) The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before. Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.
This is my exact code that I am using it
jQuery(document).ready(function(){ jQuery('.profile-edit #field_2372').bind('keydown',function (event) { var regex = new RegExp("^[0-9.@]+$"); var key = event.key; if (regex.test(key)) { event.preventDefault(); return false; } }); });
Please use https://wordpress.org/plugins/custom-css-js/ plugin to add js code and then just write this code:
jQuery('.profile-edit #field_2372').bind('keydown',function (event) { var regex = new RegExp("^[0-9.@]+$"); var key = event.key; if (regex.test(key)) { event.preventDefault(); return false; } });
Thanks
Hi Prashant
I added the code in the mentioned plugin. I tested by editing that field. I was able to add numbers 1 @ both in this fieldHi,
I just tested and it is working fine. Please check if you have pasted it like this or not: http://prntscr.com/l6ft50
Thanks
Hi, if you want to add screenshots use this site;
Just save the the image screenshot to your computer, ( using tools like snippet) then go to the link
https://postimages.org/
— then set image option to 19inch monitor (1280 x1024)
— no expiration— and upload the saved image, which would generate a link that you copy and add.
Sorry for the problems here. They should be fixed.
Sorry for the problems here. They should be fixed.
We did it yesterday so might be he posted before that time.
Kindly check screenshots below
I have checked your screenshots and yes the code is not placed properly. Please check my screenshot again: https://prnt.sc/l6ft50 I have removed everything and just wrote this:
jQuery(document).ready(function(){ jQuery('.profile-edit #field_1').bind('keydown',function (event) { var regex = new RegExp("^[0-9.@]+$"); var key = event.key; if (regex.test(key)) { event.preventDefault(); return false; } }); });
Please change field id accordingly.
Thanks
Thank you very much dear Prashant for pointing out the mistake. However I noticed the code works for the “text” field whereas for “multiline text” field it does not work. Kindly check the screenshots for multiline text field and the screenshot of the code.
Hi,
It’s because that is not a normal text area. it’s TinyMCE editor and we have to do something else there https://wordpress.stackexchange.com/questions/166077/keyup-events-in-tinymce-editor-not-working
Thanks
Thank you very much for your reply. With my little knowledge of coding can you help me out with this code for TinyMCE?
- You must be logged in to reply to this topic.