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

Change value in text box automatically using onBlur=

Hi ALL,

I have a text box with the following code:

<input  name="AD_WEEKS" type="text" class="input"  value="4" size="4" maxlength="2" onBlur="this.value='10';"   >

I use the onBlur to control the max value in the box.

The onBlur works OK but lacks control of the value
I need a script that will work onBlur and do the following

if onBlur the AD_WEEKS <= 10 then leave the value
else AD_WEEKS = 10
End If

Simple but I cant get the right script to make this work


Thanks
Steve
0
TO_Steve
Asked:
TO_Steve
  • 4
  • 3
1 Solution
 
TO_SteveAuthor Commented:
PS: Input must be checked for numeric value
0
 
ajaikumarrCommented:
Hai,

Try this.

<html>
<body>
<input  name="AD_WEEKS" id="AD_WEEKS" type="text" class="input"  value="" size="4" maxlength="2" onBlur="javascript:ValidateVal(this)" onKeypress="return onKeyPress(event, true, false, '')">

<script language="javascript">
function onKeyPress(e, Numeric, Alphabet, SpecialChars) {
      var key;
      var keychar;
      var valid = "";

      if (Numeric)
            valid = valid + "0123456789";
      if (Alphabet)
            valid = valid + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      valid = valid + SpecialChars;

      if(window.event) {
            // for IE, e.keyCode or window.event.keyCode can be used
            key = e.keyCode;
      }
      else if(e.which) {
            // netscape
            key = e.which;
      }
      else {
            // no event, so pass through
            return true;
      }

      keychar = String.fromCharCode(key);
      if (valid.indexOf(keychar) == "-1") {
            return false;
      } else {
            return true;
      }
}

function ValidateVal(objField) {
      if (objField.value != "") {
            if (objField.value < 0) {
                  objField.value = 10
            }else if (objField.value > 10) {
                  objField.value = 10
            }
      }
}
</script>
</body>
</html>

Bye
Ajai
0
 
fritz_the_blankCommented:
Here is a simple fix that seems to do what you want:

<input  name="AD_WEEKS" type="text" class="input"  value="4" size="4" maxlength="2" onBlur="if(isNaN(this.value) || parseFloat(this.value)>10){this.value=10;}"   >


FtB
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TO_SteveAuthor Commented:
Hi Ftb,

I like the code, it works well
Can you modify the code so that the min value in the inputbox must be greater then 1

Thanks
Steve
0
 
fritz_the_blankCommented:
Here you go:

<input  name="AD_WEEKS" type="text" class="input"  value="4" size="4" maxlength="2" onBlur="if(isNaN(this.value) || parseFloat(this.value)>10 || parseFloat(this.value)<=1){this.value=10;}"   >


FtB
0
 
TO_SteveAuthor Commented:
Hi Ftb,

The code seems to work on the >10 but NOT on the <=1 ???


Please check
Thanks
Steve

0
 
TO_SteveAuthor Commented:
Hi,

I fixed the problem

onBlur="if(isNaN(this.value) || (parseFloat(this.value)<=0 || parseFloat(this.value)>10)){this.value=10;}"


Thanks
Steve
0
 
fritz_the_blankCommented:
Sorry, I did <=1 rather than just >1. So, I should have written:

<input  name="AD_WEEKS" type="text" class="input"  value="4" size="4" maxlength="2" onBlur="if(isNaN(this.value) || parseFloat(this.value)>10 || parseFloat(this.value)<1){this.value=10;}"   >

FtB
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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