• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

Change substr input to lowercase by percentage of field

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

0
alex4544
Asked:
alex4544
  • 7
  • 4
1 Solution
 
HonorGodCommented:
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

0
 
HonorGodCommented:
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

0
 
HonorGodCommented:
I also forgot to comment out line 6 which I used to tell me the current percentage value.  :-(
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
alex4544Author Commented:
How can I apply this to multiple input fields - If i try it on a second input field it errors
0
 
alex4544Author Commented:
error is
lowerCheck() - parameter error
0
 
HonorGodCommented:
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
0
 
alex4544Author 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

0
 
HonorGodCommented:
What does the second tag look like?
0
 
alex4544Author Commented:
<textarea name="message"  onblur='lowerCheck(this)'
0
 
HonorGodCommented:
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 ) ) {
0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now