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

Check form field for integer betwen 1 and 5000

I have a form field called 'amount'.  When the form is submitted, I need to check to make sure that it contains an integer equal to or greater than 1 and that it's not more than 5000.

I tried using the built in form field valiation in Frontpage, but it messed up my other javascript functions on the page somehow so I can't use that.
0
JuniorBee
Asked:
JuniorBee
1 Solution
 
nabsolCommented:
Hope this helps!

<head>
<script language="JavaScript">
function check()
{
   if (document.getElementById("amount").value >= 1 && document.getElementById("amount").value <= 5000){
        return true;
    }
    else{
       alert("Enter Between 1 and 5000")
       return false;
    }
       
}
</script>
</head>
<body>
<form onsubmit="return check()">
<input id="amount" type="text" size="5">
<input type="submit" value="submit">
</form>
<body>

By Nab
0
 
JuniorBeeAuthor Commented:
Super great!

But it messes up my button.  I have a button (2 buttons) that go disabled after clicking to submit.
When I enter an invalid number and submit the form, the alrt pops up and says "Enter Between 1 and 5000", but my buttons are disabled so I cannot submit the form again :(

I tried working in something to couteract the disabled function but I am not so good at javascript syntax.  Here is the function, do you think you could help me make the button not disabled it the alert box pops up?
 =) Thx

function disable(f) {
    var button = f.elements['WithdrawButton'];
    button.value = 'Processing';
    button.disabled = 'true';
    return true;    
}

function disable2(f) {
    var button = f.elements['Cancel'];
    button.value = 'Cancel';
    button.disabled = 'true';
    return true;    
}
0
 
nabsolCommented:
What do u do. You have two buttons and you are calling these two functions when they are clicked. Can you post some code here.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JuniorBeeAuthor Commented:
I did post the code above =)
0
 
nabsolCommented:
I wonder how      button.disabled = 'true'     is working.   You have to write      button.disabled = true  

Anyway so if you don't want to disable your buttons then REMOVE this line::    button.disabled = true;      
or if you want to enable any button use :      button.disabled = false;


So button.disabled = true for disabling and  button.disabled = false for enabling :-)



By Nab
0
 
smaccariCommented:
Just suppress your call to your functions in the onclick of your submit (i suppose that the call is made there), and modify nabsol's function as this:

function check(frm)
{
   if (document.getElementById("amount").value >= 1 && document.getElementById("amount").value <= 5000){
        disable(frm);
        disable2(frm);
        return true;
    }
    else{
       alert("Enter Between 1 and 5000")
       return false;
    }
       
}

And the call to this function in the form element:


<form onsubmit="return check(this)">
0
 
mvan01Commented:
To be sure that the value is a number, and an integer, you may wish to do this:

var i = parseInt(document.getElementById("amount").value);         // returns a number - an integer
var f = document.getElementById("amount").value*1;                    // returns a floating point number, same as parseFloat(...) would, but it's easier
if (isNaN(i) || i != f) {
  alert ('Please enter an integer value only');
  return false;
}

and then continue in nabsol's or smaccari's function, with your compare, thus:

if (1<=i && i <= 5000) {

Peace and joy.  mvan
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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