Home > Net >  Restrict Uppercase in input text
Restrict Uppercase in input text

Time:11-29

I have a JS where I can detect if uppercase is being entered, but I'm unable to convert the text text to lower case. i have used CSS text-transform: lowercase; but this shows that it's lower but once submitted, it shows the text as normal (original format). I have tried with jquery toLowerCase(); but I don't know what I have missed here coz it didn't work. this is my script,

  const setup = function(fieldSelector) {
    const field = $(fieldSelector);
    const upperCase= new RegExp('[A-Z]');
    const applyStyle = function() {

      if (field.val().match(upperCase))
          {
        field.val().toLowerCase();
      } else {
        field.val();
      }
    };
    field.on('change', applyStyle);
    applyStyle();
  }
  // Note: Change the ID according to the custom field you want to target.
  setup('#issue_custom_field_values_17');
});

this code is used for redmine "View customize plugin"

CodePudding user response:

Uppercase doesn't change the original string. Try assigning a new variable for your uppercase and see if it works!

CodePudding user response:

I think this should do the job for you

$('input').keyup(function(){
 let val = $(this).val().toLowerCase()
 $(this).val(val)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text">

CodePudding user response:

try this

const char = string.charAt(field.val());
const isUpperCaseLetter = (/[A-Z]/.test(char));

CodePudding user response:

You can set the value of the field to the new lowercase value on change

 const setup = function(fieldSelector) {
        const field = $(fieldSelector);
        const upperCase= new RegExp('[A-Z]');
        // Here's the change
        const applyStyle = function() {
         field.val( field.val().toLowerCase() )
        };
        field.on('change', applyStyle);
        applyStyle();
      }
      // Note: Change the ID according to the custom field you want to target.
      setup('#issue_custom_field_values_17');
    });
  • Related