Link to home
Start Free TrialLog in
Avatar of JuniorBee
JuniorBeeFlag for United States of America

asked on

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.
Avatar of nabsol
nabsol
Flag of Pakistan image

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
Avatar of JuniorBee

ASKER

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;    
}
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.
I did post the code above =)
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
Avatar of smaccari
smaccari

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)">
ASKER CERTIFIED SOLUTION
Avatar of mvan01
mvan01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial