We help IT Professionals succeed at work.

Change substr input to lowercase by percentage of field

alex4544
alex4544 asked
on
Medium Priority
492 Views
Last Modified: 2012-05-06
I have some javascript on a php page attached to an input field to change the letters to lowercase

what I want to do is only change the input to lowercase if a percentage (80%) of that field is capital letters

Attached current code which changes first letter to capital and rest to lowercase
<input type onblur="this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase();"

Open in new window

Comment
Watch Question

HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
80% or more?  Or exactly 80%

Something like this perhaps?

<input type='text' onblur='lowerCheck(this)'>


<script type='text/javascript'>
  function lowerCheck( this ) {
    if ( this && this.nodeName == 'INPUT' ) {
      var text = this.value
      var up   = text.replace( /[^A-Z]/g, '' )
      var per  = ( up.length * 100 ) / text.length
      if ( per >= 80 ) {
        this.value = text.toLowerCase()
      }
    } else {
      alert( 'lowerCheck() - parameter error' )
    }
  }
</script>

Open in new window

Software Engineer
CERTIFIED EXPERT
Commented:
oops... typo... sorry  I shouldn't (can't) use "this" as a local variable name
  function lowerCheck( obj ) {
    if ( obj && obj.nodeName == 'INPUT' ) {
      var text = obj.value
      var up   = text.replace( /[^A-Z]/g, '' )
      var per  = ( up.length * 100 ) / text.length
      alert( per )
      if ( per >= 80 ) {
        obj.value = text.toLowerCase()
      }
    } else {
      alert( 'lowerCheck() - parameter error' )
    }
  }

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
I also forgot to comment out line 6 which I used to tell me the current percentage value.  :-(

Author

Commented:
How can I apply this to multiple input fields - If i try it on a second input field it errors

Author

Commented:
error is
lowerCheck() - parameter error
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
The "onblur" event description identifies the field to be used:

<input type='text' onblur='lowerCheck(this)'>

The "this" keyword is used to identify the current (input) document element

Author

Commented:
At the moment I have:
<script type='text/javascript'>
function lowerCheck( obj ) {
    if ( obj && obj.nodeName == 'INPUT' ) {
      var text = obj.value
      var up   = text.replace( /[^A-Z]/g, '' )
      var per  = ( up.length * 100 ) / text.length
      alert( 'Please turn of CAPSLOCK!' )
      if ( per >= 20 ) {
        obj.value = text.toLowerCase()
      }
    } else {
      alert( 'lowerCheck() - parameter error' )
    }
  }
  </script>

in the Head and then on each field
onblur='lowerCheck(this)'

but the second field errors

HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
What does the second tag look like?

Author

Commented:
<textarea name="message"  onblur='lowerCheck(this)'
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Ah, that's the problem.  Your initial definition only specified

<input type='text' ...

not

<textarea ...

So, you could change the code slightly...
Change the line that looks like this:

if ( obj && obj.nodeName == 'INPUT' ) {

to be:

if ( obj && ( 'value' in obj ) ) {
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Thanks for the grade & points.

Good luck & have a great day
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.