Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Need to mask Alphanumeric values with some special characters allowed

Need to mask Alphanumeric values with some special characters allowed

Scheduled Pinned Locked Moved JavaScript
javascriptregexhelp
8 Posts 2 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    I am using an masked-input field as below:

    {{masked-input
    mask='********************'
    disabled=readOnly
    class=(concat 'emberTagInput-input js-ember-tag-input-new' (if readOnly ' is-disabled'))
    maxlength=maxlength
    placeholder=placeholder
    value=model.Address.AltPhone
    input-format='regex'
    input-filter='[A-Za-z0-9 \-_@#]{1,20}'
    input-filter-message='Complaint Id is not valid.'
    maxlength="20"
    }}

    I want to allow all Alphanumeric values with only few special characters -, _, @, #, I don't want to allow other special characters, can somebody please help me what should be the mask value for it. Can somebody please help me in this regards, thanks in advance.

    W 1 Reply Last reply
    0
    • S simpledeveloper

      I am using an masked-input field as below:

      {{masked-input
      mask='********************'
      disabled=readOnly
      class=(concat 'emberTagInput-input js-ember-tag-input-new' (if readOnly ' is-disabled'))
      maxlength=maxlength
      placeholder=placeholder
      value=model.Address.AltPhone
      input-format='regex'
      input-filter='[A-Za-z0-9 \-_@#]{1,20}'
      input-filter-message='Complaint Id is not valid.'
      maxlength="20"
      }}

      I want to allow all Alphanumeric values with only few special characters -, _, @, #, I don't want to allow other special characters, can somebody please help me what should be the mask value for it. Can somebody please help me in this regards, thanks in advance.

      W Offline
      W Offline
      W Balboos GHB
      wrote on last edited by
      #2

      Make life easier for yourself: Create an onkeyup and onchange event for your input control. Create the function target for the events Inside function: use regex (regular expressions) to parse/cleaning 'value' from control. update control value with result. You can also handle the length, as well, truncating to whatever size you wish (a function arg?). A plain javascript function will be useful for more controls - you could avoid the overhead of creating the template.

      Ravings en masse^

      "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

      "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

      S 1 Reply Last reply
      0
      • W W Balboos GHB

        Make life easier for yourself: Create an onkeyup and onchange event for your input control. Create the function target for the events Inside function: use regex (regular expressions) to parse/cleaning 'value' from control. update control value with result. You can also handle the length, as well, truncating to whatever size you wish (a function arg?). A plain javascript function will be useful for more controls - you could avoid the overhead of creating the template.

        Ravings en masse^

        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

        S Offline
        S Offline
        simpledeveloper
        wrote on last edited by
        #3

        I have tried by putting the input-filter as below: input-filter='[A-Za-z0-9 \-_@#]{1,20}', it is accepting all the Alpha Numeric values but not allowing the 3 special characters that I want it to allow: -,@,#, can you please help me in this regards. If mouse functions are better, can you please give me an example here - thanks in advance my friend.

        W 1 Reply Last reply
        0
        • S simpledeveloper

          I have tried by putting the input-filter as below: input-filter='[A-Za-z0-9 \-_@#]{1,20}', it is accepting all the Alpha Numeric values but not allowing the 3 special characters that I want it to allow: -,@,#, can you please help me in this regards. If mouse functions are better, can you please give me an example here - thanks in advance my friend.

          W Offline
          W Offline
          W Balboos GHB
          wrote on last edited by
          #4

          OK - lets go about this analytically: It works for A-Za-z0-9 Now add just one of your special characters at a time and see which, if any work - or what we're really looking for - is which one is breaking your string. Note, also, that shortcuts exist for standard numeric and alpha ranges: [^] And, if I read it correctly, '\w' is the equivalent of '0-9A-Za-z' and the do mention the hyphen character, as well.

          • \d – is the same as [0-9]
          • \w – is the same as [a-zA-Z0-9_],
          • \s – is the same as [\t\n\v\f\r ], plus few other rare unicode space characters.

          Comes from that page. Find out the character that's killing you. It will help you remember three things (1) how to hunt for the problem (2) when you find the bad actor(s), why they're bad, (3) burn it into your mind so it is not only remembered but parallel problems are identified in advance.

          Ravings en masse^

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          S 1 Reply Last reply
          0
          • W W Balboos GHB

            OK - lets go about this analytically: It works for A-Za-z0-9 Now add just one of your special characters at a time and see which, if any work - or what we're really looking for - is which one is breaking your string. Note, also, that shortcuts exist for standard numeric and alpha ranges: [^] And, if I read it correctly, '\w' is the equivalent of '0-9A-Za-z' and the do mention the hyphen character, as well.

            • \d – is the same as [0-9]
            • \w – is the same as [a-zA-Z0-9_],
            • \s – is the same as [\t\n\v\f\r ], plus few other rare unicode space characters.

            Comes from that page. Find out the character that's killing you. It will help you remember three things (1) how to hunt for the problem (2) when you find the bad actor(s), why they're bad, (3) burn it into your mind so it is not only remembered but parallel problems are identified in advance.

            Ravings en masse^

            "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

            "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

            S Offline
            S Offline
            simpledeveloper
            wrote on last edited by
            #5

            OK I have written a function for onKeyUp, as below, but what I want is to remove those special characters that are not from the 3 in the above list -, @, #. Can you please modify the function how can I remove them automatically? Or suggest me for something. Thanks in advance my friend, JS function:

            forTest(e) {
            e = 'some test';
            },

            Ember Control (html input-tag control):

                                    {{#tag-input
                                    tags=tags
                                    addTag=(action 'addTag')
                                    onKeyUp=(action 'forTest')
                                    removeTagAtIndex=(action 'removeTagAtIndex')
                                    as |tag|
                                    }}
                                    {{tag}}
                                    {{/tag-input}}
            
            W 1 Reply Last reply
            0
            • S simpledeveloper

              OK I have written a function for onKeyUp, as below, but what I want is to remove those special characters that are not from the 3 in the above list -, @, #. Can you please modify the function how can I remove them automatically? Or suggest me for something. Thanks in advance my friend, JS function:

              forTest(e) {
              e = 'some test';
              },

              Ember Control (html input-tag control):

                                      {{#tag-input
                                      tags=tags
                                      addTag=(action 'addTag')
                                      onKeyUp=(action 'forTest')
                                      removeTagAtIndex=(action 'removeTagAtIndex')
                                      as |tag|
                                      }}
                                      {{tag}}
                                      {{/tag-input}}
              
              W Offline
              W Offline
              W Balboos GHB
              wrote on last edited by
              #6
              1. Whatever ember-control is, I don't use it. I use straight javaScript, no "frameworks". 2) Did you try, as I suggested yesterday, eliminating some of your special characters from your regex expression and see which of them work as expected? You didn't say. OK - why I send you into this direction: I use php/javaScript/SQL all in the same page, which is often a generated page. It can absolutely not work on the basis of improperly handled nested single and double quotes - which only co-exist on the final page and it's occasionally not what one expects. Unless it sticks out in an obvious way (good luck) I need to roll back parts of the page creation until it works and then I have a point to look for what's not phased correctly. You are in a similar situation: plain alpha-numeric filtering works. Your entire regex expression, however, does not. Do it piece by piece: only the alpha-numeric and then add one of your special characters. If it works, add another. If it fails - try another (not add to a failed version), which hopefully will now work without the know failure-causing character. For that matter, you may even wish to try just reversing the order of your special characters near the end of your regex expression. But you've not told me what you have tried to uncover the problem - I want to teach you how to do this; much more valuable than the expression, itself.

              Ravings en masse^

              "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

              "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

              S 1 Reply Last reply
              0
              • W W Balboos GHB
                1. Whatever ember-control is, I don't use it. I use straight javaScript, no "frameworks". 2) Did you try, as I suggested yesterday, eliminating some of your special characters from your regex expression and see which of them work as expected? You didn't say. OK - why I send you into this direction: I use php/javaScript/SQL all in the same page, which is often a generated page. It can absolutely not work on the basis of improperly handled nested single and double quotes - which only co-exist on the final page and it's occasionally not what one expects. Unless it sticks out in an obvious way (good luck) I need to roll back parts of the page creation until it works and then I have a point to look for what's not phased correctly. You are in a similar situation: plain alpha-numeric filtering works. Your entire regex expression, however, does not. Do it piece by piece: only the alpha-numeric and then add one of your special characters. If it works, add another. If it fails - try another (not add to a failed version), which hopefully will now work without the know failure-causing character. For that matter, you may even wish to try just reversing the order of your special characters near the end of your regex expression. But you've not told me what you have tried to uncover the problem - I want to teach you how to do this; much more valuable than the expression, itself.

                Ravings en masse^

                "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                S Offline
                S Offline
                simpledeveloper
                wrote on last edited by
                #7

                Yes I tried, its the mask value, which I am setting as mask='********************' is not allowing me to enter any special characters, if I remove it, its allowing all the characters and without limit. Yes my exposure to JavaScript and Frameworks is limited, I am not able to find solution. If I remove mask='********************', its not only allowing me all characters but unlimited number of characters, even if I try to set the max length property to 20 - need some help, thank you.

                W 1 Reply Last reply
                0
                • S simpledeveloper

                  Yes I tried, its the mask value, which I am setting as mask='********************' is not allowing me to enter any special characters, if I remove it, its allowing all the characters and without limit. Yes my exposure to JavaScript and Frameworks is limited, I am not able to find solution. If I remove mask='********************', its not only allowing me all characters but unlimited number of characters, even if I try to set the max length property to 20 - need some help, thank you.

                  W Offline
                  W Offline
                  W Balboos GHB
                  wrote on last edited by
                  #8

                  Get rid of the template! In the same function where you use the regex expression to clean the string, get the length of the cleaned string and if it's greater than 20 (or whatever value you like) the send back a "Substring" of length 20 as the value. Simple as that. No annoying masks to worry about for some so simple as a max-length string constraint. If your string's name is 'mystring'

                  if( mystring.length > 20)
                  mystring = mystring.substring(0, 20);

                  It takes some learning, but all the tools you need are already in javaScript.

                  Ravings en masse^

                  "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                  "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups