Skip to:
Content
Pages
Categories
Search
Top
Bottom

Block Characters e.g. @, numbers, dot etc. in text fields / multiline text


  • amkh
    Participant

    @amkh

    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,

Viewing 17 replies - 1 through 17 (of 17 total)

  • Prashant Singh
    Participant

    @prashantvatsh

    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


    amkh
    Participant

    @amkh

    Thank you for your quick response. Is this code valid for “Legacy” template?


    amkh
    Participant

    @amkh

    Also I do not get about #input . Can you please elaborate more?


    Prashant Singh
    Participant

    @prashantvatsh

    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


    amkh
    Participant

    @amkh

    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.

    amkh
    Participant

    @amkh

    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;
                }
          });
    });

    Prashant Singh
    Participant

    @prashantvatsh

    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


    amkh
    Participant

    @amkh

    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 field


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    I just tested and it is working fine. Please check if you have pasted it like this or not: http://prntscr.com/l6ft50

    Thanks


    keshabee
    Participant

    @keshabee

    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.


    amkh
    Participant

    @amkh

    Kindly check screenshots below

    https://prnt.sc/l6odfb

    https://prnt.sc/l6od8u


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    Sorry for the problems here. They should be fixed.


    Prashant Singh
    Participant

    @prashantvatsh

    Sorry for the problems here. They should be fixed.

    We did it yesterday so might be he posted before that time.


    Prashant Singh
    Participant

    @prashantvatsh

    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


    amkh
    Participant

    @amkh

    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.

    https://prnt.sc/l7xg0k
    https://prnt.sc/l7xgf8


    Prashant Singh
    Participant

    @prashantvatsh

    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


    amkh
    Participant

    @amkh

    Thank you very much for your reply. With my little knowledge of coding can you help me out with this code for TinyMCE?

Viewing 17 replies - 1 through 17 (of 17 total)
  • You must be logged in to reply to this topic.
Skip to toolbar