Calculated form field numerical amount IF ELSE

M

MS

Hello,

I have a form with a LoanRequired field which accepts numeric data only.
If user enters >14900 then I want the user to continue as normal with the
default form.
If user enters <1000 then I want a prompt to say "You must apply for at
least £1000"
If user enters between 1000 and 149000 I want to prompt "You must complete
another form" and then redirect to another URL.

Here is my code:
<script>
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}
</script>

Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is able
to submit the form with a value of between 1000 and 149000. The function is
called onBLur (the LoanRequired form field), onClick (The submit button) and
onSubmit(The form tag).

Any ideas?

Thanks!!!

Gary.
PS - I CANNOT REPLICATE THE PROBLEM, I HAVE TRIED EVERYTHING!!!!!
 
M

Michael Winter

[snip]

The type attribute is required for SCRIPT elements. That tag should read:

<script type="text/javascript">

[snip]
Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is
able to submit the form with a value of between 1000 and 149000. The
function is called onBLur (the LoanRequired form field), onClick (The
submit button) and onSubmit(The form tag).

Any ideas?

Not all users have JavaScript enabled and so your code will have no effect
whatsoever.

ALWAYS validate server-side.

Mike
 
M

MS

Not all users have JavaScript enabled and so your code will have no effect
whatsoever.


Hi Mike,

I also collect information about the browser, and whether or not the user
has JavaScript enabled - and even when these things are true, the user still
manages to sometimes get past the script.

Gary.
 
M

Michael Winter

Hi Mike,

I also collect information about the browser, and whether or not the user
has JavaScript enabled - and even when these things are true, the user
still manages to sometimes get past the script.

The only other possibility that I can think of at the moment is that your
comparisons are at fault. You are comparing a string to a number, and if
the proper type conversion doesn't occur, the comparison might fail.

Try converting the strings to numbers, then compare them to be sure.

function calcLoanRequired() {
// give this a more appropriate name
var number = Number( document.form1.LoanRequired.value );

if ( isNaN( number )) {
// Value entered wasn't actually a number
// Cancel the submission and alert the user
} else {
if ( number > 149000 ) {
} else if ( number < 1000 ) {
} else {
alert("GOING TO OTHER FORM");
document.location.href = "http://lowvalue.co.uk";
}
}
}

It's a shame you can't see a pattern in the erroneous submissions.

Good luck,
Mike
 
M

MS

Try converting the strings to numbers, then compare them to be sure.
function calcLoanRequired() {
// give this a more appropriate name
var number = Number( document.form1.LoanRequired.value );

if ( isNaN( number )) {
// Value entered wasn't actually a number
// Cancel the submission and alert the user
} else {
if ( number > 149000 ) {
} else if ( number < 1000 ) {
} else {
alert("GOING TO OTHER FORM");
document.location.href = "http://lowvalue.co.uk";
}
}
}



Hi Mike,

Thanks for the time your spending trying to help me. I have cracked it, its
not the numbers as I have "number only" script on the form field.

Here is what is happening.
When a user hits the submit button and the rate is lower than £149,000 it is
supposed to go to another file. However, it first dsiplays an alert on the
page explaining that a new page is going to be loaded. The onClick
behaviour, has a second function applied to it, which submits the form.
Because of the alert, IE does not actually "appear" to submit the form, even
though it has in fact sent the data. Then, when you press OK on the alert,
the redirection occurs.

Sorted now! :)

Thanks guys,

Gary.
 
H

Harag

Hello,

I have a form with a LoanRequired field which accepts numeric data only.
If user enters >14900 then I want the user to continue as normal with the
default form.
If user enters <1000 then I want a prompt to say "You must apply for at
least £1000"
If user enters between 1000 and 149000 I want to prompt "You must complete
another form" and then redirect to another URL.

Here is my code:
<script>
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}
</script>

Now, I get 100 enquiries a day, and although 90 of them go to the right
place, it seems around 10 a day are ignoring the script and the user is able
to submit the form with a value of between 1000 and 149000. The function is
called onBLur (the LoanRequired form field), onClick (The submit button) and
onSubmit(The form tag).

Any ideas?

Thanks!!!

Gary.
PS - I CANNOT REPLICATE THE PROBLEM, I HAVE TRIED EVERYTHING!!!!!


Maybe the end users dont have Javascript enabled...

HTH
Al.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
function calcLoanRequired()
{
if (document.form1.LoanRequired.value > 149000)
{
}
else if (document.form1.LoanRequired.value < 1000)
{
}
else
{
alert("GOING TO OTHER FORM")
document.location.href=("http://lowvalue.co.uk")
}
}



Minor point : for efficiency, use

var V = document.form1.LoanRequired.value

and then refer just to V.



You have possible problems other than those in the answers I have seen.

What happens if the string contains £12345 or 12,345 or £12,345 ?
Or even €100000 ? (that's a euro sign). It will, converted to Number,
be NaN, therefore unequal to anything.

You should validate the input with a RegExp to ensure that it contains
only digits, after which you can make V into a number by

var V = + document.form1.LoanRequired.value

which increases efficiency too.

Read the FAQ.

Read <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top